Search code examples
c#restlambdacompiler-errorsnancy

Lambda Error in Nancy Sample Application


I'm somewhat baffled by an error I am receiving while trying to get the simple Nancy example program working.

namespace NancyServer {

    using Nancy;

    public class ServerApi : NancyModule {
        public ServerApi() {
            Get["/"] = _ => "Hello World";
        }
    }

}

Visual studio 2013 pro balks at the lambda in my route.

_ => "Hello World";

I also tried a few other expressions and they all have the same problem.

Get["/products/{id}"] = _ => {
    System.Console.WriteLine( "test" );
};

The above snippet was also pulled directly from the Nancy wiki.

Intellisense underlines the lambda assignment _ => and says that the delegate does not take one argument. If I try to build I get three errors.

Error 1 Delegate 'System.Func<dynamic,System.Threading.CancellationToken,System.Threading.Tasks.Task<dynamic>>' does not take 1 arguments
Error 2 Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<dynamic>'
Error 3 Cannot convert lambda expression to delegate type 'System.Func<dynamic,System.Threading.CancellationToken,System.Threading.Tasks.Task<dynamic>>' because some of the return types in the block are not implicitly convertible to the delegate return type

I have googled for a few hours now and cannot figure out what is going on here. Is this an environment error? Could there be some kind of namespacing issue going on? I have added Nancy and Nancy.Hosting.Self as dependencies to my project. The only other code in the project is a simple main to self host the service.

namespace NancyServer {

    using System;
    using Nancy.Hosting.Self;

    public class ServerMain {
        static void Main( string[] args ) {
            var nancyHost = new NancyHost( new Uri( "http://localhost:25000" ) );
            nancyHost.Start();
            Console.WriteLine( "Server running..." );
            Console.ReadKey();
            nancyHost.Stop();
        }
    }

}

I added Nancy and Nancy.Hosting.Self directly through Nuget in VS2013 pro.


Solution

  • Change it like so : Get["/"] = (params,token) => Task.FromResult<dynamic>("Hello World");

    Nancy v2.0.0 is async all the way now.