首页 > 解决方案 > 使用通用服务解决依赖关系失败

问题描述

我在统一解析器类中使用了以下代码:

    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

标签: c#asp.net-mvc-5unity-container

解决方案


推荐阅读