班级' WebAPI.MyMiddlewareComponent'没有带2个参数的构造函数

时间:2015-07-29 13:34:35

标签: c# web asp.net

我正在尝试在this article之后实施逐步自托管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();
        }
    }
}
  

班级&#39; WebAPI.MyMiddlewareComponent&#39;没有带有2个参数的构造函数。

app.Use<MyMiddlewareComponent>(options);正在呼叫时

如果我使用一些字符串而不是MyMiddlewareConfigOptions

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

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

为什么会这样?

1 个答案:

答案 0 :(得分:3)

哦,我明白了......这是文章的错误:MyMiddlewareComponent班的这一部分

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

应替换为

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

现在可行。