webapi batch requests - asp.net-web-api

I am looking for a way to batch requests to the server. I found a post by Brad Wilson outlining how to make a batch handler using a message hanlder http://bradwilson.typepad.com/blog/2012/06/batching-handler-for-web-api.html#more but I wasn't able to get this working.
first I had compile errors because webapi did not understand "route-specific endpoint handler" like Brad's example used. there were also problems with the media type and/or formatter (can't remember which). My next attempt was to make a batch controller. so instead of a batch handler I had a batch controller. I almost has this working except when I used the MessageHandlerInvoker to call the individual commands I got exceptions about the additional handlers I have regsstered (1 for logging request/response and another to mimic user authentication).
At that point I stopped and reverted back to individual requests, not ideal, but it works.
My environment:
.net 4.0
VS 2010
mvc 4 front end (calls webapi)
webapi as a service tier
Has anyone else had any success with batched messages and webapi?

To be able to use per-route handlers you need ASP.NET Web API RTM which was only released yesterday (at the time when Brad wrote the article, it would only work with nightly MyGet feed builds or against Codeplex source).
You can get entire MVC4 RTM here or simply off Nuget.

Related

NHibernate + ASP.NET Identity + Autofac cache issue

I'm building a profile page with update form. After submitting the form with new data and several page refreshes I see sometime new and sometimes old data. It depends on thread handling current request. One thread contains new data and another one old. NHibernate is configured using ThreadStaticSessionContext and NoCacheProvider. In Autofac UserStore, OwinContext.Authentication and UserManager are configured as InstancePerRequest.
I tried to change ThreadStaticSessionContext to CallSessionContext and it started working normally. So the question is: why it works(ThreadStaticSessionContext is preferable for multithread apps) and what negative effects can it bring?
Thanks!
ThreadStaticSessionContext is for long running processes such as windows services or windows apps. For web applications you want to be implementing Session Per Request. This is what the WebSessionContext is for.
I actually don't use any of the contexts and just wire it up myself. See my answer here for an example.

HostingEnvironment.QueueBackgroundWorkItem with ASP.NET Web API using OWIN

I'm looking to leverage the QueueBackgroundWorkItem feature in available in .NET 4.5.2+ on our ASP.NET Web API application, however, I consistently am getting "Invalid Operation" exceptions when attempting to put something into the queue. I believe this may be related to the fact that our app is self-hosted using OWIN and the queueing of the work item is an IIS only feature? If this is true, just wanted to see if there would be any suggestions at all in regards to queueing background work from an owin-self hosted API.
HostingEnvironment.QueueBackgroundWorkItem() is part of System.Web and as a result, it will only work in the classic ASP.NET pipeline. It won't work in self-hosted OWIN pipeline.
Hangfire is your only option to run long background tasks
I had this issue in a test environment answered here.
In the end, I used Hangfire and its fire and forget code
//Fire-and-forget tasks
// Static methods are for demo purposes
BackgroundJob.Enqueue(
() => Console.WriteLine("Simple!"));

How to use IAppBuilder.CreatePerOwinContext in a self-hosted 2.2 WebApi

I have a working self-hosted OWIN based 2.2 WebApi project. I wanted to put authorization into it and share the identity service with my MVC 5 website. (There a numerous opinions on if or how easy doing so may be.) My attempt to share the authorization/authentication cookie has lead me into a hole. I discovered that the WebApi code was not seeing/processing the cookie even though it was in the request header. I then decided to try a test in which I would generate the cookie in the WebApi on one call and accept it on another. That attempt lead me to the fact that CreatePerOwinContext is not working as I thought it should.
I placed the following code in a method to generate the cookie.
var ctx = Request.GetOwinContext();
var asim = ctx.Get<ApplicationSignInManager>();
asim.SignInAsync(user, false, false);
and discovered that the Get was returning null. I then investigated the code in my startup
appBuilder.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
I placed a breakpoint on ApplicationSigInManger.Create and never reached it. I placed a breakpoint at the same location in my MVC 5 code and did hit the breakpoint. Yes, I did verify that I am reaching and continuing past the call to CreatePerOwinContext. The lack of hitting the breakpoint has lead me to conclude that the call to CreatePerOwinContext is not working the same in WebApi as in MVC 5.
Both the MVC project and the WebApi project are using .Net 4.6 release code and the latest versions of their respective NuGet packages.

How do I show progress of a long-running server operation (Web API Commanding) in the Lightswitch HTML client?

I have a VS 2013 Lightswitch HTML Client application to which I've added a button that makes a Web API REST post. This basically 'refreshes' the data in the table from the original upstream source. This is all working correctly, but the operation takes a few minutes, and I want to report status to the user as it runs.
Right now, I've tried attaching a simple Refresh when the post returns as follows:
$.post("/api/data/", "Refresh", function (response) {
screen.getData().then(function (newData) { screen.reQuery(); });
});
This doesn't actually seem to do a refresh (screen.reQuery is apparently the wrong call), but the better option would be to instead have the server show progress of this long-running application.
One thought I had would be to have the server call return data in the form of "percent done" in the response as it processes it, but I don't know if this would be delivered to the client piecemeal, nor the best way to display this to the user in Lightswitch.
I'm open to other third-party libraries that might help with this, but I'd like to stick with WebAPI for commanding instead of adding something like SignalR for now, if possible. Thanks!
In general this seems like not the best idea to run operations that takes minutes on the server.
A reasonable alternative is to create a single call, that will in turn create multiple Web Jobs (see Azure Web Jobs for more info). The Web Jobs will be broken to smaller individual tasks, and your html will query the web jobs rather than your Web API.

ASP.NET MVC 3: Purpose of IgnoreRoute("{resource}.axd/{*pathInfo}"); ? deprecated?

Do I even need this rule anymore?
I don't see any requests incoming for resource.axd files (as opposed to when I ran webform applications)
WebResource.axd is an HTTP Handler that is part of the .NET Framework
that does one thing and one thing only – it is tasked with getting an
embedded resource out of a DLL and returning its content. What DLL to
go to and what embedded resource to take are specified through the
querystring. For instance, a request to
www.yoursite.com/WebResource.axd?d=EqSMS…&t=63421… might return a
particular snippet of JavaScript embedded in a particular assembly.
Its still part of the framework and you can still retrieve embedded resources using the above handler. You dont want your route handler to handle such requests and that is why it is ignored. My guess is that you can get rid of it if you are completely sure that your app/libraries that you use dont use it.

Resources