Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Autofac与Web Api 2和Owin一起使用_C#_Dependency Injection_Autofac_Asp.net Web Api2_Owin - Fatal编程技术网

C# 将Autofac与Web Api 2和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

我是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 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代码添加