gpt4 book ai didi

c# - vNext 中间件抛出 "Unable to locate suitable constructor for type ' XXX'”错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:14 25 4
gpt4 key购买 nike

尝试将某些 OWIN 中间件转换为 vNext 并出现以下错误:

System.InvalidOperationException: Unable to locate suitable constructor for type 'MyMiddleware'. Ensure the type is concrete and all parameters are accepted by a constructor. at Microsoft.Framework.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Obje ct[] parameters) at Microsoft.AspNet.Builder.UseMiddlewareExtensions.<>c__DisplayClass1_0.b__0(RequestDelegate next) at Microsoft.AspNet.Builder.ApplicationBuilder.Build() at Microsoft.AspNet.Hosting.HostingEngine.EnsureApplicationDelegate(HostingContext context) at Microsoft.AspNet.Hosting.HostingEngine.Start(HostingContext context)
at Microsoft.AspNet.Hosting.Program.Main(String[] args)

我不知道如何开始调试它。我需要知道 MS 框架试图调用什么排列以及使用什么参数。我如何让它吐出此信息或设置断点?

public class MyMiddleware : AuthenticationMiddleware<MyOptions>
{
public MyMiddleware(RequestDelegate next,
IOptions<MyOptions> options,
ConfigureOptions<MyOptions> configureOptions)
: base(next, options, configureOptions)
{
// doesn't want to call this
}

public MyMiddleware(RequestDelegate next,
IOptions<MyOptions> options,
ConfigureOptions<MyOptions> configureOptions,
IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory)
: base(next, options, configureOptions)
{
// doesn't want to call this either and this is the one I'd like to be called
}
}


public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddLogging();
services.AddDataProtection();
}

public void Configure(IApplicationBuilder app)
{
app.UseErrorPage();

app.UseMyMiddleware(opts =>
{
});
}
}


public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder app, Action<MyMiddleware> configureOptions = null, string optionsName = "")
{
return app.UseMiddleware<MyMiddleware>(
new ConfigureOptions<MyMiddleware>(configureOptions ?? (o => { }))
{
Name = optionsName
});
}

我试过在构造函数中四处移动参数,还尝试使用最小的参数集为 AuthenticationMiddleware 类创建重载,但得到了同样的错误。设置 DNX_TRACE=1 也无法进一步了解问题。

最佳答案

我能够使用以下步骤弄清楚如何让 Visual Studio 进入 MS 框架代码:

  1. 转到 Debug -> Exception Settings
  2. 展开Common Language Runtime Exceptions并检查 InvalidOperationException
  3. 关注这个article关于如何调试框架代码。 (非常酷的 2015 年专题)
  4. 在上面的文章中而不是设置 sources到源代码的物理位置使用 projects元素代替。当你保存 global.json VS 应该刷新解决方案资源管理器并添加它与您的解决方案相匹配的任何框架项目。对我来说是Microsoft.Framework.DependencyInjection & Microsoft.Framework.DependencyInjection.Interface .下面是我的 global.json看起来像:

{
"projects": [ "src", "test", "D:/GitHub/DependencyInjection/src"],//注意使用正斜杠而不是反斜杠,令人失望;(
“资源”:[“包”],
“SDK”:{
“版本”:“1.0.0-beta4”

}

  1. 接下来在您的项目上启动调试器,它不仅应该编译您的项目,还应该编译您从 github 下载的任何 MS 框架源代码,从而生成必要的框架 .pdb,以允许您在第 2 步中感兴趣的异常时中断.

通过 ActivatorUtilities.CreateInstance 后代码库我可以看到问题。在我的扩展方法中复制/粘贴失败。我用的是 MyMiddlewareAction<>ConfigureOptions参数。将它们更改为 MyOptions修复它。

关于c# - vNext 中间件抛出 "Unable to locate suitable constructor for type ' XXX'”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497031/

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