2013-08-26 265 views
0

我创建了WCF服务项目。 它在SVC文件中有以下内容。作为Windows服务托管WCF服务

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

SVC参考

http://localhost/DeepakGateway/Service.svc 

服务是UP和产生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(); 


    } 

回答

0

您需要添加一个实现在ServiceHost构造服务合同之类的合同的服务的类型,并输入您的AddServiceEndpoint

假设你服务实现类看起来是像这样:

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

,那么你需要:

m_Host = new ServiceHost(typeof(ApiImplementation), httpUrl); 
m_Host.AddServiceEndpoint(typeof(IApiImplementation), new WSHttpBinding(), ""); 
  • 需要知道什么(具体的)类服务类的服务主机到主机
  • 端点需要知道什么服务合同(接口),它暴露
+0

有很多服务合同,我需要选择哪一个! –

+0

@IsharehappyK:你想在这个端点上公开的那个.... –