C# 将Autofac与Web Api 2和Owin一起使用
我是DI库的新手,正在尝试在带有Owin的WebApi 2项目中使用Autofac。这是我的Owin创业班C# 将Autofac与Web Api 2和Owin一起使用,c#,dependency-injection,autofac,asp.net-web-api2,owin,C#,Dependency Injection,Autofac,Asp.net Web Api2,Owin,我是DI库的新手,正在尝试在带有Owin的WebApi 2项目中使用Autofac。这是我的Owin创业班 [assembly: OwinStartup(typeof(FMIS.SIGMA.WebApi.Startup))] namespace FMIS.SIGMA.WebApi { public class Startup { public void Configuration(IAppBuilder app) { var
[assembly: OwinStartup(typeof(FMIS.SIGMA.WebApi.Startup))]
namespace FMIS.SIGMA.WebApi
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
var config = new HttpConfiguration();
WebApiConfig.Register(config);
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
ConfigureOAuth(app);
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
}
当我调用一个Api方法时,我得到了这个错误
尝试创建类型为的控制器时出错
“我的控制者”。确保控制器具有
无参数公共构造函数
我错过了什么
MyController代码是这样的
public class MyController : ApiController
{
ISomeCommandHandler someCommanHandler;
public MyController(ISomeCommandHandler SomeCommandHandler)
{
this.someCommanHandler = SomeCommandHandler;
}
// POST: api/My
public void Post([FromBody]string value)
{
someCommanHandler.Execute(new MyCommand() {
Name = "some value"
});
}
// GET: api/My
public IEnumerable<string> Get()
{
}
// GET: api/My/5
public string Get(int id)
{
}
}
公共类MyController:ApiController
{
等轴司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令部司令;
公共MyController(ISomeCommandHandler SomeCommandHandler)
{
this.SomeCommandHandler=SomeCommandHandler;
}
//帖子:api/My
公共作废帖子([FromBody]字符串值)
{
执行(新的MyCommand(){
Name=“一些值”
});
}
//获取:api/My
公共IEnumerable Get()
{
}
//获取:api/My/5
公共字符串Get(int-id)
{
}
}
您已将DependencyResolver
设置为AutofacWebApiDependencyResolver
,因此Autofac将发挥作用并为您实例化依赖项。现在,当需要接口实例时,您必须明确地告诉Autofac应该使用哪些具体实现
您的控制器需要ISomeCommandHandler
的实例:
MyController(ISomeCommandHandler SomeCommandHandler)
因此,您需要配置公开该接口的类型:
builder.RegisterType<CommandHandler>.As<ISomeCommandHandler>();
builder.RegisterType.As();
有关Autofac注册概念的更多示例,请查看此示例。您已将
DependencyResolver
设置为AutofacWebAPI DependencyResolver
,因此Autofac将发挥作用并为您实例化依赖项。现在,当需要接口实例时,您必须明确地告诉Autofac应该使用哪些具体实现
您的控制器需要ISomeCommandHandler
的实例:
MyController(ISomeCommandHandler SomeCommandHandler)
因此,您需要配置公开该接口的类型:
builder.RegisterType<CommandHandler>.As<ISomeCommandHandler>();
builder.RegisterType.As();
有关Autofac注册概念的更多示例,请查看此示例。您已将
DependencyResolver
设置为AutofacWebAPI DependencyResolver
,因此Autofac将发挥作用并为您实例化依赖项。现在,当需要接口实例时,您必须明确地告诉Autofac应该使用哪些具体实现
您的控制器需要ISomeCommandHandler
的实例:
MyController(ISomeCommandHandler SomeCommandHandler)
因此,您需要配置公开该接口的类型:
builder.RegisterType<CommandHandler>.As<ISomeCommandHandler>();
builder.RegisterType.As();
有关Autofac注册概念的更多示例,请查看此示例。您已将
DependencyResolver
设置为AutofacWebAPI DependencyResolver
,因此Autofac将发挥作用并为您实例化依赖项。现在,当需要接口实例时,您必须明确地告诉Autofac应该使用哪些具体实现
您的控制器需要ISomeCommandHandler
的实例:
MyController(ISomeCommandHandler SomeCommandHandler)
因此,您需要配置公开该接口的类型:
builder.RegisterType<CommandHandler>.As<ISomeCommandHandler>();
builder.RegisterType.As();
有关Autofac注册概念的更多示例,请查看此示例。您可以显示您的MyController代码吗?@hugo MyController代码添加可以显示您的MyController代码吗?@hugo MyController代码添加可以显示您的MyController代码吗?@hugo MyController代码添加可以显示您的MyController代码吗?@hugo MyController代码添加