我有一个以这种方式实现的自定义ServiceBehavior
,作为装饰WCF服务的属性:
public sealed class NHibernateSessionServiceBehavior : Attribute, IServiceBehavior
{
public Type ServiceType { get; set; }
public IConfigurator Configurator { get; set; }
public NHibernateSessionServiceBehavior(Type serviceType, IConfigurator configurator)
{
ServiceType = serviceType;
Configurator = configurator;
}
#region Implementation of IServiceBehavior
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
NHibernateSessionHelper.Instance.InitializeSessionFactory(Configurator);
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatch in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher endpointDispatch in channelDispatch.Endpoints)
{
endpointDispatch.DispatchRuntime.MessageInspectors.Add(new NHibernateMessageInspector(ServiceType));
}
}
}
#endregion
}
我遇到的问题是我需要向属性构造函数传递IConfigurator
的实例,但编译器显然不允许我这样做。我需要一种方法将此行为添加到注入IConfigurator
实例的服务中。
关于如何做到这一点的任何想法?
答案 0 :(得分:1)