c# - StructureMap 异常代码:202 未为 PluginFamily System.Net.Http.HttpMessageHandler、System.Net.Http、版本 = 4.2.0.0 定义默认实例
问题描述
我有一个 WCF 应用程序,它正在侦听消息。EventHandler 使用 ServiceClient,它使用 HttpClient 调用另一个 API。
国际奥委会类:
public static class IoC
{
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
// Handlers
x.For<IHandle>().Use<EventHandler>();
// Service Clients
x.For<HttpClient>().Use<HttpClient>();
x.For<IServiceClient>().Use<ServiceClient>();
});
return ObjectFactory.Container;
}
}
我尝试在 Initialize 之后但在返回之前添加它:
ObjectFactory.Configure(x =>
{
x.Scan(scan =>
{
scan.LookForRegistries();
scan.Assembly("System.Net.Http");
});
});
但是,我仍然在标题中得到例外。
解决方案
I was able to resolve this issue. The problem was that I was using an older version of StructureMap (v2.6.4.1) which was not compatible with System.Net.Http, Version=4.2.0.0. After updating to the latest version and making some adjustments since ObjectFactory doesn't exist in the latest version, I was getting an error:
No default Instance is registered and cannot be automatically determined for type 'System.Net.Http.HttpMessageHandler'
This is because StructureMap defaultly tries to use the greediest constructor. To fix this, I changed my DI for HttpClient to this:
x.For<HttpClient>().Use<HttpClient>().SelectConstructor(() => new HttpClient());
推荐阅读
- python - 在 PyQt5 中一次显示多个问题
- python - 如何在python中将输出作为具有唯一键和多个值的字典返回?
- flutter - 在 Flutter 中缓存来自 WebView 的图像
- python - url open 在某些网站而不是其他网站上返回错误
- segmentation-fault - !$OMP PARALLEL 在 fortran 和分段错误中的问题
- pandas - 通过 putty 在本地服务器上安装 numpy 的问题
- python - 如何在python中验证isdigit()中的浮点数
- regex - 向已创建的正则表达式添加条件
- android - 无法使用 kotlin 在 google 登录上为 android 选择不同的帐户
- html - 悬停在导航上的效果。li 但排除其余的 li 元素