WCF 服务连接的 Autofac 问题

发布于 2024-10-05 03:40:38 字数 1582 浏览 0 评论 0原文

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

在我们详细介绍之前,先介绍一下我的服务模块的容器设置!

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

我一定在这里遗漏了一些重要的东西。如果您看到我的错误,请告诉我:)

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()).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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2024-10-12 03:40:38

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

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

一个简单的错误,我花了几个小时才弄清楚。希望这能帮助其他一些迷失的灵魂。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文