Create WebService and three call modes (reproduced) using C #

xiaoxiao2021-03-06  63

9cbs online

A more important part of Microsoft .NET strategy is WebService, using WebService we can create a real valid distributed application.

Here, we do some instructions for WebService.

Suppose A is the client, B is the WebService server, and the user sends SOAP requests to the server via HTTP protocol, and WebService returns data in the client XML format.

Now let's take a look at the process of creating a WebService:

The server's WebService is required. In the middle of SOAP, XML we don't have to care, on the client, how is it more important to get an object from WebService? The answer is to use the Proxy object. The client is responsible for communicating with WebService by the proxy object (Proxy). So use WebService in the client, which is completely and uses a local object.

We now explain with a simple example.

Open vs.net, new project (ASP.NET Web Service), type http: // localhost / webserver in your location, where WebServer is the name of the project. After confirming, a service1.asmx.cx appears, 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 Web Server

{

///

/// service1's summary description.

///

(1)

Public class service1: system.web.services.Webservice

{

Public service1 ()

{

// Codegen: This call is necessary for the ASP.NET Web service designer.

InitializationComponent ();

}

#Region Component Designer Generated Code

/ / Web service designer necessary

Private icontainer Components = NULL;

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing && Components! = NULL)

{

Components.dispose ();

}

Base.dispose (Disposing);

}

#ndregion

// Web service example

// HelloWorld () sample service Returns Hello World

// To generate, cancel the following list, then save and generate items

/ / To test this web service, press F5 key

// [WebMethod]

// public string helloworld ()

// {

// Return "Hello World";

//}

}

}

The following is added in (1)

[Webservice (Namespace = "http:// localhost / webserver /")]

This is because SOAP is based on the HTTP protocol, and the client cannot know that WebService is located on that server. In practical applications, such as http://www.orfly.com, Namespace is changed to http://www.orfly.com/webserver.

Let's add a method to this WebService.

// [WebMethod]

// public string helloworld ()

// {

// Return "Hello World";

//}

Microsoft helped us write one, then add a method. The name is called show.

[WebMethod]

Public String Show (String Yourname)

{

Return "http://www.orrfly.com" "Welcome" YourName

}

Now, you can run, press F5, click Show, enter your name, then click INVOTE

I saw it.

Successful. Open the bin directory, VS.NET has done proxy .webserver.dll.

Now we test in different environments:

1. Open VS.NET, create a new "Windows Application" project, named Client, add buttons, text box.

Now you have to use the agent, right-click on the REFERENCE (reference), select Add Reference, select Browse, find the webserver.dll under the BIN directory under the Webserver directory.

Add a reference to System.Web.WebServices, there is in the list.

Add to Form1.cs

Using system.Web.services;

Using Web Server;

In

Private system.windows.Forms.Button button1;

Private system.windows.Forms.TextBox textBox1;

Behind, insert

Private WebServer.Service1 Client

Create an instance of a service1. Double-click the button, 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 the project, click the button, and display in the text box.

Http://www.orfly.com Welcome to the tornado .NET

2. ASP.NET Web window test

The method is the same as the above, add a reference to establish an instance of Service1.

Not in detail here.

3. Test in VB

This is relatively complicated

First establish a "standard EXE" project in VB. Add a reference: Microsoft SOAP TYPE LIBRARY. Note: If you don't have Microsoft SOAP Toolkit, there is no such library. Can be downloaded in http://www.orrfly.com.

Add a Text

Private sub flow_load ()

TEXT1.TEXT = Add ()

End Sub

Public Function Add () AS STRING

DIM ObjsoapClient as new soapclient

ObjsoapClient.ClientProperty ("ServerHttpRequest") = True

Call ObjsoapClient.msoapinit ("http://localhost/webserver/service1.asmx? WSDL", "Service1", "Service1SoAP")

This sentence can also

Objsoapclient.msoapinit ("http://localhost/webserver/service1.asmx? WSDL")

Add = ObjsoapClient.show ("Tornado .NET")

END FUNCTION

Commissioning success requires attention:

Run the server WebService program, appear below

Support the following. For a formal definition, check the service instructions.

Click on the service instructions to get a complete WSDL file

http://localhost/webserver/service1.asmx? WSDL

We will use this file, which contains our definition methods, and more.

Usage:

The second, the third parameter can be found in the WSDL file. It can also be omitted.

转载请注明原文地址:https://www.9cbs.com/read-113490.html

New Post(0)