2013-08-26 263 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:你想在這個端點上公開的那個.... –