gpt4 book ai didi

c# - 使用 Microsoft Asp.NET 依赖注入(inject)时找不到 HttpActionContext.get_Request() 方法

转载 作者:行者123 更新时间:2023-12-02 13:25:57 26 4
gpt4 key购买 nike

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

已关注 this blog post ,我添加了 ConfigureServices 函数,实现了 DependencyResolver,并创建了 HttpConfiguration 实例来使用它。

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

但是在完成所有这些操作之后,对应用程序的请求会返回错误 500 并出现异常

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

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

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

DependcyResolver 实现:

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);
}

}

将 Controller 添加到 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:

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()

最佳答案

关于c# - 使用 Microsoft Asp.NET 依赖注入(inject)时找不到 HttpActionContext.get_Request() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46887799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com