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)'.
我一定在这里遗漏了一些重要的东西。如果您看到我的错误,请告诉我:)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了。这是我忽略的一件小事。
Autofac 仅采用类型的最后一个定义。因为我重新注册了它采用最后一个定义的类型。
这只是问题的一部分。另一部分(生成有趣的异常消息的部分)是 RegisterType() 尝试自动装配类型这一事实。并且因为除了被命名的例外屏蔽之外,所有对象都可以通过其类型找到。
工作配置如下所示:
一个简单的错误,我花了几个小时才弄清楚。希望这能帮助其他一些迷失的灵魂。
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:
Easy mistake that took me some hours to figure out. Hope this helps some other lost soul out there.