gpt4 book ai didi

c# - Autofac 不解析外部 Controller 的属性

转载 作者:行者123 更新时间:2023-11-30 22:10:20 24 4
gpt4 key购买 nike

我有两个项目:一个标准的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com