c# - 使用通用服务解决依赖关系失败
问题描述
我在统一解析器类中使用了以下代码:
public object GetService(Type serviceType)
{
try
{
return _container.Resolve(serviceType);
}
catch (ResolutionFailedException ex)
{
mLogger.Error(ex.Message.ToString() + "\n");
}
catch(Exception ex)
{
mLogger.Error(ex.Message.ToString() + "\n");
}
return null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType);
}
catch (ResolutionFailedException ex)
{
mLogger.Error(ex.Message.ToString() + "\n");
}
catch (Exception ex)
{
mLogger.Error(ex.Message.ToString() + "\n");
}
return null;
}
public IDependencyScope BeginScope()
{
var child = _container.CreateChildContainer();
return new UnityResolver(child);
}
public void Dispose()
{
_container.Dispose();
}
在 WebAPIConfig 我使用了容器:
var container = new UnityContainer();
container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.PerResolve);
config.DependencyResolver = new UnityResolver(container);
现在我遇到了一些中间问题,当我在服务器上部署代码时,它可以工作一段时间,并且在 1 或 2 小时后出现错误:
Exception : Resolution of the dependency failed, type = 'System.Net.Http.Formatting.IContentNegotiator', name = '(none)'.\nException occurred while: while resolving.\nException is: InvalidOperationException - The current type, System.Net.Http.Formatting.IContentNegotiator, is an interface and cannot be constructed. Are you missing a type mapping?
InvalidOperationException - The current type, System.Net.Http.Formatting.IContentNegotiator, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving System.Net.Http.Formatting.IContentNegotiator,(none)
---> System.InvalidOperationException: The current type, System.Net.Http.Formatting.IContentNegotiator, is an interface and cannot be constructed. Are you missing a type mapping? ---> Unity.ObjectBuilder.BuildPlan.DynamicMethod.Creation.DynamicMethodConstructorStrategy+InvalidRegistrationException: Exception of type 'Unity.ObjectBuilder.BuildPlan.DynamicMethod.Creation.DynamicMethodConstructorStrategy+InvalidRegistrationException' was thrown.
--- End of inner exception stack trace ---
at Unity.ObjectBuilder.BuildPlan.DynamicMethod.Creation.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)
at lambda_method(Closure , IBuilderContext )
at Unity.ObjectBuilder.BuildPlan.DynamicMethod.DynamicBuildPlanGenerationContext.<>c__DisplayClass16_0.<GetBuildMethod>b__0(IBuilderContext context)
at Unity.Strategies.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
at Unity.UnityContainer.ThrowingBuildUp(IBuilderContext context)
--- End of inner exception stack trace ---
at Unity.UnityContainer.ThrowingBuildUp(IBuilderContext context)
at ConfigurationAPI.App_Start.UnityResolver.GetService(Type serviceType) in E:\Agent\_work\4\s\ConfigurationAPI\App_Start\UnityResolver.cs:line 25
解决方案
推荐阅读
- graphql - 如何使用 GraphQL 过滤问题评论作者
- git - 如果我在 Github 上更改文件名,然后将这些更改拉入本地存储库,文件名是否也会在本地更改?
- excel - 按自然顺序对 Excel 进行排序
- colors - 如何在没有放大关节的情况下在 JFreeChart 中设置我自己的颜色?
- python - 操作:保存变量然后操作与单行
- postgresql - 升级 postgresql 9.6 -> postgresql 13 因缺少 plpython2 扩展而被阻止,但我似乎无法删除扩展
- c# - 如何创建一个与另一个重叠的网格并在第二个网格中显示第一个网格的某些控件
- c - 为什么在 K&R 8.7 的 free() 的“for”循环的“for”循环的增量部分的测试部分没有增量?
- vb.net - 我的项目似乎已构建,但错误不断发生,我无法找出原因
- mongodb - 在我的 mongodb 数据库中创建文档后不执行数据库操作