2012-09-05 190 views
1

我需要傳遞一個存在 DI容器作爲參數傳遞給WCF服務構造 爲了做到這一點我使用IInstanceProvider WCF服務應在自主機託管。參數傳遞給WCF服務構造

public class CustomInstanceProvider : IInstanceProvider, IContractBehavior 
{ 
    private readonly IUnityContainer UnityContainer; 

    public CustomInstanceProvider(IUnityContainer unityContainer) 
    { 
     if (unityContainer == null) 
     { 
      throw new ArgumentNullException("unityContainer"); 
     } 

     UnityContainer = unityContainer; 
    } 


    #region Implementation of IInstanceProvider 

    public object GetInstance(InstanceContext instanceContext) 
    { 
     return new Service(UnityContainer); 
    } 

    public object GetInstance(InstanceContext instanceContext, Message message) 
    { 
     return this.GetInstance(instanceContext); 
    } 
    public void ReleaseInstance(InstanceContext instanceContext, object instance) 
    { 

    } 

    #endregion 

    #region Implementation of IContractBehavior 

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint) 
    { 

    } 

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime) 
    { 
     dispatchRuntime.InstanceProvider = this; 

    } 

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
    { 

    } 


    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
    { 

    } 

    #endregion 
} 

我也實現了CustomServiceHost

public class CustomServiceHost : ServiceHost 
    { 
     public CustomServiceHost(IUnityContainer unityContainer, Type serviceType, params Uri[] baseAddresses) 
     : base(serviceType, baseAddresses) 
    { 
     if (unityContainer == null) 
     { 
      throw new ArgumentNullException("unityContainer is null"); 
     } 

     foreach (var cd in this.ImplementedContracts.Values) 
     { 
      cd.Behaviors.Add(new CustomInstanceProvider(unityContainer)); 
     } 

    } 
} 

和ServiceHostFactory

public class CustomServiceHostFactory : ServiceHostFactory 
{ 
    private readonly IUnityContainer UnityContainer; 
    public CustomServiceHostFactory(IUnityContainer unityContainer) 
    { 
     UnityContainer = unityContainer; 
    } 

    protected override ServiceHost CreateServiceHost(Type serviceType, 
     Uri[] baseAddresses) 
    { 
     return new CustomServiceHost(UnityContainer, serviceType, baseAddresses); 
    } 

}

我創建WCF服務:

  var uris = new Uri[1]; 
     uris[0] = new Uri("http://localhost:8793/Service/"); 
     CustomServiceHostFactory factory = new CustomServiceHostFactory(Container); 
     CustomServiceHost serviceHost = (CustomServiceHost)factory.CreateServiceHost("guy",uris); 
     ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), ""); 
     serviceHost.Open(); 

我得到一個異常:

An exception occurred while initializing module 'PluginsModule'. 

- The exception message was: 'ServiceHostFactory.CreateServiceHost' cannot be invoked within the current hosting environment. This API requires that the calling application be hosted in IIS or WAS. 

- The Assembly that the module was trying to be loaded from was:<"PluginsModule.plugin" , Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 

Check the InnerException property of the exception for more information. If the exception occurred while creating an object in a DI container, you can exception.GetRootException() to help locate the root cause of the problem. 

請幫

感謝。

+0

難道你不想調用CreateServiceHost類型作爲第一個參數而不是字符串嗎?這是你重寫的方法 – Yoztastic

+0

它是受保護的方法 – Guy

+0

是否返回新的CustomServiceHost(UnityContainer,serviceType,baseAddresses);'調用時調用'(CustomServiceHost)factory.CreateServiceHost(「guy」,uris);'? – Yoztastic

回答

6

好的, 問題是我不需要使用CustomInstanceProvider。 ServiecInstanceProvider僅供IIS使用。 我希望我的服務能夠在自主服務器中託管。

 var uris = new Uri[1]; 
     uris[0] = new Uri("http://localhost:8793//Service/ntService/"); 
     var serviceHost = new CustomServiceHost(Container,typeof(Service),uris); 
     serviceHost.AddDefaultEndpoints(); 

這就是所有我需要到: 實現IInstanceProvider & ServiceHost的。 現在我可以傳遞參數給我的構造函數。

謝謝