1

I'm having some issues trying to wire Simple Injector and web api

the error I'm getting is:

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

and it throws the exception by the time I'm trying to register the controllers with the extension method that comes with the integration library for simpleinjector and web api (nuget)

simple injector webapi integraton package

container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

I just modified the code to override the IAssemblyResolver but it doesn't work

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);
    }

I took the custom assembly resolver from this question and I just pointed it to the bin folder

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();
        }
    }

Any Ideas?

UPDATE:

this is the stack:

[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
Community
  • 1
  • 1
pedrommuller
  • 15,741
  • 10
  • 76
  • 126
  • Can you post the complete call stack and all the exception information? – Steven Jan 13 '15 at 17:09
  • please check my update with the stack, thanks @Steven – pedrommuller Jan 13 '15 at 17:36
  • What happens if you remove the `.Replace(typeof(IAssembliesResolver),` line? I expect it to fail as well. – Steven Jan 14 '15 at 07:48
  • it fails @steven, I kind of solve it by removeing the line for the api registration and doing a manual registration of the controllers but that's a workaround more than a solution for the issue – pedrommuller Jan 14 '15 at 17:24
  • 1
    I think the problem is caused by an invalid assembly binding. It might be that your application references a different version of Web API than Simple Injector does. NuGet will normally fix this for you by adding configuration in your web.config, but it might have failed doing that (for whatever reason). You might want to check this. – Steven Jan 14 '15 at 17:28

1 Answers1

4

After struggling for some hours I got to the point to get this exception:

Entry point was not found

it turned out that this the binding redirect was missing

<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>

thanks to @steven to help me pointing out the direction to solve this issue

I removed the CustomAssembliesResolver and everything works fine now!

pedrommuller
  • 15,741
  • 10
  • 76
  • 126