mercredi 29 juillet 2015

The class 'WebAPI.MyMiddlewareComponent' does not have a constructor taking 2 arguments

I am trying to implement step-by-step selfhosting OWIN application following this article. I've done all examples before 'Use Configuration Objects for Configuring Middleware' section, but during coding the example from that section i've got error. Here my code:

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();
        }
    }
}

The class 'WebAPI.MyMiddlewareComponent' does not have a constructor taking 2 arguments.

when app.Use<MyMiddlewareComponent>(options); is calling. If i use some string instead of MyMiddlewareConfigOptions:

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

it works. Version of Owin package is 3.0.1.0, .NET Framework - 4.5.

Why it is happening?




Aucun commentaire:

Enregistrer un commentaire