We have an application divided in five projects which are the following:
- A project with only Html pages
- Web Api project which functions as the service layer which only contains ApiController classes
- A Business layer class library
- A Business layer contracts class library which only contains interfaces
- A Data layer class library
- A Data layer contracts class library which also only contains interfaces
The Web Api services contains references to all the class libraries as well as the Autofac and AutofacWebApiDependencyResolver.
I have already added the necessary code to register the container and resolver as it is explained in the Autofac documentation:
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
Currently I have a very basic dependency hierarchy to test this which is as follows:
//On the data contracts
public interface IData
{
void SomeDataMethod();
}
//On the data layer
public class Data : IData
{
public void SomeDataMethod(){}
}
//On the business contracts
public interface IBusiness
{
void SomeBusinessMethod();
}
//On the business layer
public class Business : IBusiness
{
private readonly IData _data;
public Business(IData data)
{
_data = data;
}
}
//On the Web Api project
[EnableCors("*", "*", "*")]
public class MyController : ApiController
{
private IBusiness _business;
public MyController(IBusiness business)
{
_business = business;
}
}
So there's not rocket science at all here but when I run the project I get the following error:
XMLHttpRequest cannot load http://localhost:61101/api/MyController. No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:56722' is therefore not allowed
access.
If I remove the constructor from the controller the application works perfectly, the controller gets instantiated and its get method gets called.
What could I be doing wrong?