Use C # to create WebService and three call modes

xiaoxiao2021-03-06  62

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 necessary. 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 determining the emergence of a Service1.asmx.cx, double-click, the code window, using System appears; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web. Summary Description of Services; Namespace WebServer {///

/// service1. /// (1) public class service1: system.web.services.WebService {public service1 () {// codegen: This call is the initializeComponent ();} #} #} #} # Region Component Designer Generated Code // Web Service Designer The private icontainer component = null; /// /// designer supports the required method - do not use the code editor to modify the // / this method . /// private void initializecomponent ()} /// /// Clean all the resources being used.

/// Protected Override Void Dispose (Bool Disposing) {IF (Disposing && Components! = NULL) {Components.dispose ();} #ndregion // Web Services 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 // [WebMethod] // Public String HelloWorld () // {// Return "Hello World"; //}}} Add [WebSpace = "http: // localhost / webserver /")] This is because SOAP is based on HTTP protocol The client cannot know that WebService is on that server. In practical applications, this WebService is placed on http://www.orfly.com, then 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. Method name is show. [WebMethod] public string show (String Yourname) {return "http://www.orfly.com" "Welcome" YourName;} Now, you can run, press F5, click Show, enter Your name, then click INVOTE to see it. Success. Open a 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 use to use the agent, right-click Reference Reference, select Add Reference, select Browse, find the webserver.dll under the BIN directory under the Webserver directory, add a reference to the reference, in There are in the list. In form1.cs was added using System.Web.Services; using webserver; button1 then the private System.Windows.Forms.Button; private System.Windows.Forms.TextBox textBox1; later, is inserted to establish a private webserver.service1 Client service1 Example.

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

New Post(0)