具有自定义构造函数的ServiceBehavior。需要注入依赖项

时间:2013-04-24 19:08:22

标签: c# wcf dependency-injection

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