다음을 통해 공유


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

  1. Open Visual Studio 2005.

  2. Create an ASP.NET Web service.

    1. On the File menu, choose New Web Site.
    2. In the New Web Site dialog box, select the ASP.NET Web Service icon.
    3. Enter the address of the Web server on which you will develop the XML Web service and directory name. For this procedure specify https://localhost/WSEHostedWebService.
    4. Click OK to create the project.
  3. 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.

  4. Create a Windows service.

    1. On the File menu, choose New Project.
    2. In the New Project dialog box, select the Windows Service icon.
    3. Enter the name of the Windows service. For this procedure, specify WindowsServiceToHostASMXWebService.
    4. Click OK to create the project.
      This creates a Windows service project and closes the Web service project.
  5. Add references to the Microsoft.Web.Services3, System.Web.Services, and System.Web assemblies.

    1. On the Project menu, click Add Reference.
    2. Click the .NET tab, select Microsoft.Web.Services3.dll, System.Web.dll, and System.Web.Services.dll, and then click OK.
  6. Add the source file and configuration files for the Web service.

    1. When the Web service has a configuration file, in Solution Explorer, right-click the project name, point to Add, and choose Existing Item.
    2. 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.
    3. In Solution Explorer, right-click the project name, point to Add, and choose Existing Item.
    4. 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.
  7. When the Web service has a configuration file, rename the web.config file to app.config.

    1. In Solution Explorer, right-click web.config, and choose Rename Item.
    2. In the edit box, type app.config.
  8. Open the source for the Windows service.

    1. In Solution Explorer, right-click the file containing the Windows service, and then click View Code.
  9. 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;
    
  10. 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
    {
    
  11. 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 ));
    }
    
  12. 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();
    }
    
  13. 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);
    }
    
  14. Specify that the startup object for the project is the Windows service.

    1. In Solution Explorer, right-click the project name, and choose Properties.
    2. In Startup object, select WindowsServiceToHostASMXWebService.
  15. 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.

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

End Class

    
    
    ...
    
    
    Imports System.ServiceProcess
Imports Microsoft.Web.Services3
Imports Microsoft.Web.Services3.Addressing
Imports Microsoft.Web.Services3.Messaging

Partial Public Class WindowsServiceToHostASMXWebService
    Inherits ServiceBase

    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 Overrides Sub OnStop()
        ' Stop hosting the ASP.NET Web service.
        SoapReceivers.Clear()
    End Sub

End Class
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);
        }

    }
}