尝试将一些OWIN中间件转换为vNext并收到以下错误:
System.InvalidOperationException:无法找到合适的 “MyMiddleware”类型的构造函数。 确保类型具体,并接受所有参数 由构造函数。在 Microsoft.Framework.DependencyInjection.ActivatorUtilities.CreateInstance(的IServiceProvider provider,Type instanceType,Obje ct [] parameters)at Microsoft.AspNet.Builder.UseMiddlewareExtensions<> c__DisplayClass1_0.b__0(RequestDelegate 接下来)在Microsoft.AspNet.Builder.ApplicationBuilder.Build()中 Microsoft.AspNet.Hosting.HostingEngine.EnsureApplicationDelegate(HostingContext 上下文) Microsoft.AspNet.Hosting.HostingEngine.Start(HostingContext context)
在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也无法进一步了解该问题。
答案 0 :(得分:3)
我能够弄清楚如何使用以下步骤让Visual Studio进入MS框架代码:
Debug -> Exception Settings
Common Language Runtime Exceptions
并查看InvalidOperationException
sources
设置为源代码的物理位置,而是使用projects
元素。当您保存global.json
VS时,应刷新解决方案资源管理器并添加与您的解决方案匹配的任何框架项目。对我而言,Microsoft.Framework.DependencyInjection
&amp; Microsoft.Framework.DependencyInjection.Interface
。以下是我global.json
的样子: {
&#34;项目&#34;:[&#34; src&#34;,&#34; test&#34;,&#34; D:/ GitHub / DependencyInjection / src&#34; ],//注意使用正斜杠而不是反斜杠,令人失望;(
&#34;来源&#34;:[&#34;包&#34;],
&#34; sdk&#34;:{
&#34;版本&#34;:&#34; 1.0.0-beta4&#34;
}
}
在单步执行ActivatorUtilities.CreateInstance
代码库后,我可以看到问题所在。在我的扩展方法中复制/粘贴失败。我在MyMiddleware
和Action<>
参数中使用了ConfigureOptions
。将它们更改为MyOptions
修复它。