HostingEnvironment.QueueBackgroundWorkItem with ASP.NET Web API using OWIN - asp.net-web-api

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!"));

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.

Can you use WcfTestClient against WebApi?

This article suggests it was possible, or in the works, with some code that suggests it can be done, but I can't figure out what code needs to happen or the WcfTestClient's uri needs to be.
Here's the code from the article that makes me think I can do it:
// Metadata routes to support $metadata and code generation in the WCF Data Service client.
configuration.Routes.MapHttpRoute(
ODataRouteNames.Metadata,
"$metadata",
new { Controller = "ODataMetadata", Action = "GetMetadata" }
);
Is this feature implemented?
No, it does not work as you intend. WCF Test Client supports talking to SOAP-based services. OData is not supported in the current version.
Granted, as #Snixtor mentioned, you could create a SOAP service using ASP.NET Web API, including support for metadata (WSDL). But I really don't know of any good reason why anyone would want to do that.

webapi batch requests

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.

ASP.NET MVC3 HttpApplication doesn't fire events like BeginRequest, EndRequest, PostAcquireRequestState

ASP.NET MVC3 HttpApplication doesn't fire events like BeginRequest, EndRequest, PostAcquireRequestState etc. So it works fine but it doesn't fire events! I have tried to reinstall asp.net via aspnet_regiis - but there was no luck. Also i have tried to put events to Application_Start the same result.
I published web site via Web Deploy.
The interesting thing is - i have created new MVC project for testing purposes, deploy it and it works - HttpApplication events work perfectly.
Configuration:
WINDOWS 2008R2, IIS 7.5, ASP.NET MVC 3, all updates were installed.
Any help will be appreciated. Thanks in advance!
Example of subscribtion to HttpApplication events:
public MvcApplication()
{
EndRequest += MvcApplication_EndRequest;
PostAcquireRequestState += MvcApplication_PostAcquireRequestState;
PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
}
There is a pretty good article on this: http://forums.asp.net/p/1306960/2572147.aspx#2572147
It should be a better solution than turning on managing of all requests by managed handlers. The latter is not very interesting for performance.
As you saw from my comments i found a solution, but it's not complete solution, because it should work with runAllManagedModulesForAllRequests="false", probably because i don't want, that static content will be involved in integrated pipeline - it's performance cost effective!
So i found real solution thanks to Kev: Prevent IIS from serving static files through ASP.NET pipeline

Oracle CRM On-Demand - Use Tasks Status to update Service Request Status

We're using CRM On-Demand for our Service Group and I'm running into an application limitation and am wondering if anyone has a workaround or just some general ideas on how to accomplish our goal.
In the application, our major focus is around the Service Request and driving for users to create Tasks for all Activities related to working towards closure. For example, a customer calls in and we need a technical resource to make a return call to diagnose the issue in detail, so a Task is assigned to that resource. Once that Task has been marked as completed, I'd like the Status to be updated. I tried creating a workflow using JoinFieldValue(), which wasn't working. I tried a more basic approach and tried to just have a field on the Service Request be populated with the Status of the Task, but that did not work either.
Upon further investigation in the Help File, there is a relationship from the Activity object to the Service Request object, but not one the other way.
So, has anyone else run into this limitation and found some other method to have a Status change on the Task update the Status of a Service Request?
(Also, I'd like to try and avoid writing a custom web service for this purpose, which is why I'm trying to use the tools in the app)
Thanks in advance for any ideas!
actually, if I well understood your issue is related on workflow cross object.
OCOD doesn't manage this type of workflow when you need to use workaround.
In order to cover a cross object worklflow you have many possibilities:
webservices as you said, but you could imagine a js code that will run WS and hosted directly into OCOD (in R19 you could hoste that in Client Side extension). That could be a good solution
Another one could be using Report with a custom look up functionnality with the usage of "Callback" function
I would prefer the 1st solution.

Resources