类';WebAPI.MyMiddlewareComponent';没有接受2个参数的构造函数

本文关键字:2个 参数 构造函数 WebAPI MyMiddlewareComponent | 更新日期: 2023-09-27 18:05:16

我正试图在本文之后逐步实现OWIN应用程序的自托管。在"使用配置对象配置中间件"部分之前,我已经完成了所有的示例,但在对该部分的示例进行编码时,我遇到了错误。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
// Add the Owin Usings:
using Owin;
using Microsoft.Owin.Hosting;
using Microsoft.Owin;

namespace WebAPI
{
    // use an alias for the OWIN AppFunc:
    using AppFunc = Func<IDictionary<string, object>, Task>;
    class Program
    {
        static void Main(string[] args)
        {
            WebApp.Start<Startup>("http://localhost:8080");
            Console.WriteLine("Server Started; Press enter to Quit");
            Console.ReadLine();
        }
    }
    public class MyMiddlewareConfigOptions
    {
        string _greetingTextFormat = "{0} from {1}{2}";
        public MyMiddlewareConfigOptions(string greeting, string greeter)
        {
            GreetingText = greeting;
            Greeter = greeter;
            Date = DateTime.Now;
        }
        public string GreetingText { get; set; }
        public string Greeter { get; set; }
        public DateTime Date { get; set; }
        public bool IncludeDate { get; set; }
        public string GetGreeting()
        {
            string DateText = "";
            if (IncludeDate)
            {
                DateText = string.Format(" on {0}", Date.ToShortDateString());
            }
            return string.Format(_greetingTextFormat, GreetingText, Greeter, DateText);
        }
    }
    public static class AppBuilderExtensions
    {
        public static void UseMyMiddleware(this IAppBuilder app, MyMiddlewareConfigOptions configOptions)
        {
            app.Use<MyMiddlewareComponent>(configOptions);
        }
        public static void UseMyOtherMiddleware(this IAppBuilder app)
        {
            app.Use<MyOtherMiddlewareComponent>();
        }
    }
    public class MyMiddlewareComponent
    {
        AppFunc _next;
        // Add a member to hold the greeting:
        string _greeting;
        public MyMiddlewareComponent(AppFunc next, string greeting)
        {
            _next = next;
            _greeting = greeting;
        }
        public async Task Invoke(IDictionary<string, object> environment)
        {
            IOwinContext context = new OwinContext(environment);
            // Insert the _greeting into the display text:
            await context.Response.WriteAsync(string.Format("<h1>{0}</h1>", _greeting));
            await _next.Invoke(environment);
        }
    }
    public class MyOtherMiddlewareComponent
    {
        AppFunc _next;
        public MyOtherMiddlewareComponent(AppFunc next)
        {
            _next = next;
        }
        public async Task Invoke(IDictionary<string, object> environment)
        {
            IOwinContext context = new OwinContext(environment);
            await context.Response.WriteAsync("<h1>Hello from My Second Middleware</h1>");
            await _next.Invoke(environment);
        }
    }

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Set up the configuration options:
            var options = new MyMiddlewareConfigOptions("Greetings!", "John");
            options.IncludeDate = true;
            // Pass options along in call to extension method:
            //app.UseMyMiddleware(options);
            app.Use<MyMiddlewareComponent>(options);           
            app.UseMyOtherMiddleware();
        }
    }
}

类"WebAPI.MyMiddlewareComponent"没有接受2个参数的构造函数。

当CCD_ 1正在呼叫时。如果我使用一些字符串而不是MyMiddlewareConfigOptions:

app.Use<MyMiddlewareComponent>("somestring");  

它是有效的。Owin软件包的版本为3.0.1.0,.NET Framework-4.5。

为什么会发生这种事?

类';WebAPI.MyMiddlewareComponent';没有接受2个参数的构造函数

哦,我想明白了。。。这是文章的错误:MyMiddlewareComponent类中的这一部分

public MyMiddlewareComponent(AppFunc next, string greeting)
{
    _next = next;
    _greeting = greeting;
}

应该被取代

public MyMiddlewareComponent(AppFunc next, MyMiddlewareConfigOptions options)
{
    _next = next;
    _greeting = options.GetGreeting();
}

现在它起作用了。