Skip to content

Instantly share code, notes, and snippets.

View darrelmiller's full-sized avatar

Darrel darrelmiller

View GitHub Profile
[Fact]
public async Task DispatchBasedOnStatusCodeMediaTypeAndProfile()
{
Person testPerson = null;
var machine = new HttpResponseMachine();
// Define method to translate response body into DOM for specified media type
machine.AddMediaTypeParser<JToken>("application/json", async (content) =>
{
class Program
{
static void Main(string[] args)
{
ServicePointManager.DefaultConnectionLimit = 2;
var tasks = new Task[10];
for (int i = 0; i < 10; i++)
{
tasks[i] = Run();
public class RunscopePassagewayHandler : DelegatingHandler {
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
if (request.RequestUri.Host.Contains("passageway.io")) {
var uriWithoutPort = request.RequestUri.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped);
request.RequestUri = new Uri(uriWithoutPort);
}
return base.SendAsync(request, cancellationToken);
}
}
1) Remove all nuget packages with any relationship to the package having a problem
2) Ensure that no project still has a DLL reference to assemblies from those nuget packages
3) Remove all binding redirects related to these nuget packages
4) Add nuget packages, one at a time, in order of dependencies. (i.e. Don't let any nuget package automatically pull in another)
5) Add binding redirects for anything that still has a problem
6) Enjoy the build successful
public static class TaskHelper
{
public static Task<T> RunWithCancel<T>(Func<CancellationToken,T> function, CancellationToken token)
{
return Task.Run(() => function(token),token);
}
}
public class NullJsonHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
if (response.Content == null)
{
response.Content = new StringContent("{}");
@darrelmiller
darrelmiller / gist:10682188
Created April 14, 2014 20:56
Create an AppFunc for a WebApi application
public static Func<IDictionary<string, object>, Task> CreateWebApiAppFunc(HttpConfiguration config)
{
var app = new HttpServer(config);
var options = new HttpMessageHandlerOptions()
{
MessageHandler = app,
BufferPolicySelector = new OwinBufferPolicySelector(),
ExceptionLogger = new WebApiExceptionLogger(),
ExceptionHandler = new WebApiExceptionHandler()
};
@darrelmiller
darrelmiller / gist:10682407
Created April 14, 2014 21:00
Create Owin host from HttpListener
public static IDisposable CreateHttpListenerServer(Uri baseAddress, Func<IDictionary<string, object>, Task> appFunc)
{
var props = new Dictionary<string, object>();
var address = Address.Create();
address.Host = baseAddress.Host;
address.Port = baseAddress.Port.ToString();
address.Scheme = baseAddress.Scheme;
address.Path = baseAddress.AbsolutePath;
public class JsonStringController : ApiController
{
public HttpResponseMessage Post([FromBody]string jsonBody)
{
var myobject = Newtonsoft.Json.JsonConvert.DeserializeObject<MyObject>(jsonBody)
return new HttpResponseMessage(HttpStatusCode.Created);
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
var webApiconfig = new HttpConfiguration();
webApiconfig.Routes.MapHttpRoute("test","Test",new {controller="Test"});
app.UseWebApi(webApiconfig);
// Turn cross domain on
var config = new HubConfiguration { EnableCrossDomain = true };