WCF服务连接的Autofac问题

时间:2010-11-29 10:08:17

标签: wcf autofac

我很抱歉用我的小问题打扰了社区,但我只是被困住了!

在此之前我们详细介绍了服务模块的容器设置!

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()).Named<IShieldConfiguration>("SRV_SHIELD_CONFIG");

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

我一直在讨论的问题是服务构造函数的第二个参数不能解决

我已经尝试了所有,我所知道的排列,包括在没有容器分辨率的情况下简单地初始化参数。但所有目的都在同一个例外:

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)'.

我必须错过这里至关重要的事情。如果你看到我的错误,那么请告诉我:)

1 个答案:

答案 0 :(得分:2)

发现问题。这是我忽略的一件小事。

Autofac仅采用类型的最后一个定义。因为我重新注册了最后一个定义的类型。

这只是问题的一部分。另一部分(生成有趣的异常消息的部分)是RegisterType()尝试自动装配该类型的事实。并且因为除了异常屏蔽之外,所有对象都可以通过它们的类型找到。

工作配置如下所示:

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");
        }
    }

容易犯错,花了我几个小时才弄明白。希望这能帮助其他失去灵魂的人。

相关问题