Last active
December 10, 2015 11:48
-
-
Save thecodejunkie/4429702 to your computer and use it in GitHub Desktop.
Async requiring you return Task/Task<T>/void =( Trying to make Get[] work for both Async and non-async, but since using the async keyword requires you to return void/Task/Task<T> and since you cannot overload based on return type.. we're screwed. You cannot even return a type, which can be implicitly cast to Task/Task<T> because you'll get a com…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Xunit; | |
public class Fixture | |
{ | |
[Fact] | |
public void Should_FactMethodName() | |
{ | |
var module = new Class1(); | |
var result = module.routes["/"].Invoke(new object()); | |
var i = 10; | |
} | |
} | |
public class Class1 : Module | |
{ | |
public Class1() | |
{ | |
Get["/"] = x => | |
{ | |
return "non async"; | |
}; | |
Get["/stuff"] = async x => | |
{ | |
var client = new HttpClient(); | |
var result = await client.GetAsync("http://www.nancyfx.org"); | |
var content = await result.Content.ReadAsStringAsync(); | |
return content; | |
}; | |
} | |
} | |
public class Module | |
{ | |
public readonly IDictionary<string, Func<dynamic, dynamic>> routes = new Dictionary<string, Func<dynamic, dynamic>>(); | |
public Builder Get | |
{ | |
get { return new Builder(this); } | |
} | |
public class Builder | |
{ | |
private readonly Module module; | |
public Builder(Module module) | |
{ | |
this.module = module; | |
} | |
//public Func<dynamic, dynamic> this[string path] | |
//{ | |
// set { this.module.routes.Add(path, value); } | |
//} | |
public Func<dynamic, Task<dynamic>> this[string path] | |
{ | |
set { this.module.routes.Add(path, value); } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about forcing return type as
Task<dynamic>
. This is a huge breaking change though. but might be worth it.Task.FromResult
is only present in .net 4.5+, so adding a helper method in NancyModule could be good.ToTask
is a helper method.