I have a WebApi that uses Simple Injector that was working perfectly fine, but i had to implement OAuth in to the project. Now i have done that and my ApiControllers give me an error like Simple Injector has now been setup correctly
I have my Start.cs file
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Web API configuration and services
HttpConfiguration config = new HttpConfiguration();
Container container = SimpleInjectorConfig.Initialize(app);
ConfigureAuth(app, container);
WebApiConfig.Register(config);
app.UseWebApi(config);
}
public void ConfigureAuth(IAppBuilder app, Container container)
{
var OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = container.GetInstance<IOAuthAuthorizationServerProvider>()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
In my SimpleInjectorConfig file i have
public class SimpleInjectorConfig
{
public static Container Initialize(IAppBuilder app)
{
var container = GetInitializeContainer(app);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
return container;
}
public static Container GetInitializeContainer(IAppBuilder app)
{
var container = new Container();
container.RegisterSingle<IAppBuilder>(app);
container.Register<IOAuthAuthorizationServerProvider,
ApiAuthorizationServerProvider>();
// myService
container.Register<IService, MyService>();
// myRepository
container.Register<IRepository, MyRepository>();
// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
return container;
}
}
public class ApiAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
private IService _service;
public ApiAuthorizationServerProvider(IService service)
{
_service = service;
}
public override async Task ValidateClientAuthentication(
OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(
OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers
.Add("Access-Control-Allow-Origin", new[] { "*" });
User user = _service.Query(e => e.Email.Equals(context.UserName) &&
e.Password.Equals(context.Password)).FirstOrDefault();
if (user == null)
{
context.SetError("invalid_grant",
"The user name or password is incorrect.");
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}
}
Now my project builds fine, but i cannot test if OAuth is working yet, because i cannot connect to my ApiController. I have not added the [Authorize] tag to the Controller yet, so i should be able to access it.
Here is my controller
public class MyController : ApiController
{
private IService _service;
public MyController(IService service)
{
_service = service;
}
public IHttpActionResult Get()
{
// get all entities here via service
return Ok(list);
}
}
The error message i get says
An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor.
I thought this would be registered via
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);