【发布时间】:2017-07-06 20:21:01
【问题描述】:
所以我在 C# 中有以下代码:
public Container ConfigureSimpleInjector(IAppBuilder app)
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
container.RegisterPackages();
app.Use(async (context, next) =>
{
using (AsyncScopedLifestyle.BeginScope(container))
{
await next();
}
});
container.Verify();
return container;
}
app.Use() 定义为Owin.AppBuilderUserExtensions.Use(),如下所示:
public static IAppBuilder Use(this IAppBuilder app, Func<IOwinContext, Func<Task>, Task> handler);
VB等效如下:
Public Function ConfigureSimpleInjector(app As IAppBuilder) As Container
Dim container = New Container()
container.Options.DefaultScopedLifestyle = New AsyncScopedLifestyle()
container.RegisterPackages()
app.Use(Async Sub(context, [next])
Using AsyncScopedLifestyle.BeginScope(container)
Await [next]()
End Using
End Sub)
container.Verify()
Return container
End Function
由于某种原因,在VB版本中,app.Use()没有使用现有的扩展功能,而是直接使用IAppBuilder.Use(),如下:
Function Use(middleware As Object, ParamArray args() As Object) As IAppBuilder
为什么 VB 代码不使用与 C# 相同的扩展函数,我怎样才能让它使用它?
编辑
为了清楚起见,扩展方法不是我自己的。它们来自 Owin 3rd 方库。所以没有重命名扩展方法的选项。
【问题讨论】:
-
如果您在
app.Use(Async ...中使用Func而不是Sub,会有所不同吗?目前该方法的签名与框架中的签名不匹配。 -
我不相信,因为据我所知,它不应该返回一个值。我的意思是使用
Function会引发错误,因为它期待返回值。 -
国际海事组织应该。为了使用覆盖的方法,签名必须匹配。看看definition。如果不是,编译器应该如何知道您想准确调用此方法?目前 Sub 它被视为一个参数(因此使用第一个方法定义)。您现在如何(您可以验证)在 C# 版本中,它使用 Func 参数调用该方法吗?
-
对不起,我在写答案时把你的代码块误解为扩展方法。如果将 Owin 库编译为 DLL,那么我相信扩展方法也应该在 VB.NET 中工作,但我不确定。