如何将值传递给我的wcf服务的构造函数? [英] How do I pass values to the constructor on my wcf service?
问题描述
我想将值传递给实现我的服务的类的构造函数。
但是,ServiceHost只允许我传递类型的名称来创建,而不是传递给它的构造函数的参数。
$ b $我想通过一个创建我的服务对象的工厂。
到目前为止我已经发现:
- WCF依赖注入行为,这超出了我正在寻找的,似乎对我的需求来说过于复杂。
您需要实现定制 ServiceHostFactory
, ServiceHost
和 IInstanceProvider
。
给定具有此构造函数签名的服务:
public MyService(IDependency dep)
这是一个可以启动MyService的例子:
public c lass MyServiceHostFactory:ServiceHostFactory
{
private readonly IDependency dep;
public MyServiceHostFactory()
{
this.dep = new MyClass();
}
保护覆盖ServiceHost CreateServiceHost(键入serviceType,
Uri [] baseAddresses)
{
返回新的MyServiceHost(this.dep,serviceType,baseAddresses );
}
}
public class MyServiceHost:ServiceHost
{
public MyServiceHost(IDependency dep,Type serviceType,params Uri [] baseAddresses)
:base(serviceType,baseAddresses)
{
if(dep == null)
{
throw new ArgumentNullException(dep);
}
foreach(在此.ImplementedContracts.Values中的var cd)
{
cd.Behaviors.Add(new MyInstanceProvider(dep));
}
}
}
public class MyInstanceProvider:IInstanceProvider,IContractBehavior
{
private readonly IDependency dep;
public MyInstanceProvider(IDependency dep)
{
if(dep == null)
{
throw new ArgumentNullException(dep);
}
this.dep = dep;
#region IInstanceProvider成员
公共对象GetInstance(InstanceContext instanceContext,消息消息)
{
返回this.GetInstance(instanceContext );
}
public object GetInstance(InstanceContext instanceContext)
{
返回新的MyService(this.dep);
}
public void ReleaseInstance(InstanceContext instanceContext,object instance)
{
var alternatives = instance as IDisposable;
如果(一次性!= null)
{
一次性.Dispose();
}
}
#endregion
#region IContractBehavior会员
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验证(ContractDescription contractDescription,ServiceEndpoint endpoint)
{
}
#endregion
} $ b在您的MyService.svc文件中注册MyServiceHostFactory,或直接在代码中使用MyServiceHost进行自我托管方案。
p>
您可以轻松地概括这种方法,实际上一些DI容器已经为您完成了此操作(提示:Windsor的WCF设施)。
I would like to pass values into the constructor on the class that implements my service.
However ServiceHost only lets me pass in the name of the type to create, not what arguments to pass to its contrstructor.
I would like to be able to pass in a factory that creates my service object.
What I have found so far:
- WCF Dependency Injection Behavior which is more than what I am looking for and seems to be over-complex for my needs.
You'll need to implement a combination of custom ServiceHostFactory
, ServiceHost
and IInstanceProvider
.
Given a service with this constructor signature:
public MyService(IDependency dep)
Here's an example that can spin up MyService:
public class MyServiceHostFactory : ServiceHostFactory
{
private readonly IDependency dep;
public MyServiceHostFactory()
{
this.dep = new MyClass();
}
protected override ServiceHost CreateServiceHost(Type serviceType,
Uri[] baseAddresses)
{
return new MyServiceHost(this.dep, serviceType, baseAddresses);
}
}
public class MyServiceHost : ServiceHost
{
public MyServiceHost(IDependency dep, Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
if (dep == null)
{
throw new ArgumentNullException("dep");
}
foreach (var cd in this.ImplementedContracts.Values)
{
cd.Behaviors.Add(new MyInstanceProvider(dep));
}
}
}
public class MyInstanceProvider : IInstanceProvider, IContractBehavior
{
private readonly IDependency dep;
public MyInstanceProvider(IDependency dep)
{
if (dep == null)
{
throw new ArgumentNullException("dep");
}
this.dep = dep;
}
#region IInstanceProvider Members
public object GetInstance(InstanceContext instanceContext, Message message)
{
return this.GetInstance(instanceContext);
}
public object GetInstance(InstanceContext instanceContext)
{
return new MyService(this.dep);
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
var disposable = instance as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
#endregion
#region IContractBehavior Members
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)
{
}
#endregion
}
Register MyServiceHostFactory in your MyService.svc file, or use MyServiceHost directly in code for self-hosting scenarios.
You can easily generalize this approach, and in fact some DI Containers have already done this for you (cue: Windsor's WCF Facility).
这篇关于如何将值传递给我的wcf服务的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!