c# - Autofac 无法解析外部 Controller 上的属性

标签 c# c#-4.0 asp.net-web-api

我有两个项目:一个标准 Web Api 项目和一个包含所有 Controller 的类库项目。在 Web Api 中,我在 Global.asax 类上有以下内容:

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        WebApiConfig.Register(GlobalConfiguration.Configuration);

        RouteConfig.RegisterRoutes(RouteTable.Routes);

        var builder = new ContainerBuilder();

        builder.RegisterApiControllers(GetAssemblies(true)).PropertiesAutowired();
        builder
            .RegisterAssemblyTypes(GetAssemblies(false))             
            .Where(t => t.GetCustomAttributes(typeof(IocContainerMarkerAttribute), false).Any())
            .PropertiesAutowired();

        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(builder.Build());
    }

    private static Assembly[] GetAssemblies(bool isController)
    {
        var path = HttpContext.Current.Server.MapPath("~/Bin");

        return isController
            ? Directory.GetFiles(path, "*.dll") .Where(x => x.Contains(".Controllers")).Select(Assembly.LoadFile).ToArray()
            : Directory.GetFiles(path, "*.dll").Select(Assembly.LoadFile).ToArray();
    }
}

Controller :

public class PropertyAgentController : ApiController
{
    public ICommandControllerProcessor CommandControllerProcessor { get; set; }


    [HttpPost]
    public HttpResponseMessage HandleMessage()
    {
        return CommandControllerProcessor.HandleMessage(this);
    }
}

和依赖关系:

public interface ICommandControllerProcessor
{
    HttpResponseMessage HandleMessage(ApiController controller);
}

[IocContainerMarker]
public class CommandControllerProcessor : ICommandControllerProcessor
{
    public virtual HttpResponseMessage HandleMessage(ApiController controller)
    {
        return null;
    }
}

CommandControllerProcessor 类位于 Web api 项目中。当我在同一个项目中拥有 Controller 时,该属性正在解析,但一旦我创建了不同的项目,仍然会发现 Controller ,但该属性未连接。

有什么想法可能是什么问题吗?

非常感谢。

最佳答案

我按照 nemesv 给我的提示进行操作并使其正常工作。基本上缺少了一些东西,但注册覆盖和 LoadFile 的使用绝对是一个问题。

    protected void Application_Start()
    {
        WebApiConfig.Register(GlobalConfiguration.Configuration);

        RouteConfig.RegisterRoutes(RouteTable.Routes);

        var builder = new ContainerBuilder();

        builder.RegisterApiControllers(GetAssemblies(true)).PropertiesAutowired();

        builder
            .RegisterAssemblyTypes(GetAssemblies(false))             
            .Where(t => t.GetCustomAttributes(typeof(IocContainerMarkerAttribute), false).Any())
            .AsImplementedInterfaces()
            .PropertiesAutowired();

        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(builder.Build());
    }

    private static Assembly[] GetAssemblies(bool isController)
    {
        var path = HttpContext.Current.Server.MapPath("~/Bin");

        return isController
            ? Directory.GetFiles(path, "*.dll").Where(x => x.Contains(".Controllers")).Select(Assembly.LoadFrom).ToArray()
            : Directory.GetFiles(path, "*.dll").Where(x => !x.Contains(".Controllers")).Select(Assembly.LoadFrom).ToArray();
    }

关于c# - Autofac 无法解析外部 Controller 上的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20902848/

相关文章:

c# - 找不到 Azure Cosmos BulkExecuter 方法

c# - ReaderWriterLockSlim 是否有等效的 lock{} 语句?

c# - 什么时候应该在 C# 4.0 中使用动态关键字?

asp.net-mvc - RequestContext.Principal.Identity.Name 在 Web api 2 帖子中为空

.net - ConfigureAwait 有什么作用? ASP.NET Web API 2

.net - ASP.NET WebApi - 为多个来源启用 CORS

c# - 非标准时区字符串的 DateTime.ParseExact 问题

c# - 同时保存两个 .xlsm 文件(每个大约 7MB)时,ClosedXML .SaveAs(MemoryStream ms) 没有进展

java - .Net 4 垃圾收集

c# - 反对字符串,反之亦然