开发者

Adding IoC Support to my WCF service hosted in a windows service (Autofac)

开发者 https://www.devze.com 2022-12-29 18:06 出处:网络
I\'d like to setup my WCF services to use an IoC Container. There\'s an article in the Autofac wiki about WCF integration, but it\'s showing just an integration with a service hosted in IIS.

I'd like to setup my WCF services to use an IoC Container. There's an article in the Autofac wiki about WCF integration, but it's showing just an integration with a service hosted in IIS.

But my services are hosted in a windows service.

Here I got an advice to hook up the opening event http://groups.google.com/group/autofac/browse_thread/thread/23eb7ff07d8bfa03

I've followed the advice and this is what I got so far:

    private void RunService<T>()
    {
        var builder = new ContainerBuilder();

        builder.Register(c => new DataAccessAdapter("1")).As<IDataAccessAdapter>();

        ServiceHost serviceHost = new ServiceHost(typeof(T));

        serviceHost.Opening += (sender, args) => serviceHost.Description.Behaviors.Add(
            new AutofacDependencyInjectionServiceBehavio开发者_如何学JAVAr(builder.Build(), typeof(T), ??? ));                      


        serviceHost.Open();
     }

The AutofacDependencyInjectionServiceBehavior has a ctor which takes 3 parameters. The third one is of type IComponentRegistration and I have no idea where can I get it from. Any ideas ?

Thanks in advance.


I have written a blog post that describes how to use the Autofac WCF Integration when Self-Hosting WCF Services.

http://alexmg.com/self-hosting-wcf-services-with-the-autofac-wcf-integration/

That should be enough to point you in the right direction. I am going to update the documentation on the Autofac wiki to include the same example.


There are some changes in Autofac since the Alex Meyer response. It's basically one line of code:

//Instead of
host.Description.Behaviors.Add(new AutofacDependencyInjectionServiceBehavior(container, typeof(EchoService), registration));
//Use this
host.AddDependencyInjectionBehavior<IEchoService>(container);

Source: https://autofaccn.readthedocs.io/en/latest/integration/wcf.html#self-hosting

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号