How to create WebService using C #

Source: Internet
Author: User

Using C # To create WebService, the service side of the webservice is a must, the middle of the soap,xml we do not care. Here is a brief introduction to creating WebService using C #.

Ad:51cto Technology Salon | Give apps an amazing interaction and experience >>

C # Learning to a certain extent will involve some advanced knowledge of C # creation WebService, this article tries to make a brief introduction.

Suppose A is the client, B is the WebService server, the user sends a SOAP request to the servers through the HTTP protocol, and WebService returns the data in the client XML format.

Now let's take a look at creating an approximate procedure for creating a C # WebService:

The webservice of the service side must be built. In the middle of the soap,xml we do not have to care, on the client side, the more important is how to get the object from WebService? The answer is to use a proxy object. The client is responsible for the communication with the WebService by proxy objects (proxies). So using WebService on the client is exactly the same as using a local object.

We now create a WebService instance with a simple C # to illustrate.

Open Vs.net, New Project (ASP), type Http://localhost/webserver in the location, where webserver is the name of the project. After determining, a Service1.asmx.cx appears, double-click, the Code window appears,

  1. Using System;
  2. Using System.Collections;
  3. Using System.ComponentModel;
  4. Using System.Data;
  5. Using System.Diagnostics;
  6. Using System.Web;
  7. Using System.Web.Services;
  8. namespace webserver
  9. {
  10. ///
  11. A summary description of the Service1.
  12. ///
  13. (1)
  14. public class Service1:
  15. System.Web.Services.WebService
  16. {
  17. Public Service1 ()
  18. {
  19. CODEGEN: The call is an ASP. NET Web
  20. Required by the service designer
  21. InitializeComponent ();
  22. }
  23. #region Component Designer generated code
  24. Required by the WEB service designer
  25. Private IContainer components = null;
  26. ///
  27. The designer supports the required method-
  28. Do not use the Code editor to modify
  29. The contents of this method.
  30. ///
  31. private void InitializeComponent ()
  32. {
  33. }
  34. ///
  35. Clean up all the resources that are in use.
  36. ///
  37. protected override void Dispose
  38. (bool disposing)
  39. {
  40. if (disposing && Components! = null)
  41. {
  42. Components. Dispose ();
  43. }
  44. Base. Dispose (disposing);
  45. }
  46. #endregion
  47. WEB Services Sample
  48. HelloWorld () sample service return string Hello World
  49. To build, uncomment the following line, and then save and build your project
  50. To test this WEB service, press the F5 key
  51. [WebMethod]
  52. Public string HelloWorld ()
  53. // {
  54. return "Hello World";
  55. // }
  56. }
  57. }

Join at (1) below

[WebService (namespace= "http://localhost/webserver/")]

This is because SOAP is based on the HTTP protocol, and the client cannot know that WebService is on that server. In practical applications, such as placing this webservice on http://www.ourfly.com, the namespace is changed to Http://www.ourfly.com/webserver.

Let's add a method to this webservice.

  1. [WebMethod]
  2. Public string HelloWorld ()
  3. // {
  4. return "Hello World";
  5. // }
  6. Microsoft helped us write one, and then added a method.
  7. The method name is called show.
  8. [WebMethod]
  9. public string Show (String yourname)
  10. {
  11. Return "http://www.ourfly.com" + "Welcome" +YOURNAME;
  12. }
  13. Now, you can run it, press F5, click Show, enter your name,
  14. Then click on Invote to see it.
  15. 〈 ? XML version= "1.0" encoding= "utf-8"?
  16. 〈string xmlns="http://tempuri.org/"
  17. Http://www.ourfly.com Welcome yyg〈/string〉

It worked. Open the Bin directory, Vs.net has done the proxy. Webserver.dll.

Now we test in a different environment:

1. Open vs.net, new "Windows Application" project, named Client, add button, text box.

Now to use the agent, right-click on the right side of the reference (reference), select "Add Reference", select Browse, find the webserver directory under the bin directory Webserver.dll
Add a system.web.webservices reference, which is in the list.

In the Form1.cs, join

Using System.Web.Services;

Using Webserver;

And then in

Private System.Windows.Forms.Button button1;

Private System.Windows.Forms.TextBox TextBox1;

Back, insert

Private Webserver.service1 Client

Create an instance of Service1. Double-click the button with the following code:

    1. private void button1_click
    2. (object sender, System.EventArgs e)
    3. {
    4. Client =new Service1 ();
    5. string name;
    6. Name=client.show ("tornado. Net");
    7. textbox1.text=name;
    8. }

Press F5, run project, click button, text box shows http://www.ourfly.com welcome tornado. Net

2. Testing the ASP. NET Web window

The method is exactly the same as above, adding references, and creating an instance of Service1 is not in detail here.

3. Testing in VB

This will be relatively complex, first in VB to establish a "Standard EXE" project. Add Reference: Microsoft Soap Type Library.

Note: If Microsoft Soap Toolkit is not installed, there is no such type library. can be downloaded in http://www.ourfly.com.

  1. Add a text
  2. Private Sub Form_Load ()
  3. Text1.Text = Add ()
  4. End Sub
  5. Public Function Add () as String
  6. Dim objsoapclient as New
  7. SoapClient objsoapclient.
  8. Clientproperty ("serverhttprequest") = True
  9. Call Objsoapclient.mssoapinit (
  10. "Http://localhost/webserver/service1.asmx?WSDL",
  11. "Service1", "Service1Soap")
  12. This sentence can also be
  13. Objsoapclient.mssoapinit (
  14. "Http://localhost/webserver/service1.asmx?WSDL")
  15. ADD = Objsoapclient.show ("tornado. Net")
  16. End Function

To be aware of the successful commissioning:

Programs that run the service-side WebService support the following operations when they occur. about the formal definition.

Click on the service description to get the full WSDL file Http://localhost/webserver/Service1.asmx?WSDL

We're going to use this file, which contains the methods we've defined, and so on. MSSoapInit (Bstrwsdlfile as string,[bstrservicename as String],[bstrport as String], [Bstrwsmldile as String]) Usage: The second one, the third parameter can be found in the WSDL file. can also be omitted.

This is the simple process of creating WebService in C #, which I hope will help you.

How to create WebService using C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.