Skip to main content
 首页 » 编程设计

c#之不支持服务类型 IAssembliesResolver。参数名称 : serviceType

2024年02月06日11jirigala

我在尝试连接 Simple Injector 和 web api 时遇到了一些问题

我得到的错误是:

The service type IAssembliesResolver is not supported. Parameter name: serviceType

当我尝试使用 simpleinjector 和 web api (nuget) 集成库附带的扩展方法注册 Controller 时,它抛出了异常

container.RegisterWebApiControllers(GlobalConfiguration.Configuration); 

我刚刚修改了代码以覆盖 IAssemblyResolver,但它不起作用

public static void Initialize() 
    { 
        GlobalConfiguration.Configuration.Services 
        .Replace(typeof(IAssembliesResolver), 
            new CustomAssembliesResolver()); 
 
        var container = new Container(); 
 
        InitializeContainer(container); 
 
        container.RegisterWebApiControllers(GlobalConfiguration.Configuration); 
 
        container.Verify(); 
 
        GlobalConfiguration.Configuration.DependencyResolver = 
            new SimpleInjectorWebApiDependencyResolver(container); 
    } 

我从 this question 中获取了自定义程序集解析器,并将其指向 bin 文件夹

public class CustomAssembliesResolver:DefaultAssembliesResolver 
    { 
        public override ICollection<Assembly> GetAssemblies() 
        { 
            var appPath = AppDomain.CurrentDomain.BaseDirectory; 
 
            var  plugins = ( 
             from file in Directory.GetFiles( 
                 appPath + "\\bin", "*.dll", 
                 SearchOption.AllDirectories) 
             let assembly = Assembly.LoadFile(file) 
             select assembly) 
             .ToArray();     
 
            return base.GetAssemblies() 
                .Union(plugins).ToArray(); 
        } 
    } 

有什么想法吗?

更新:

这是堆栈:

[ArgumentException: The service type IAssembliesResolver is not supported. 
Parameter name: serviceType] 
   System.Web.Http.Services.DefaultServices.GetService(Type serviceType) +529 
   System.Web.Http.ServicesExtensions.GetService(ServicesContainer services) +62 
   System.Web.Http.ServicesExtensions.GetServiceOrThrow(ServicesContainer services) +60 
   SimpleInjector.SimpleInjectorWebApiExtensions.RegisterWebApiControllers(Container container, HttpConfiguration configuration) +107 
   ProjectName.Api.App_Start.SimpleInjectorWebApiInitializer.Initialize() in c:\Users\User\Documents\Visual Studio 2013\Projects\ProjectName.Api\Project.Api\App_Start\SimpleInjectorWebApiInitializer.cs:25 
 
[TargetInvocationException: Exception has been thrown by the target of an invocation.] 
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +229 
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +211 
   System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35 
   WebActivator.BaseActivationMethodAttribute.InvokeMethod() +341 
   WebActivator.ActivationManager.RunActivationMethods() +534 
   WebActivator.ActivationManager.RunPostStartMethods() +38 
   WebActivator.StartMethodCallingModule.Init(HttpApplication context) +159 
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +530 
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +304 
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +404 
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475 
 
[HttpException (0x80004005): Exception has been thrown by the target of an invocation.] 
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12617364 
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12456981 

请您参考如下方法:

经过几个小时的努力,我得到了这个异常:

Entry point was not found

结果发现这个绑定(bind)重定向丢失了

<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
  <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="5.0.0.0" /> 
  </dependentAssembly> 

感谢@steven帮我指出了解决这个问题的方向

我删除了 CustomAssembliesResolver,现在一切正常!