Programmatically starting an HTTP server in C#?

前端 未结 4 965
不思量自难忘°
不思量自难忘° 2021-02-01 11:20

I\'m a C#, ASP.NET newbie.

Let\'s say I have an existing C# project (from a \"Console Application\" template; I work with Visual Studio). I want to be able to start a si

4条回答
  •  轮回少年
    2021-02-01 11:48

    Here is a good article on this: http://msdn.microsoft.com/en-us/library/aa529311.aspx

    The key is the usage of the Microsoft.Web.Services3

    Sample code copied from the linked article:

    public partial class WindowsServiceToHostASMXWebService  : ServiceBase
    {
        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();
        }
    }
    

    and calling it:

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

提交回复
热议问题