开发者

Autofac problem with WCF service wire-up

开发者 https://www.devze.com 2023-01-27 04:35 出处:网络
I am sorry to bother the community with my little problem, but I am just stuck! Before we get into details here is my container setup for the service module!

I am sorry to bother the community with my little problem, but I am just stuck!

Before we get into details here is my container setup for the service module!

public class ServiceModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.Register(c => new ContextService(c.Resolve<IContextDataProvider>(),
                                                     c.ResolveNamed<IExceptionShield>("SRV_HOST_SHIELD"),
                                                     c.Resolve<IMonitoring>()))
                .As<IContextService>();

            builder.Register(c => new ExceptionShield(
                c.ResolveNamed<IShieldConfiguration>("SRV_SHIELD_CONFIG")))
                .Named<IExceptionShield>("SRV_HOST_SHIELD");

            builder.Register(c => new ServiceExceptionShieldConfiguration()开发者_如何学C).Named<IShieldConfiguration>("SRV_SHIELD_CONFIG");

            builder.RegisterType<ContextService>().Named<object>("Service.ContextService");
        }
    }

The problem that I am consistently hawing is that the second parameter of the service constructor can not be resolved.

I have tried all ,to me known, permutations including just plainly initializing the parameter without container resolution. But all ends in the same exception:

None of the constructors found with 'Public binding flags' on type 'Service.ContextService' can be invoked with the available services and parameters:
Cannot resolve parameter 'Common.ExceptionShield.IExceptionShield exceptionShield' of constructor 'Void .ctor(IContextDataProvider, Common.ExceptionShield.IExceptionShield, Common.Monitoring.IMonitoring)'.

I must be missing something crucial here. If you see my error then please do tell me :)


Found the problem. It is a small thing that I overlooked.

Autofac only takes the last definition of a type. And because I reRegisterd the type it took the last definition.

This was only part of the problem. The other part (the one generating the funny exception message) was the fact that the RegisterType() tries to autowire the type. And because all the objects could be found by their type except the exception shield, which was named.

The working configuration looks like the following:

public class ServiceModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.Register(c => new ContextService(c.Resolve<IContextDataProvider>(),
                                                     c.ResolveNamed<IExceptionShield>("SRV_HOST_SHIELD"),
                                                     c.Resolve<IMonitoring>()))
                .Named<object>("Service.ContextService");

            builder.Register(c => new ExceptionShield(
                c.ResolveNamed<IShieldConfiguration>("SRV_SHIELD_CONFIG")))
                .Named<IExceptionShield>("SRV_HOST_SHIELD");

            builder.Register(c => new ServiceExceptionShieldConfiguration()).Named<IShieldConfiguration>("SRV_SHIELD_CONFIG");
        }
    }

Easy mistake that took me some hours to figure out. Hope this helps some other lost soul out there.

0

精彩评论

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