Using C # to create WebService and three ways of calling

Source: Internet
Author: User
web| to create Microsoft. A more important part of the net strategy is webservice, with WebService we can create truly efficient distributed applications.
Below, we do some explaining to webservice.
Assuming that a is the client, B is the WebService server, and the user sends a SOAP request through the HTTP protocol, WebService returns the data in the client XML format.
Now let's take a look at the general process of creating a webservice:
The webservice of the service side are to be built. The middle of the soap,xml we do not have to care, in the client side, more important is how to obtain objects from WebService? The answer is to use a proxy object. The client is responsible for communicating with the WebService by proxy object (proxy). So using WebService on the client is exactly the same as using a local object.

Let's now describe it as a simple example.
Open Vs.net, create a new project (asp.net Web Service), and type Http://localhost/webserver in the location, where webserver is the name of the project. OK, after a Service1.asmx.cx, double click, the Code window appears,
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.Web;
Using System.Web.Services;

namespace webserver
{
<summary>
Summary description of the Service1.
</summary>
(1)
public class Service1:System.Web.Services.WebService
{
Public Service1 ()
{
CodeGen: This call is required by the ASP.net Web service designer
InitializeComponent ();
}

#region Component Designer generated code

Required by the WEB service designer
Private IContainer components = null;

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing && components!= null)
{
Components. Dispose ();
}
Base. Dispose (disposing);
}

#endregion

WEB Services Sample
HelloWorld () sample service return string Hello World
To build, uncomment the following lines, and then save and build your project
To test this WEB service, press the F5 key

[WebMethod]
public string HelloWorld ()
// {
Return to "Hello World";
// }
}
}
The following is added at (1)
[WebService (namespace= "http://localhost/webserver/")]
This is because SOAP is based on the HTTP protocol, and the client cannot know 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.

Now let's add a method to this webservice.
[WebMethod]
public string HelloWorld ()
// {
Return to "Hello World";
// }
Microsoft has written one for us, then added a method. The method name is called show.
[WebMethod]
public string Show (String yourname)
{
Return "http://www.ourfly.com" + "Welcome" +YOURNAME;
}
Now, you can run it, press F5, click Show, type your name, and then click Invote
See it.
<?xml version= "1.0" encoding= "Utf-8"?>
<string xmlns= "http://tempuri.org/" >http://www.ourfly.com welcome yyg</string>

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

Now we are testing in different environments:
1. Open Vs.net, create a new "Windows Application" project, name 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 available 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 and the code is as follows:
private void Button1_Click (object sender, System.EventArgs e)
{
Client =new Service1 ();
String name;
Name=client.show ("Tornado. NET");
Textbox1.text=name;
}
Press F5, run project, click Button, text box to display
Http://www.ourfly.com welcome tornado. NET


2. Testing the ASP.net web window
Method is the same as the above, add a reference, create an instance of Service1
I'm not here to elaborate.
3. Test in VB
This is going to be relatively complicated.
First in VB to establish a "Standard EXE" project. Add Reference: Microsoft Soap Type Library. Note: If you do not have the Microsoft Soap Toolkit installed, there is no library of this type.
can be downloaded in http://www.ourfly.com.
Add a text
Private Sub Form_Load ()
Text1.Text = Add ()
End Sub

Public Function Add () as String
Dim Objsoapclient as New soapclient
Objsoapclient.clientproperty ("serverhttprequest") = True
Call Objsoapclient.mssoapinit ("Http://localhost/webserver/service1.asmx?WSDL", "Service1", "Service1Soap")
That's okay.
Objsoapclient.mssoapinit ("Http://localhost/webserver/service1.asmx?WSDL")

ADD = Objsoapclient.show ("Tornado. NET")
End Function

The success of debugging requires attention:
The program that runs the service-side webservice, appears below
The following actions are supported. For a formal definition, check the service description.
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]) Use of:
The second and third parameters are found in the WSDL file. can also be omitted.


Follow up: There are also generated WSDL files from COM, and so on, I will be introduced in succession.


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.