在运行时注册自定义控制器 [英] Register custom controller in runtime

查看:92
本文介绍了在运行时注册自定义控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时在我的mvc应用程序中注册一些api控制器。
控制器本身位于类库项目中,并且看起来像:

I've some api controller that i want to register at runtime in my mvc application. The controller itself resides into Class Library project and looks like:

public class CustomController : ApiController
{
    public string Get()
    {
        return "Hello";
    }       
}

我已经编译了该库(给它起了个名字 Injection.dll ),然后在我的mvc应用中尝试通过Unity.Mvc库容器注册这些控制器

I've compile that library (gave it a name Injection.dll), and in my mvc app try to register those controller via Unity.Mvc library container

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();

        Assembly customLogicAssembly = Assembly.LoadFrom(HttpContext.Current.Server.MapPath("~/bin/" + "Injection.dll"));
        Type existingType = customLogicAssembly.GetTypes().First();
        container.RegisterType(existingType);

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}

因此,似乎注册正常,我没有在调试时出错,但是当我导航到api / Custom / Get时,
i没有预期的结果。 找不到资源。 错误。
Pls帮助我在这里错过了一切。

So it seems that registration works fine and i have no errors while debuggig it, but i don't have expected result, when i navigate to api/Custom/Get i have an The resource cannot be found. error in browser. Pls help what i miss here.

推荐答案

MVC和Web API都有自己的依赖项解析器。

MVC and Web API have each its own dependency resolver.

您的代码正在注册MVC依赖项解析器:

Your code is registering the MVC Dependeny Resolver:

DependencyResolver.SetResolver(yourResolver);

您需要注册的是Web API依赖解析器,例如:

What you need to register is the Web API Dependency resolver, like this:

GlobalConfiguration.Configuration.DependencyResolver = yourResolver;

在两种情况下,预期的解析器都必须实现 IDependencyResolver ,但是它有不同的用途。

The expected resolver in both cases must implement IDependencyResolver, but it serves a different purpose.

不建议您同时使用相同的依赖项解析器(因此,容器和组合根)。如果需要这样做,请使用 IDependencyResolver 的两个不同实例,并注册它们所属的控制器。

It's not recommended that you share the same dependency resolver (thus container and composition root) for both things. If you need to do so, use two different instances of IDependencyResolver and register the controllers where they belong.

重要说明
有两个不同的 IDependencyResolver 接口,一个接口用于MVC,另一个接口用于Web API。它们具有相同的名称,但位于不同的名称空间上。并且您需要为每种情况实现适当的选择(或者,如果需要为MVC和Web API都实现DI,则两者都实现):

IMPORTANT NOTE There are two different IDependencyResolver interfaces, one for MVC, and the other for Web API. They have the same name, but are on different namespaces. And you need to implement the appropriate for each case (or both, if you need to implement DI for both MVC and Web API):

  • System.Web.Mvc.IDependencyResolver, for MVC
  • System.Web.Http.Dependencies.IDependencyResolver, for Web API

这篇关于在运行时注册自定义控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆