【问题标题】:Hosting WCF Service as Windows Service将 WCF 服务托管为 Windows 服务
【发布时间】:2013-08-26 04:07:29
【问题描述】:

我已经创建了 WCF 服务项目。 它在 SVC 文件中有以下内容。

    <%@ ServiceHost Service="Deepak.BusinessServices.Implementation.ApiImplementation"
      Factory="Deepak.BusinessServices.Implementation.CustomServiceHostFactory"%>

SVC 参考

    http://localhost/DeepakGateway/Service.svc

服务已启动并生成 WSDL。现在我想将此服务托管为 Windows 服务。 我该怎么做?

我已经创建了“Windows 服务”项目并且有以下代码。

protected override void OnStart(string[] args)
    {
        if (m_Host != null)
        {
            m_Host.Close();
        }
        Uri httpUrl = new Uri("http://localhost/DeepakGateway/Service.svc");

        m_Host = new ServiceHost
        (typeof(?????? WHAT TO FILL HERE?), httpUrl);
        //Add a service endpoint
        m_Host.AddServiceEndpoint
        (typeof(?????? WHAT TO FILL HERE?), ), new WSHttpBinding(), "");
        //Enable metadata exchange
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        m_Host.Description.Behaviors.Add(smb);
        //Start the Service
        m_Host.Open();


    }

【问题讨论】:

    标签: wcf windows-services hosting svc


    【解决方案1】:

    您需要在ServiceHost构造函数中添加实现您的服务契约的类的类型,并在您的AddServiceEndpoint中添加服务契约的类型

    假设您的服务实现类如下所示:

    namespace Deepak.BusinessServices.Implementation
    { 
        public class ApiImplementation : IApiImplementation
        {
           ....
        }
     }
    

    那么你需要:

    m_Host = new ServiceHost(typeof(ApiImplementation), httpUrl);
    m_Host.AddServiceEndpoint(typeof(IApiImplementation), new WSHttpBinding(), "");
    
    • 服务宿主需要知道要托管什么(具体)服务类类型
    • 端点需要知道它公开了什么服务契约(接口)

    【讨论】:

    • 服务合同有很多,我需要选择哪一个!
    猜你喜欢
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多