使用Unity依赖注入与WCF服务 [英] Using Unity Dependency Injection with WCF services
问题描述
MyServiceHost
$ b $ public class MyServiceHost:ServiceHost
{
public MyServiceHost(IUnityContainer container,Type serviceType,params Uri [] baseAddresses)
:base(serviceType ,baseAddresses)
{
if(container == null)
{
throw new ArgumentNullException(container);
}
foreach(在此.ImplementedContracts.Values中的var cd)
{
cd.Behaviors.Add(new DependencyInjectionInstanceProvider(container));
}
}
}
DependencyInjectionInstanceProvider: / strong>
public class DependencyInjectionInstanceProvider:IInstanceProvider,IContractBehavior
{
private readonly IUnityContainer container;
public DependencyInjectionInstanceProvider(IUnityContainer container)
{
if(container == null)
{
throw new ArgumentNullException(container);
}
this.container = container;
}
#region IInstanceProvider成员
公共对象GetInstance(InstanceContext instanceContext,消息消息)
{
返回此.GetInstance(的InstanceContext);
}
public object GetInstance(InstanceContext instanceContext)
{
var serviceType = instanceContext.Host.Description.ServiceType;
return this.container.Resolve(serviceType);
}
public void ReleaseInstance(InstanceContext instanceContext,object instance)
{
this.container.Teardown(instance);
}
#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
}
MyServiceHostFactory:
public class MyServiceHostFactory:ServiceHostFactory
{
private readonly IUnityContainer container;
protected override ServiceHost CreateServiceHost(type serviceType,Uri [] baseAddresses)
{
返回新的MyServiceHost(this.container,serviceType,baseAddresses);
}
}
尝试构造函数注入的电子邮件服务:
public class EmailValidator:IEmailValidator
{
private IFakeDAL fakeDAL;
public EmailValidator(IFakeDAL fakeDAL)
{
this.fakeDAL = fakeDAL;
}
public bool ValidateAddress(string emailAddress)
{
Console.WriteLine(Validating:{0},emailAddress);
string pattern = @^([0-9a-zA-Z]([ - 。\w] * [0-9a-zA-Z])* @(([ 9A-ZA-Z])+([ - \w] * [0-9A-ZA-Z])* \)+ [A-ZA-Z] {2,9})$;
return Regex.IsMatch(emailAddress,pattern);
}
}
我的控制台主机启动服务:
static void Main(string [] args)
{
键入serviceType = typeof (为EmailValidator);
Uri serviceUri = new Uri(http:// localhost:8080 /);
MyServiceHostFactory shf = new MyServiceHostFactory();
ServiceHost host = shf.CreateServiceHost(serviceType,serviceUri);
// ServiceHost host = new ServiceHost(serviceType,serviceUri);
host.Open();
我的问题在于控制台主机逻辑。由于第一个参数期望构造器字符串而不是类型,因此CreateServiceHost调用具有语法错误。我不明白,因为它接受一个Type参数。除此之外,我不明白我应该将IFakeDAL映射到一个具体的类。我可以在app.config文件中执行,还是应该在其他地方注册?
ServiceHostFactory
用于在IIS中托管。在自我托管中,您应直接使用您的派生的 ServiceHost
。 这里你有整个例子,包括Unity配置。
I have the following after doing some research on other questions:
MyServiceHost:
public class MyServiceHost : ServiceHost
{
public MyServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
foreach (var cd in this.ImplementedContracts.Values)
{
cd.Behaviors.Add(new DependencyInjectionInstanceProvider(container));
}
}
}
DependencyInjectionInstanceProvider:
public class DependencyInjectionInstanceProvider : IInstanceProvider, IContractBehavior
{
private readonly IUnityContainer container;
public DependencyInjectionInstanceProvider(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
#region IInstanceProvider Members
public object GetInstance(InstanceContext instanceContext, Message message)
{
return this.GetInstance(instanceContext);
}
public object GetInstance(InstanceContext instanceContext)
{
var serviceType = instanceContext.Host.Description.ServiceType;
return this.container.Resolve(serviceType);
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
this.container.Teardown(instance);
}
#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
}
MyServiceHostFactory:
public class MyServiceHostFactory : ServiceHostFactory
{
private readonly IUnityContainer container;
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new MyServiceHost(this.container, serviceType, baseAddresses);
}
}
Email Service with an attempted Constructor Injection:
public class EmailValidator : IEmailValidator
{
private IFakeDAL fakeDAL;
public EmailValidator(IFakeDAL fakeDAL)
{
this.fakeDAL = fakeDAL;
}
public bool ValidateAddress(string emailAddress)
{
Console.WriteLine("Validating: {0}", emailAddress);
string pattern = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$";
return Regex.IsMatch(emailAddress, pattern);
}
}
My Console Host to start the Service:
static void Main(string[] args)
{
Type serviceType = typeof(EmailValidator);
Uri serviceUri = new Uri("http://localhost:8080/");
MyServiceHostFactory shf = new MyServiceHostFactory();
ServiceHost host = shf.CreateServiceHost(serviceType, serviceUri);
//ServiceHost host = new ServiceHost(serviceType, serviceUri);
host.Open();
My problem resides in the console host logic. The CreateServiceHost call has a syntax error due to the first argument expecting a Constructor string and not a Type. Which I don't understand since it does accept a Type parameter. In addition to that I don't understand where I should be mapping IFakeDAL to a concrete class. Can I do that in an app.config file or should I register that somewhere else?
ServiceHostFactory
is for hosting in IIS. In self hosting you should use your derived ServiceHost
directly. Here you have whole example including Unity configuration.
这篇关于使用Unity依赖注入与WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!