Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection webapi&;简单注入器-解决从外部dll加载的控制器问题_Dependency Injection_Asp.net Mvc 4 - Fatal编程技术网

Dependency injection webapi&;简单注入器-解决从外部dll加载的控制器问题

Dependency injection webapi&;简单注入器-解决从外部dll加载的控制器问题,dependency-injection,asp.net-mvc-4,Dependency Injection,Asp.net Mvc 4,我正在使用MVC4 Web API和Simple Injector构建一个Web API,它应该公开各种CRUD和查询操作。在我的案例中使用IOC的原因是,我们是一家开发商店,我需要能够让客户构建他们自己的web api控制器,以公开他们需要的数据,从而从我们的系统中公开需要的数据。因此,我希望设计我的解决方案,通过使所有控制器(包括我们的控制器和我们的客户控制器)都是外部的,并且可以通过IOC加载,从而使我能够跟踪自己的产品 网站没有对库的任何引用,但库包含我希望在网站中使用的控制器。我有找到

我正在使用MVC4 Web API和Simple Injector构建一个Web API,它应该公开各种CRUD和查询操作。在我的案例中使用IOC的原因是,我们是一家开发商店,我需要能够让客户构建他们自己的web api控制器,以公开他们需要的数据,从而从我们的系统中公开需要的数据。因此,我希望设计我的解决方案,通过使所有控制器(包括我们的控制器和我们的客户控制器)都是外部的,并且可以通过IOC加载,从而使我能够跟踪自己的产品

网站没有对库的任何引用,但库包含我希望在网站中使用的控制器。我有找到dll插件并加载控制器类型的代码,但当我尝试导航到它将表示的路由时,它说找不到它

i、 e.如果我尝试导航到/api/Test1Api,我会看到文本“hello world”

我这里的问题是,虽然我已经加载了控制器类型,但我无法将其转换为网站所说的路由

下面是我如何注册容器的

[assembly: WebActivator.PostApplicationStartMethod(typeof(Spike.Web.Api.App_Start.SimpleInjectorInitializer), "Initialize")]
public static class SimpleInjectorInitializer
{
    public static void Initialize()
    {
        // Create the IOC container.
        var container = new Container();

        InitializeContainer(container);

        container.RegisterMvcAttributeFilterProvider();
        // Verify the container configuration
        container.Verify();

        // Register the dependency resolver.
        GlobalConfiguration.Configuration.DependencyResolver =
                    new SimpleInjectorWebApiDependencyResolver(container);
        //DependencyResolver.SetResolver(new SimpleInjectorWebApiDependencyResolver(container));
    }

    private static void InitializeContainer(Container container)
    {
        var appPath = AppDomain.CurrentDomain.BaseDirectory;

        string[] files = Directory.GetFiles(appPath + "\\bin\\Plugins", "*.dll",
            SearchOption.AllDirectories);
        var assemblies = files.Select(Assembly.LoadFile);

        // register Web API controllers
        var apiControllerTypes =
            from assembly in assemblies
            where !assembly.IsDynamic
            from type in assembly.GetExportedTypes()
            where typeof(IHttpController).IsAssignableFrom(type)
            where !type.IsAbstract
            where !type.IsGenericTypeDefinition
            where type.Name.EndsWith("Controller", StringComparison.Ordinal)
            select type;

        // register MVC controllers
        var mvcControllerTypes =
            from assembly in assemblies
            where !assembly.IsDynamic
            from type in assembly.GetExportedTypes()
            where typeof(IController).IsAssignableFrom(type)
            where !type.IsAbstract
            where !type.IsGenericTypeDefinition
            where type.Name.EndsWith("Controller", StringComparison.Ordinal)
            select type;

        foreach (var controllerType in apiControllerTypes)
        {
            container.Register(controllerType);
        }

        foreach (var controllerType in mvcControllerTypes)
        {
            container.Register(controllerType);
        }
    }
}
我知道了

因此,我在Web Api调用CustomAssembliesResolver中创建了一个新类,该类继承自DefaultAssembliesResolver。实际上,我将我的程序集添加到查找控制器时解析的程序集列表中。我仍然有使用简单注入器的代码用于解决方案的DI部分

public class CustomAssembliesResolver : DefaultAssembliesResolver
{
    public override ICollection<Assembly> GetAssemblies()
    {
        var appPath = AppDomain.CurrentDomain.BaseDirectory;
        var baseAssemblies = base.GetAssemblies();
        var assemblies = new List<Assembly>(baseAssemblies);
        var files = Directory.GetFiles(appPath + "\\bin\\Plugins", "*.dll",
                        SearchOption.AllDirectories);
        var customAssemblies = files.Select(Assembly.LoadFile);

        // register Web API controllers
        var apiControllerAssemblies =
            from assembly in customAssemblies
            where !assembly.IsDynamic
            from type in assembly.GetExportedTypes()
            where typeof(IHttpController).IsAssignableFrom(type)
            where !type.IsAbstract
            where !type.IsGenericTypeDefinition
            where type.Name.EndsWith("Controller", StringComparison.Ordinal)
            select assembly;

        foreach (var assembly in apiControllerAssemblies)
        {
            baseAssemblies.Add(assembly);
        }

        return assemblies;
    }
}
希望这对别人有帮助

可能重复的
GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new CustomAssembliesResolver());