哦,亲爱的,我在问题中发布了我的答案。
事实:
- Unity.Wcf是第三方库,由于缺少所有者,目前不支持。
- Unity 文档已经展示了一种创建自己的
UnityServiceHost
用于 DI的方法。
- 执行前面提到的文档中的确切示例并替换
Unity.Wcf
为参考实现将解决问题并使服务器消耗队列。
解决方案相当简单。
为了节省您一些时间,这里是来自 Microsoft 的Unity 文档的示例代码:
UnityServiceHost:
public class UnityServiceHost : ServiceHost
{
public UnityServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
foreach (var contractDescription in ImplementedContracts.Values)
{
contractDescription.Behaviors.Add(new UnityInstanceProvider(container));
}
}
}
UnityInstanceProvider
public class UnityInstanceProvider : IInstanceProvider, IContractBehavior
{
private readonly IUnityContainer container;
public UnityInstanceProvider(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetInstance(InstanceContext instanceContext, Message message)
{
return GetInstance(instanceContext);
}
public object GetInstance(InstanceContext instanceContext)
{
return container.Resolve(instanceContext.Host.Description.ServiceType);
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
}
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.InstanceProvider = this;
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
}