我有一个以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
实例的服务中。关于如何做到这一点的任何想法?