MVC4 웹 API 및 간단한 인젝터를 사용하여 다양한 CRUD 및 쿼리 작업을 노출해야하는 웹 API를 작성하고 있습니다. 필자의 경우 IOC를 사용하는 이유는 우리가 개발구에 들기 때문에 고객이 자신의 웹 API 컨트롤러를 구축하여 시스템에서 필요를 폭로하는 데 필요한 데이터를 노출 할 수 있어야하기 때문입니다. 결과적으로 IOC를 통해 모든 컨트롤러를 우리와 고객 모두에게 외부 및로드 가능하게함으로써 내 제품을 dogfood 할 수있는 방식으로 솔루션을 설계하고자했습니다.웹 API 및 간단한 인젝터 - 외부 dll에서로드 된 문제 해결 컨트롤러
웹 사이트에 라이브러리에 대한 참조가 없지만 라이브러리에 웹 사이트에서 사용하려는 컨트롤러가 포함되어 있습니다. 나는 dll 플러그인을 찾고 제어기 유형을로드하는 코드를 가지고 있지만 표현할 경로를 탐색하려 할 때 찾을 수 없다고 말합니다.
즉 내가/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 - 외부 DLL에서로드 된 컨트롤러를 해결할 수 없음] (http://stackoverflow.com/questions/16865789/web-api-unable-to-resolve-controller-that-was- 외부에서로드 된 - dll) – Steven