I am following this article for understanding OWIN and with respect to points 3 and 4 in there would want to know how to pass a parameter to the Logging constructor so I can pass it in to the owin framework as shown in point #4.
我正在关注本文以理解OWIN,关于第3点和第4点,我们想知道如何将参数传递给Logging构造函数,以便将其传递给owin框架,如第4点所示。
I have created the AppFunc alias using this line of code:
我使用以下代码行创建了AppFunc别名:
using AppFunc = Func<IDictionary<string, object>, Task>;
What is that expression on the right of the equal sign even called?
甚至称为等号右边的那个表达是什么?
So now my constructor is like this
所以现在我的构造函数是这样的
public LoggingMiddleware(AppFunc next, MyDependency dependency)
{
}
now how do I create the object?
现在我该如何创建对象?
In Point #3, it says I can add a dependency object to the constructor and gives an example of how to register it using the type name. In point #4 in the article it says that you can also create an instance in advance and pass it in when registering but then it doesn't explain how to create the constructor. I tried this and did not work
在Point#3中,它说我可以向构造函数添加一个依赖项对象,并给出一个如何使用类型名称注册它的示例。在文章的第4点中,它说你也可以提前创建一个实例并在注册时传递它,但是它没有解释如何创建构造函数。我试过这个并没有用
LoggingMiddleware lmw = new LoggingMiddleware(new Func<"","">(),depObj);
I want to create a constructor of LoggingMiddleware so I can register it to the OWIN framework using this line
我想创建一个LoggingMiddleware的构造函数,这样我就可以使用这一行将它注册到OWIN框架
var logger = new LoggingMiddleware(????);
app.Use(logger);
1 个解决方案
#1
The AppFunc
is provided by the framework, so you don't create it.
AppFunc由框架提供,因此您不需要创建它。
Per part 3, if you want to use a type, the AppFunc
is injected into the constructor.
根据第3部分,如果要使用类型,则将AppFunc注入构造函数。
Per part 4, if you want to use an instance you don't inject the AppFunc
as a dependency, you have another Initialize
method.
根据第4部分,如果要使用实例,则不要将AppFunc作为依赖项注入,而是使用另一个Initialize方法。
You could probably combine the two:
你可以将两者结合起来:
public class LoggingMiddleware
{
private AppFunc next;
public LoggingMiddleware(AppFunc next, MyDependency dependency)
{
this.next = next;
}
public void Initialize(AppFunc next)
{
this.next = next;
}
public async Task Invoke(IDictionary<string, object> environment)
{
await next.Invoke(environment);
}
}
And use as a type like so:
并使用如下类型:
app.Use(typeof(LoggingMiddleware), depObj);
And use an an instance like so:
并使用像这样的实例:
var logger = new LoggingMiddleware(null, depObj);
app.Use(logger);
#1
The AppFunc
is provided by the framework, so you don't create it.
AppFunc由框架提供,因此您不需要创建它。
Per part 3, if you want to use a type, the AppFunc
is injected into the constructor.
根据第3部分,如果要使用类型,则将AppFunc注入构造函数。
Per part 4, if you want to use an instance you don't inject the AppFunc
as a dependency, you have another Initialize
method.
根据第4部分,如果要使用实例,则不要将AppFunc作为依赖项注入,而是使用另一个Initialize方法。
You could probably combine the two:
你可以将两者结合起来:
public class LoggingMiddleware
{
private AppFunc next;
public LoggingMiddleware(AppFunc next, MyDependency dependency)
{
this.next = next;
}
public void Initialize(AppFunc next)
{
this.next = next;
}
public async Task Invoke(IDictionary<string, object> environment)
{
await next.Invoke(environment);
}
}
And use as a type like so:
并使用如下类型:
app.Use(typeof(LoggingMiddleware), depObj);
And use an an instance like so:
并使用像这样的实例:
var logger = new LoggingMiddleware(null, depObj);
app.Use(logger);