Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将WCF服务作为Windows服务托管_Wcf_Windows Services_Hosting_Svc - Fatal编程技术网

将WCF服务作为Windows服务托管

将WCF服务作为Windows服务托管,wcf,windows-services,hosting,svc,Wcf,Windows Services,Hosting,Svc,我已经创建了WCF服务项目。 它在SVC文件中有以下内容 <%@ ServiceHost Service="Deepak.BusinessServices.Implementation.ApiImplementation" Factory="Deepak.BusinessServices.Implementation.CustomServiceHostFactory"%> 服务启动并生成WSDL。现在我想将此服务作为Windows服务宿主。 我怎么做 我已经创建

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

    <%@ ServiceHost Service="Deepak.BusinessServices.Implementation.ApiImplementation"
      Factory="Deepak.BusinessServices.Implementation.CustomServiceHostFactory"%>
服务启动并生成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();


    }

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

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

namespace Deepak.BusinessServices.Implementation
{ 
    public class ApiImplementation : IApiImplementation
    {
       ....
    }
 }
那么你需要:

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

有很多服务合同,我需要选择哪一个!
m_Host = new ServiceHost(typeof(ApiImplementation), httpUrl);
m_Host.AddServiceEndpoint(typeof(IApiImplementation), new WSHttpBinding(), "");