I have two projects: a standard Web Api project and a class library project which contains all the controllers. In the Web Api I have the following on the Global.asax class:
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();
}
}
the controller:
public class PropertyAgentController : ApiController
{
public ICommandControllerProcessor CommandControllerProcessor { get; set; }
[HttpPost]
public HttpResponseMessage HandleMessage()
{
return CommandControllerProcessor.HandleMessage(this);
}
}
and the dependency:
public interface ICommandControllerProcessor
{
HttpResponseMessage HandleMessage(ApiController controller);
}
[IocContainerMarker]
public class CommandControllerProcessor : ICommandControllerProcessor
{
public virtual HttpResponseMessage HandleMessage(ApiController controller)
{
return null;
}
}
The CommandControllerProcessor class lives in the web api project. The property was being resolved when I had the controllers in the same project but as soon as I created a different project, the controllers are still discovered but the property is not wired.
Any ideas of what the problem might be?
Thanks a lot.