-
Open Visual Studio 2005.
-
Create an ASP.NET Web service.
-
On the File menu, choose New Web Site.
-
In the New Web Site dialog box, select the ASP.NET Web Service icon.
-
Enter the address of the Web server on which you will develop the XML Web service and directory name. For this procedure specify
http://localhost/WSEHostedWebService.
-
Click OK to create the project.
-
Add code to implement the Web service.
By default there is a HelloWorld Web service method in a class named Service. The default Service class is used by this procedure.
-
Create a Windows service.
-
On the File menu, choose New Project.
-
In the New Project dialog box, select the Windows Service icon.
-
Enter the name of the Windows service. For this procedure, specify
WindowsServiceToHostASMXWebService.
-
Click OK to create the project.
This creates a Windows service project and closes the Web service project.
-
Add references to the Microsoft.Web.Services3, System.Web.Services, and System.Web assemblies.
-
On the Project menu, click Add Reference.
-
Click the .NET tab, select Microsoft.Web.Services3.dll, System.Web.dll, and System.Web.Services.dll, and then click OK.
-
Add the source file and configuration files for the Web service.
-
When the Web service has a configuration file, in Solution Explorer, right-click the project name, point to Add, and choose Existing Item.
-
When the Web service has a configuration file, navigate to the folder containing the Web service, change the Files of type to All Files (*.*), select Web.config, and click Add.
-
In Solution Explorer, right-click the project name, point to Add, and choose Existing Item.
-
Navigate to the App_Code child folder of the folder containing the Web service, select the source file (Service1.vb or Service1.cs), and click Add.
-
When the Web service has a configuration file, rename the web.config file to app.config.
-
In Solution Explorer, right-click web.config, and choose Rename Item.
-
In the edit box, type app.config.
-
Open the source for the Windows service.
-
In Solution Explorer, right-click the file containing the Windows service, and then click View Code.
-
Add using or Import directives to the file containing the Windows service.
Imports System.ServiceProcess
Imports Microsoft.Web.Services3
Imports Microsoft.Web.Services3.Addressing
Imports Microsoft.Web.Services3.Messaging
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;
-
When the class implementing the Windows service doesn't derive from ServiceBase, derive from ServiceBase and be a partial class.
The following code example changes the name of the class to WindowsServiceToHostASMXWebService and derives it from System.ServiceProcess.ServiceBase.
Partial Public Class WindowsServiceToHostASMXWebService
Inherits ServiceBase
public partial class WindowsServiceToHostASMXWebService : ServiceBase
{
-
In the OnStart method of the Windows service, add code to host the ASP.NET Web service.
To host the ASP.NET Web service, call the Add method and supply the endpoint and type for the Web service.
The following code example specifies that the Web service named Service is to be hosted at the soap.tcp://localhost/Service endpoint.
Protected Overrides Sub OnStart(ByVal args() As String)
' Start a listener for the ASP.NET Web service named Service.
Dim Address As New Uri("soap.tcp://localhost/Service")
SoapReceivers.Add(New EndpointReference(Address), GetType(Service))
End Sub
protected override void OnStart(string[] args)
{
Uri address = new Uri("soap.tcp://localhost/TestService");
SoapReceivers.Add(new EndpointReference(address), typeof(Service ));
}
-
In the OnStop method for the Windows service, add code to stop hosting the ASP.NET Web service.
To stop hosting the ASP.NET Web service, call the Clear method.
The following code example stops hosting of the ASP.NET Web service.
Protected Overrides Sub OnStop()
' Stop hosting the ASP.NET Web service.
SoapReceivers.Clear()
End Sub
protected override void OnStop()
{
SoapReceivers.Clear();
}
-
Edit the Main method to create an instance of WindowsServiceToHostASMXWebService. When you renamed the service in step 10, the class name was not modified in the Main method.
When a Web service is hosted outside of IIS, some of the programming elements that are specific to HTTP are not available. The System.Web.HttpContext.Current property is one example of this. The following paragraphs summarize the other elements that are not available.
The following properties of the System.Web.Services.WebMethodAttribute attribute cannot be used by a Web service that is hosted outside of IIS.
- BufferResponse
- CacheDuration
- EnableSession
- TransactionOption
The following configuration elements cannot be used by a Web service that is hosted outside of IIS.
- <serviceDescriptionFormatExtensionTypes>
- <soapExtensionTypes>
- <soapExtensionReflectorTypes>
- <soapExtensionImporterTypes>
- <wsdlHelpGenerator>
' The main entry point for the process
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New WindowsServiceToHostASMXWebService}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// Change the following line to match.
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WindowsServiceToHostASMXWebService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
-
Specify that the startup object for the project is the Windows service.
-
In Solution Explorer, right-click the project name, and choose Properties.
-
In Startup object, select
WindowsServiceToHostASMXWebService.
-
Install the Windows service.
Unlike most projects you create in Visual Studio, Windows Service projects cannot be run directly from the development environment by pressing F5. This is because the service in the project must be installed before the project can run. For details about installing the Windows service, see the How to: Install and Uninstall Services topic in the .NET Framework SDK documentation.