使用Microsoft Asp.NET依赖注入时找不到HttpActionContext.get_Request()方法 [英] HttpActionContext.get_Request() method not found when using Microsoft Asp.NET Dependency Injection

查看:217
本文介绍了使用Microsoft Asp.NET依赖注入时找不到HttpActionContext.get_Request()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Microsoft Extensions Dependency Injection 添加到现有的ASP.NET WebApi / Owin项目。

I am trying to add Microsoft Extensions Dependency Injection to an existing ASP.NET WebApi/Owin project.

跟随此博客文章,我添加了 ConfigureServices 函数,该函数已实现 DependencyResolver ,并创建了 HttpConfiguration 实例来使用它。

Following this blog post, I have added the ConfigureServices function, implemented DependencyResolver, and made HttpConfiguration instance to use it.

我也将所有非抽象控制器添加到DI中,并确保使用DI机制实例化它们。

Also I added all the non-abstract controllers to DI, and ensured they are instantiated using the DI mechanism.

但是在完成所有这些操作之后,对应用程序的请求返回错误500例外

But after all these actions, requests to the application return error 500 with exception


未找到方法:'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request() '。

Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'.

我明白了即使我只是将 Microsoft.Extensions.DependencyInjection 包添加到NuGet且不更改任何其他代码,也可以得到相同的结果。删除后,一切正常。

I get the same result even if I just add the Microsoft.Extensions.DependencyInjection package to NuGet and don't change any other code. After I remove it, everything works fine.

这是我添加到Startup.cs中的代码:

Here is the code I added into Startup.cs:

DependcyResolver实现:

The DependcyResolver implementation:

public class DefaultDependencyResolver :
    System.Web.Http.Dependencies.IDependencyResolver, 
    System.Web.Http.Dependencies.IDependencyScope
{
    protected IServiceProvider serviceProvider;

    public DefaultDependencyResolver(IServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }

    public IDependencyScope BeginScope()
    {
        return this;
    }

    public void Dispose() { }

    public object GetService(Type serviceType)
    {
        return this.serviceProvider.GetService(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return this.serviceProvider.GetServices(serviceType);
    }

}

在DI中添加控制器:

public void ConfigureServices(IServiceCollection services)
{
    System.Diagnostics.Debugger.Launch();   
    IEnumerable<Type> controllers = typeof(Startup).Assembly.GetExportedTypes()
       .Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
       .Where(t => typeof(IController).IsAssignableFrom(t)
          || t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase));

    foreach (var c in controllers)
    {
        services.AddTransient(c);
    }
}

创建ServiceCollection,调用服务配置并设置DependencyResolver:

Creating ServiceCollection, calling services configuration and setting DependencyResolver:

public void Configuration(IAppBuilder app) {
    // ...
    HttpConfiguration httpConfiguration = new HttpConfiguration();

    var services = new ServiceCollection();
    ConfigureServices(services);

    httpConfiguration.DependencyResolver = 
        new DefaultDependencyResolver(services.BuildServiceProvider());

    WebApiConfig.Register(httpConfiguration);
    app.UseWebApi(httpConfiguration);
}

异常堆栈跟踪:

at myCompany.myProduct.Controllers.SomeController.Get(String id, String offline)
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()


推荐答案

您可能需要将绑定重定向添加到web.config文件。
检查以下链接,以获取有关该问题的更多信息:

You might need to add binding redirects to your web.config file. Check the following links with more information on the issue:

https://github.com/Microsoft/BotBuilder/issues/3615

> https://github.com/dotnet/standard/issues/481

https://docs.microsoft.com/zh-cn/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection

这篇关于使用Microsoft Asp.NET依赖注入时找不到HttpActionContext.get_Request()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆