owin - No conversion available between HelloWorldComponent and System.Func`2[System.Collections.Generic.IDictionary`2 // Parameter name: signature -
i working through scott allen's mvc 5 fundamentals course on pluralsight
i error @ "using (webapp.start(uri)) " in code below.
the error
an unhandled exception of type 'system.argumentexception' occurred in microsoft.owin.dll system.argumentexception unhandled hresult=-2147024809 message=no conversion available between consoleapplication1.helloworldcomponent , system.func`2[system.collections.generic.idictionary`2[system.string,system.object],system.threading.tasks.task]. parameter name: signature source=microsoft.owin paramname=signature stacktrace: @ microsoft.owin.builder.appbuilder.convert(type signature, object app) @ microsoft.owin.builder.appbuilder.buildinternal(type signature) @ microsoft.owin.builder.appbuilder.build(type returntype) @ microsoft.owin.hosting.serverfactory.serverfactoryadapter.create(iappbuilder builder) @ microsoft.owin.hosting.engine.hostingengine.startserver(startcontext context) @ microsoft.owin.hosting.engine.hostingengine.start(startcontext context) @ microsoft.owin.hosting.starter.directhostingstarter.start(startoptions options) @ microsoft.owin.hosting.starter.hostingstarter.start(startoptions options) @ microsoft.owin.hosting.webapp.startimplementation(iserviceprovider services, startoptions options) @ microsoft.owin.hosting.webapp.start(startoptions options) @ microsoft.owin.hosting.webapp.start[tstartup](startoptions options) @ microsoft.owin.hosting.webapp.start[tstartup](string url) @ consoleapplication1.program.main(string[] args) in e:\eshared\dev2015\webappscottallen\consoleapplication1\consoleapplication1\program.cs:line 16 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.threadhelper.threadstart_context(object state) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception:
the code is
using system; using system.collections.generic; using system.io; using system.threading.tasks; using microsoft.owin.hosting; using owin; namespace consoleapplication1 { using appfunc = func<idictionary<string, object>, task>; class program { static void main(string[] args) { string uri = "http://localhost:8080"; using (webapp.start<startup>(uri)) // katana please start, using configuration startup class , listening on port given uri { console.writeline("started!"); console.readkey(); console.writeline("stopping!"); } } } public class startup { public void configuration(iappbuilder app) { app.use<helloworldcomponent>(); } } public class helloworldcomponent { appfunc _next; public helloworldcomponent(appfunc next) { _next = next; } // katana uses reflection find invoke function matches appfunc signature public task invoke(idictionary<string, object> environment) { var response = environment["owin.responsebody"] stream; using (var writer = new streamwriter(response)) { return writer.writeasync("hello"); } } } }
packages.config is
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="microsoft.owin" version="3.0.1" targetframework="net451" /> <package id="microsoft.owin.diagnostics" version="3.0.1" targetframework="net451" /> <package id="microsoft.owin.host.httplistener" version="3.0.1" targetframework="net451" /> <package id="microsoft.owin.host.systemweb" version="3.0.1" targetframework="net451" /> <package id="microsoft.owin.hosting" version="3.0.1" targetframework="net451" /> <package id="owin" version="1.0" targetframework="net451" /> </packages>
i wonder did not use message, wonder have changed
there new way write our middlewares components, looks this:
public class helloworldcomponent : owinmiddleware { public helloworldcomponent(owinmiddleware next) : base(next) { } public override task invoke(iowincontext context) { return context.response.writeasync("hello, world!"); } }
specifically, the constructor must accept owinmiddleware
reference first parameter, otherwise error because ctor signature not match expected current owin implementation.
further, consider following parameterized usage:
var param1 = "hello, world!"; appbuilder.use<helloworldcomponent>(param1)
to support want modified constructor signature:
public class helloworldcomponent : owinmiddleware { public helloworldcomponent(owinmiddleware next) : base(next) { } public override task invoke(iowincontext context, string param1) { return context.response.writeasync(param1); } }
thus, allowing parameterize our middleware via use()
's params array.
Comments
Post a Comment