1

WCF サービスで依存性注入に Unity を使用する方法を探していました。私はこれら 2 つのブログで説明されているコードを理解しようとしていますが、これは非常によく似ています。

そこで、このコードをソリューションの別のプロジェクトに追加し、SVC ファイルのカスタム servicehostfactory を WFC サービス (別のプロジェクト) に参照させました。

問題は、WCF サービス メソッドからコンテナー内のオブジェクトにアクセスするにはどうすればよいかということです。


編集

これらは私の実装です:

ServiceHostFactory...

namespace UnityWcfAssembler
{
public class UnityServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        UnityServiceHost serviceHost = new UnityServiceHost(serviceType, baseAddresses);
        UnityContainer container = new UnityContainer();
        serviceHost.Container = container;

        //TODO configuration from app.config 
        //configure container
        //UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        //section.Configure(serviceHost.Container);

        InitializeSessionFactories(container);

        return serviceHost;
    }

    private static void InitializeSessionFactories(UnityContainer container)
    {
        Dictionary<String, ISessionFactory> sessions = new Dictionary<string, ISessionFactory>();

        Configuration Cfg = new Configuration();
        Cfg.Configure();
        Cfg.SetProperty("connection.connection_string",
            "Data Source=(Local);Initial Catalog=Fossils;Integrated Security=true;");
        ISessionFactory Factory = Cfg.BuildSessionFactory();
        sessions.Add("fossils", Factory);

        Cfg.SetProperty("connection.connection_string",
            "Data Source=(Local);Initial Catalog=TypeCollection;Integrated Security=true;");
        ISessionFactory typeFactory = Cfg.BuildSessionFactory();

        sessions.Add("type", typeFactory);

        Cfg.SetProperty("connection.connection_string",
            "Data Source=(Local);Initial Catalog=PersonalCollection;Integrated Security=true;");
        ISessionFactory persFactory = Cfg.BuildSessionFactory();

        sessions.Add("personal", persFactory);

        container.RegisterInstance(sessions);
    }
}
}

サービスホスト...

namespace UnityWcfAssembler
{
    public class UnityServiceHost : ServiceHost
    {
        public UnityContainer Container { get; set; }

        public UnityServiceHost()
        {
            Container = new UnityContainer();
        }

        public UnityServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses)
        {
            Container = new UnityContainer();
        }

        protected override void OnOpening()
        {
            new UnityServiceBehavior(Container).AddToHost(this);

            base.OnOpening();

            if (Description.Behaviors.Find<UnityServiceBehavior>() == null)
                Description.Behaviors.Add(new UnityServiceBehavior(Container));
        }
    }
}

インスタンスプロバイダー...

namespace UnityWcfAssembler
{
    public class UnityInstanceProvider : IInstanceProvider
    {
        public UnityContainer Container { set; get; }
        public Type ServiceType { set; get; }

        public UnityInstanceProvider() : this(null)
        {
        }

        public UnityInstanceProvider(Type type)
        {
            ServiceType = type;
            Container = new UnityContainer();
        }

        // Get Service instace via unity container
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return Container.Resolve(ServiceType);
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            return GetInstance(instanceContext, null);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }
    }
}

IServiceBehavior...

namespace UnityWcfAssembler
{
    public class UnityServiceBehavior : IServiceBehavior
    {
        public UnityInstanceProvider InstanceProvider { get; set; }
        private ServiceHost serviceHost;

        public UnityServiceBehavior()
        {
            InstanceProvider = new UnityInstanceProvider();
        }

        public UnityServiceBehavior(UnityContainer unity)
        {
            InstanceProvider = new UnityInstanceProvider();
            InstanceProvider.Container = unity;
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
            {
                ChannelDispatcher cd = cdb as ChannelDispatcher;
                if (cd != null)
                {
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                    {
                        InstanceProvider.ServiceType = serviceDescription.ServiceType;
                        ed.DispatchRuntime.InstanceProvider = InstanceProvider;
                    }
                }
            }
        }

        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }

        public void AddToHost(ServiceHost host)
        {
            // only add to host once
            if (serviceHost != null) return;
            host.Description.Behaviors.Add(this);
            serviceHost = host;
        }
    }
}

Wcf サービス...

namespace FossilsWcfService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
    public class FossilsService : IFossilsService
    {
        private readonly Dictionary<string, ISessionFactory> sessionFactories;

        public FossilsService(Dictionary<string, ISessionFactory> s)
        {
            sessionFactories = s;
        }

        public SpeciesList GetAllSpecies()
        {
            SpeciesList list = new SpeciesList();

            ISessionFactory factory = sessionFactories["fossils"];

            if(factory == null)
            {
                list.Species.Add(new FossilSpecies { GenusName = "Session factory could not be resolved from container!" });
                return list;                
            }

            ISession session = factory.OpenSession();

            SpeciesManager speciesManager = new SpeciesManager(session);

            IList<FossilSpecies> species = speciesManager.GetAllSpecies();

            foreach (FossilSpecies fossilSpecies in species)
            {
                list.Species.Add(fossilSpecies);
            }
            return list;
        }

FossilsWcfService.svc...

<%@ ServiceHost Language="C#" Debug="true" 
Service="Server.Services.ExampleService"
Factory="UnityWcfAssembler.UnityServiceHostFactory" %>

後者にはおそらく別のファイル名が必要ですか?

4

1 に答える 1

3

ファクトリが機能するようになったので、インターフェイスを使用してプロパティを作成し、コンストラクターを宣言する必要があります。残りは Unity が行います。

public class InvoiceService : IInvoiceService {

    private IPayService payService;

    public IPayService PayService
    {
        get { return payService; }
        set { payService= value; }
    }


    public InvoiceService(IPayService provider)
    {
        this.payService= provider;
    }

    public bool Pay(){
         return PayService.Pay();
    }

}

My Service Factory の実装

public class InvoiceFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(
                                          Type serviceType, Uri[] baseAddresses)
        {
            UnityServiceHost host = new UnityServiceHost(serviceType, baseAddresses);
            UnityContainer unity = new UnityContainer();
            host.Container = unity;

//I'm doing it like this because I put some AOP in the service injected
            var clazz = Intercept.ThroughProxy<IPayService>(new PayServiceConcreteClass(), 
                                            new InterfaceInterceptor(), new[] { new LoggingInjection() });

            unity.RegisterType<IPayService>().RegisterInstance(clazz);

            return host;
        }
    }

質問のコードの後に​​編集

何が間違っているのかよくわかりませんが、よくわからないことが 2 つあります。

ServiceHostFactory...

//Better as an Interface
IDictionary<String, ISessionFactory> sessions = new Dictionary<string, ISessionFactory>();


//container.RegisterInstance(sessions);
//Registering the type not the class
container.RegisterType<IDictionary<String, ISessionFactory>>().RegisterInstance(sessions);

Wcf サービス...

private readonly IDictionary<string, ISessionFactory> sessionFactories;

        public FossilsService(IDictionary<string, ISessionFactory> s)
        {
            sessionFactories = s;
        }

FossilsWcfService.svc

codebehind 属性が欠落していませんか? このようなもの

<%@ ServiceHost Language="C#" Debug="true" Factory="InvoiceFactory" Service="InvoiceService" CodeBehind="InvoiceService.svc.cs" %>

...

于 2011-08-19T09:23:37.613 に答える