WSe enables ASP. NET web service to host on the consoleProgramIn Windows Service, COM + and Windows Forms applications, it can call network methods using TCP or any other accepted transmission protocol.
The following describes how to create a web service that is not hosted in IIS and the source code of the sample project. If you are interested, take the test.
The example project uses Windows service as the host Program of the network service. The following describes how to create the network service and Host Program.
-
- Open vs2005
-
- Create a Windows Service
- Click the File menu and select a new project
- In the project template of the new project, select Windows Service
- Enter the project name, for example, windowsservicetohostasmxwebservice.
- Click OK
-
- Add Microsoft. Web. services3, system. Web. Services, and system. web references.
- Click Add reference in the project menu.
- Select the. NET tab, select Microsoft. Web. services3, system. Web. Services, and system. Web, and click OK.
-
- Add the configuration file app. config
- Add a file named helloservice. CS to the project, Code As follows:Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Web. Services;
Namespace Windowsservicetohostasmxwebservice
{
[WebService (namespace = " Http://tempuri.org/ " )]
[Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
Public Class Helloservice: system. Web. Services. WebService
{
Public Helloservice ()
{
//If you use the designed components, uncomment the following lines:
//Initializecomponent ();
}
[Webmethod]
Public String Helloworld ()
{
Return "Hello World";
}
}
}
- Change the service. CS code in the project as follows: Code
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. diagnostics;
Using System. serviceprocess;
Using System. text;
Using Microsoft. Web. services3;
Using Microsoft. Web. services3.addressing;
Using Microsoft. Web. services3.messaging;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Namespace Windowsservicetohostasmxwebservice
{
Public Partial Class Service1: servicebase
{
Public Service1 ()
{
Initializecomponent ();
}
Protected Override Void Onstart ( String [] ARGs)
{
// Todo: Add code here to start the service.
Uri address = New Uri ( " Soap. TCP: // localhost/helloservice " );
Soapreceivers. Add ( New Endpointreference (address ), Typeof (Helloservice ));
}
Protected Override Void Onstop ()
{
//Todo: Add code here to stop the service.
Soapreceivers. Clear ();
}
}
}
-
- Add projectinstaller required for Windows Service Installation
-
- In this way, you can use the web service host program after the service is installed and started (how to install and start it is not described in detail, but you can query it online. The following describes how to create a proxy for such a service and how to access such a service on the client.
- to create a producer service proxy, I have not found a good solution. wsdl.exe can create a proxy class. This is. net Framework provides the default tool, but because it is not released on the Web server, it is impossible to directly generate a proxy class for the helloservice in the project. I am using a stupid method, I first created an ASP.. net service project, and then create a code and helloservice. CS-like network service, and then publish it to the HTTP server, then the WSDL can access http: // localhost/Host-in-winservice/service. asmx? WSDL
in this example, we use wsdl.exe to generate the default proxy. cs. The operation is as follows:
- open vs2005 command prompt line
- Cd to a specified directory, such as D :\
- Use wsdl.exe to generate the default proxy file proxy. CS
- in this way, a proxy. CS file can be obtained on disk D, which is the default proxy class. The client program is created below
- Create a client program. This article is the console program, and then add Microsoft. web. services3,
system. web. services, and system. web reference will get the proxy class proxy. copy CS to the project.
the following are the most important steps: in the previous translation documents, mentioned that the Proxy Base class for network services using WSE 3.0 must be changed to webservicesclientprotocol , (see WSE 3.0 document translation : WSe architecture )
Therefore, you must first modify the base class of the original proxy class as shown in:
Original proxy class
New proxy
In addition, the constructor is modified as follows due to the address change of the network service.
Original proxy class
New proxy class:
- After modifying the proxy, modify the code in program. CS as follows:
- Run the generated client.exe in the test run mode. The result is as shown in, indicating that the client is successfully run:
Sample project file: Sample project file
Method Improvement for generating proxy classes
when writing this Article , no good method was found to generate a proxy class for the network service published in non-IIS, so step 9 is troublesome. Tool used by the producer: wsewsdl3.exe, which can easily generate proxy classes. The steps are as follows:
wsewsdl3.exe is in the installation directory/Microsoft WSE \ V3.0 \ tools, after cmd-CD is added to this directory, you can obtain the proxy file in the same directory of the tool, as shown in. This file can not be modified at all, ease of use