How to: Host an ASP.NET Web Service Outside of IIS
The Web Services Enhancements for Microsoft .NET Framework (WSE) enables ASP.NET Web services to be hosted in console applications, Windows services, COM+ components or Windows Forms applications and then be called using the TCP protocol or any custom transport protocol written for WSE.
Host an ASP.NET Web service in a Windows service
-
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.
-
On the File menu, choose New Web Site.
-
Add code to implement the Web service.
By default there is a
HelloWorldWeb service method in a class namedService. The defaultServiceclass 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.
-
On the File menu, choose New 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.
-
On the Project menu, click Add Reference.
-
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, in Solution Explorer, right-click the project name, point to Add, and choose Existing Item.
-
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.
-
In Solution Explorer, right-click web.config, and choose Rename Item.
-
Open the source for the Windows service.
-
In Solution Explorer, right-click the file containing the Windows service, and then click View Code.
-
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.
-
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
WindowsServiceToHostASMXWebServiceand derives it from System.ServiceProcess.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
Serviceis to be hosted at thesoap.tcp://localhost/Serviceendpoint. -
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.
-
Edit the
Mainmethod to create an instance ofWindowsServiceToHostASMXWebService. When you renamed the service in step 10, the class name was not modified in theMainmethod.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>
- BufferResponse
-
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.
-
In Solution Explorer, right-click the project name, and choose Properties.
-
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.
Example
The following code example creates a Windows service named WindowsServiceToHostASMXWebService that hosts a Web service named Service at the soap.tcp://localhost/Service endpoint.
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service () { } [WebMethod] public string HelloWorld() { return "Hello World"; } } ... 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; namespace WindowsServiceToHostASMXWebService { public partial class WindowsServiceToHostASMXWebService : ServiceBase { public WindowsServiceToHostASMXWebService() { InitializeComponent(); } protected override void OnStart(string[] args) { Uri address = new Uri("soap.tcp://localhost/TestService"); SoapReceivers.Add(new EndpointReference(address), typeof(Service )); } protected override void OnStop() { SoapReceivers.Clear(); } 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); } } }