Use C # (. net1.1) to create WebService and three call Methods

Source: Internet
Author: User
A more important part of Microsoft's. NET strategy is WebService. With WebService, we can create truly effective distributed applications. Program .
Below, we will give some instructions on WebService.
Assume that A is a client and B is a WebService server. The user sends a SOAP request to the server over HTTP, and WebService returns XML data from the client.
Now let's take a look at the general process of creating a WebService:
The WebService of the server must be created. We don't need to care about the soap and XML in the middle. on the client side, it is important to get objects from WebService? The answer is to use a proxy object. Proxy is used to communicate with WebService. Therefore, using WebService on the client is the same as using a local object.

We will describe it with a simple example.
Open vs.net, create a project (Asp.net web service), and type http: // localhost/webserver in the location, where WebServer is the name of the project. After confirmation, A service1.asmx. CX is displayed. Double-click it.CodeWindow,
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 of 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>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing & components! = NULL)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Endregion

// Web service example
// Helloworld () Sample Service returns the string Hello World
// To generate a project, uncomment the following lines and save and generate the project
// To test this web service, press F5

// [Webmethod]
// Public String helloworld ()
//{
// Return "Hello World ";
//}
}
}
Add
[WebService (namespace = "http: // localhost/webserver/")]
This is because soap is based on the HTTP protocol, and the client cannot know the server where the WebService is located. In practice, for example, placing this WebService on a http://www.ourfly.com, The namespace is changed to the http://www.ourfly.com/webserver.

Next we will add a method for this WebService.
// [Webmethod]
// Public String helloworld ()
//{
// Return "Hello World ";
//}
Microsoft helped us write one, and then added a method. The method name is show.
[Webmethod]
Public String show (string yourname)
{
Return "http://www.ourfly.com" + "welcome" + yourname;
}
Now you can run it. Press F5, Click Show, enter 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>

Succeeded. Open the bin directory and vs.net has completed proxy. webserver. dll.

Now we test in different environments:
1. Open vs.net, create a "Windows application" project, name it "client", add a button, and text box.
Now you want to use the proxy. Right-click reference on the right, select Add reference, and select Browse. Find webserver. dll under the bin directory under the webserver directory.
Add a reference to system. Web. WebServices, which is included in the list.
In form1.cs, add
Using system. Web. Services;
Using webserver;

Then
Private system. Windows. Forms. Button button1;
Private system. Windows. Forms. textbox textbox1;
Insert
Private webserver. service1 Client
Create a service1 instance. Double-click the button. The Code is as follows:
Private void button#click (Object sender, system. eventargs E)
{
Client = new service1 ();
String name;
Name = client. Show ("tornado. Net ");
Textbox1.text = Name;
}
Press F5 to run the project, click the button, and the text box is displayed.
Http://www.ourfly.com welcome tornado. net

2. ASP. NET web window test
the method is the same as above. Add reference and create a service1 instance.
I will not elaborate here.
3. Test in VB
This is relatively complicated
first, create a "Standard EXE" project in VB. Add reference: Microsoft soap Type Library. NOTE: If Microsoft soap toolkit is not installed, this type of Library does not exist.
it can be downloaded in the 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 ")
This sentence can also be used.
Objsoapclient. mssoapinit ("http: // localhost/webserver/service1.asmx? WSDL ")

Add = objsoapclient. Show ("tornado. Net ")
End Function

Notes for successful debugging:
Run the WebService program on the server.
The following operations are supported. For formal definitions, see service description.
Click service description to obtain the complete WSDL file.
Http: // localhost/webserver/service1.asmx? WSDL
We need to use this file, including the methods we have defined.

mssoapinit (bstrwsdlfile as string, [bstrservicename as string], [bstrport as string], [bstrwsmldile as string]) usage:
second, the third parameter can be found in the WSDL file. It can also be omitted

Related Article

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.