zoukankan      html  css  js  c++  java
  • Web Service Tutorial in C#

    Web Service Tutorial in C#


    Creating a Web Service in C#.NET is remarkably easy, just follow the steps here and you'll have yourself a Web Service in no time!

    Starting the Project

    In Visual Studio.NET open a new project and select the ASP.NET Web Service template for C#. This will give you the following code:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    namespace WebService1
    {
    /// 
    /// Summary description for Service1.
    /// 
    public class Service1 : System.Web.Services.WebService
    {
    public Service1()
    {
    //CODEGEN: This call is required by the ASP.NET Web Services Designer
    InitializeComponent();
    }
    // WEB SERVICE EXAMPLE
    // The HelloWorld() example service returns the string Hello World
    // To build, uncomment the following lines then save and build the project
    // To test this web service, press F5
    //		[WebMethod]
    //		public string HelloWorld()
    //		{
    //			return "Hello World";
    //		}
    }
    }
    

    You will notice that there is already an example in this code block. All you have to do is uncomment the example and run the project.

    But since you came here to learn how to create these, lets spice things up and make our own.

     

    Building the Functions

    Lets create two simple methods to demonstrate how web services work. One we will call SayHello and it will accept one parameter, the other will be called SayGoodbye and it will not accept any parameters.

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    namespace HelloService
    {
    public class HelloService : System.Web.Services.WebService
    {
    public HelloService()
    {
    //CODEGEN: This call is required by the ASP.NET Web Services Designer
    InitializeComponent();
    }
    [WebMethod]
    public string SayHello(String strMyName)
    {
    return "Hello " + strMyName;
    }
    [WebMethod]
    public string SayGoodBye()
    {
    return "Goodbye!";
    }
    }
    }
    

    Notice that each method has the WebMethod attribute, this lets .NET know that we would like to expose these methods over the web.

     

    Attributes

    Ok so our class is basically completed, but this class doesn't tell others much about what our class is for. Wouldn't it be nice if we could give a discription to others who view our web service so they knew what our service was all about? Well as I'm sure you guessed already, we can! Take a peek at this modified block of code.

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    namespace HelloService
    {
    [WebService(
    Namespace="http://abstractvb.com/",
    Name="Hello Web Service in C#",
    Description="My Description")]
    public class HelloService : System.Web.Services.WebService
    {
    public HelloService()
    {
    //CODEGEN: This call is required by the ASP.NET Web Services Designer
    InitializeComponent();
    }
    [WebMethod]
    public string SayHello(String strMyName)
    {
    return "Hello " + strMyName;
    }
    [WebMethod]
    public string SayGoodBye()
    {
    return "Goodby!";
    }
    }
    }
    

    Notice that there are now three attributes on the Web Service tag? We now have the namespace, Name and Description; these tags allow us to expose information to people who are browsing the description of our web service.

     

    Start It Up

    Go on and give your new web service a compile and run (F5). Visual Studio.NET will take you to http://localhost/Service1/Service1.asmx where you will see your name and description attributes displayed.

    If you click on the Service Description link you will goto the same webpage but with the ?wsdl parameter after it like this: http://localhost/Service1/Service1.asmx?WSDL

    But if you click on our method SayGoodbye it will take you to another page that has all information anyone needs to connect up to and starting using your web service. That is the best part of all this, since this is based on open standards like XML and SOAP, anyone using any language can use your web service!

    Try click on the Invoke button. You will get your result displayed like so:

    <?xml version="1.0" encoding="utf-8" ?>
    <string xmlns="http://abstractvb.com/">Goodbye!</string>
    

    As you can see your web service works, and it's returning the value Goodby!.

    Try going back and clicking on the GoodBye method. You will notice it has a textbox. this is so you can fill in the parameter to the function. Enter you name a press the invoke button and you will notice that it uses your name on the reply from the web service.

    Using a Web Service

    Now that you know how to make a web service, lets put it to work by calling it from a normal VB Windows Application. So first create a new windows application.

    In the solution explorer window right-click on References and select Add Web Reference.

    In the Add a Web Reference window enter the path to your Web Service (http://localhost/Service1/Service1.asmx ) Then Click on the Add Reference button.

    You now can use the web service in your application just like any other class. Here is an example:

    (note: you will need to add a button to your form called button1)

    private void button1_Click(object sender, System.EventArgs e)
    {
    localhost.HelloWebServiceinC MyService = new localhost.HelloWebServiceinC();
    System.Console.WriteLine(MyService.SayHello("John"));
    System.Console.WriteLine(MyService.SayGoodBye());
    }
    

    That's all there is to consuming a web service.

     

    Conclusion

    This is by no means a complete list of what you can do with web services, but it's a start and it should have you on track to building distributed applications using web services in no time.

  • 相关阅读:
    layer ----- 弹层
    php start
    node.js使用iconv-lite和zlib解决gzip压缩和gbk乱码
    AngularJS中promise的使用
    AngularJS中的$routeProvider
    AngularJS入门教程记录
    AngularJS中的$resource
    Javascript原型易错点记录
    触屏相关事件问题记录
    图片预加载
  • 原文地址:https://www.cnblogs.com/taoeternal/p/640293.html
Copyright © 2011-2022 走看看