C# 在windows服务上托管ApicController

C# 在windows服务上托管ApicController,c#,.net,wcf,rest,service,C#,.net,Wcf,Rest,Service,我有一个带有控制器和服务的RESTAPI项目,它在IIS上运行良好 现在,我正试图找到一种方法,将其作为windows服务托管在非IIS计算机上。到目前为止没有运气,TopShelf似乎是我想要的,但我有多个控制器/服务要托管,而且它似乎一次只能处理一个 有没有办法让windows服务项目运行另一个引用的项目并托管它?不必每次都卸载服务,调试就更容易了,因此理想情况下,项目将保持独立。您可以在windows服务中托管WCF服务。为此,请创建一个windows服务项目并引用WCF项目。还要在win

我有一个带有控制器和服务的RESTAPI项目,它在IIS上运行良好

现在,我正试图找到一种方法,将其作为windows服务托管在非IIS计算机上。到目前为止没有运气,TopShelf似乎是我想要的,但我有多个控制器/服务要托管,而且它似乎一次只能处理一个


有没有办法让windows服务项目运行另一个引用的项目并托管它?不必每次都卸载服务,调试就更容易了,因此理想情况下,项目将保持独立。

您可以在windows服务中托管WCF服务。为此,请创建一个windows服务项目并引用WCF项目。还要在windows服务项目中添加System.ServiceModel和System.ServiceModel.Description DLL,然后编写如下函数

private ServiceHost host;

private void HostWcfService()
{
        //Create a URI to serve as the base address
         Uri httpUrl = newUri("http://localhost:1256/MyService/Service");

         //Create ServiceHost
         host = newServiceHost(typeof(MyService.IService), httpUrl);

         //Add a service endpoint
         host.AddServiceEndpoint(typeof(MyService.IService), newWSHttpBinding(), "");

         //Enable metadata exchange
         ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
         smb.HttpGetEnabled = true;
         host.Description.Behaviors.Add(smb);

         //Start the Service
         host.Open();

}
        protected override void OnStart(string[] args)
        {
            if (host != null)
            {
                host.Close();
            }
               HostWcfService();

        }
protected override void OnStop()
{
    if (host != null)
    {
        host.Close();
        host = null;
    }
}
并在OnStart方法中调用HostWcfService,如下所示

private ServiceHost host;

private void HostWcfService()
{
        //Create a URI to serve as the base address
         Uri httpUrl = newUri("http://localhost:1256/MyService/Service");

         //Create ServiceHost
         host = newServiceHost(typeof(MyService.IService), httpUrl);

         //Add a service endpoint
         host.AddServiceEndpoint(typeof(MyService.IService), newWSHttpBinding(), "");

         //Enable metadata exchange
         ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
         smb.HttpGetEnabled = true;
         host.Description.Behaviors.Add(smb);

         //Start the Service
         host.Open();

}
        protected override void OnStart(string[] args)
        {
            if (host != null)
            {
                host.Close();
            }
               HostWcfService();

        }
protected override void OnStop()
{
    if (host != null)
    {
        host.Close();
        host = null;
    }
}
并像这样更新OnStop方法

private ServiceHost host;

private void HostWcfService()
{
        //Create a URI to serve as the base address
         Uri httpUrl = newUri("http://localhost:1256/MyService/Service");

         //Create ServiceHost
         host = newServiceHost(typeof(MyService.IService), httpUrl);

         //Add a service endpoint
         host.AddServiceEndpoint(typeof(MyService.IService), newWSHttpBinding(), "");

         //Enable metadata exchange
         ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
         smb.HttpGetEnabled = true;
         host.Description.Behaviors.Add(smb);

         //Start the Service
         host.Open();

}
        protected override void OnStart(string[] args)
        {
            if (host != null)
            {
                host.Close();
            }
               HostWcfService();

        }
protected override void OnStop()
{
    if (host != null)
    {
        host.Close();
        host = null;
    }
}