Autofac无法解析外部控制器上的属性

时间:2014-01-03 11:41:21

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

我有两个项目:一个标准的Web Api项目和一个包含所有控制器的类库项目。在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();
    }
}

控制器:

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项目中。当我在同一个项目中安装了控制器时,该属性正在解决,但是一旦我创建了一个不同的项目,控制器仍然被发现但属性没有连线。

关于问题可能是什么的任何想法?

非常感谢。

1 个答案:

答案 0 :(得分:3)

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