QueueUserWorkItem in ASP.Net

QueueUserWorkItem in ASP.Net

http://msdn.microsoft.com/en-us/library/windows/desktop/ms684957(v=vs.85).aspx

It Queues a work item to a worker thread in the thread pool

This thread pool is the ASP.Net thread pool which is actually the CLR Thread Pool

Queues a method for execution. The method executes when a thread pool thread becomes available.

DON’T USE THIS AS YOU ARE TAKING AWAY THREADS THAT ASP.NET WOULD HAVE OTHERVBISE USED FOR REQUEST PROCESSING

 

Advertisements

How to Build ASP.NET Web Applications Using Async

How to Build ASP.NET Web Applications Using Async

Talk by Rowan Miller in TedEd 2013

SEE: http://view.officeapps.live.com/op/view.aspx?src=http%3a%2f%2fvideo.ch9.ms%2fsessions%2fteched%2fna%2f2013%2fDEV-B337.pptx

WEB FORMS

  1. Async is not Parallel
  2. For page load scenarios, don’t use async on Page_Load.Instead, inside it, create a RegisterAsyncTask (new PageAsyncTask (async() => {})); and put async code in there
  3. Note that all that a method marked async does is that it returns a Task<T> to the caller and then inside it it does the intrusive stuff (like IO, File Read etc) using await, meaning that it automatically makes it so that the thread is released when the intrusive stuff (marked with await) happens
  4. Then when the result comes back, a new thread continues the execution to the end of the method
  5. When consuming a Async method, either you can accept the task that it returns or you are free to await the response
  6. With asp,net you have to explicitly opt into the async page lifecycle by setting async = true
  7. Use HTTP Request instead of WebRequest API as it is completely Async
  8. SIMPLY: AWAIT FREES UP THE THREAD UNTIL THE OPERATION IS FINISHED
  9. ASYNC DOES NOT AFFECT THE CLIENT AT ALL
  10. ALWAYS USE RegisterAsyncTask TO CALL ASYNC CODE

WEB API

  1. JUST MARK THE METHOD AS ASYNC
  2. ASYNC DOES NOT AFFECT THE CALLER AT ALL
  3. EF 6 HAS ASYNC DB CALLS

CANCELLATION TOKENS

  1. ALLOW CALLER TO TERMINATE A..
  2. ASYNC DELAY: AWAIT TASK.DELAY(3000);
  3. USE ALL 3 TOKENS TOGETHER IN SINGLE CANCELLATIONTOKEN OBJECT

PARALLELISM

  1. Note that async and await are not about parallelism at all, but instead about freeing blocked threads
  2. The way we can do parallelism is to not do async Task1; await t1; async t2;await t2; as this is sequential operation.
  3. Instead kick off all the async calls one after the other and then simply wait all and then write the await code (without await keyword though)

OR Best create a List of Tasks : var tasks = new List<Task<string>>;

Then instead of await(whenAll), flush the response as soon as any task completes:

While(tasks.Any())

{

Var completed = await Task.WhenAny(tasks);

Tasks.Remove(completed);

If(!completed.IsFaulted)

Context.Response.Write(completed.Result);

Context.Response.Flush();

}

}

The last piece of optimization is to Flush asynchronously. But there is no FlushAsync method, so :

Await Task.FactoryFromAsync(context.Response.BeginFlush, context.ResponseEndFlush,null)

NOTE: For stuff like HTTPHandler, use HTTPTaskAsyncHandler instead of IHttpAsyncHandler as it is a higher level abstraction of the IHttpAsyncHandler

.Net uses the convention to name async methods as abcAsync and if abcAsync already exists, they call it abcTaskAsync. USE THE SAME CONVENTION

DON’T WORK ON BACKGROUND THREAD ON SERVERS AS THERE ARE NO BACKGROUND THREADS ON SERVERS…ALL THREADS COME FROM THE SAME POOL (IIS THREAD POOL) THAT IS USED TO SERVE REQUESTS

USE THAT MODEL IN DESKTOP THREADS

DON’T USE ASYNC AND AWAIT ON A BACKGROUND THREAD. USE IT ON THE REQUEST THREAD ONLY..BUT IF U ABSOLUTELY HAVE TO SPIN BG THREAD USE SYNCHRONIZATIONCONTEXT.POST

NEVER USE TASK.WAIT OR TASK.RESULT AS THEY ARE BLOCKING. USE AWAIT OR TASK.WHENALL INSTEAD

DON’T USE TASK.CONTINUE WITH AS IT IS REDUNDANT NOW THAT WE HAVE AWAIT AND ALSO IT SPINS NEW THREAD SO THE CONTEXT OF REQUEST IS LOST

 

Put context in HttpContext.Items..NOWHERE ELSE..Don;t use Thread Local Storage

FIRE AND FORGET: Use webBagrounder

 

ASP.NET 4.5 has not been registered on the Web server

—————————

Microsoft Visual Studio

—————————

ASP.NET 4.5 has not been registered on the Web server. You need to manually configure your Web server for ASP.NET 4.5 in order for your site to run correctly.

—————————

OK   Help

 

fixed this issue by running

aspnet_regiis -i

using the visual studio command line tools as an administrator

 

Configure Pluggable SOAP Extension

Please follow the below steps to configure the dll (SoapExtensionSampleDll.dll) in your web application:

1. Copy the compiled dll SoapExtensionSampleDll.dll  from the given solution to the bin directory of your web application

2. Add following configuration to your web.config :

1.  Register the dll :

<dependentAssembly>

<assemblyIdentity name=”SoapExtensionSampleDll” />

</dependentAssembly>

2.  Register the Soap Extension:

<soapExtensionTypes>

<add type=” SoapExtensionSampleDll.MySoapExtension, SoapExtensionSampleDll, Version=1.0.0.0, />

</soapExtensionTypes>

How To Migrate From Asmx To WCF

BEST: http://blogs.msdn.com/b/trobbins/archive/2006/12/02/integrating-wcf-with-your-existing-asmx-services.aspx

http://stackoverflow.com/questions/5686320/asmx-to-wcf-conversion

Simple Eg: http://stackoverflow.com/questions/12094256/moving-asmx-service-to-wcf-basic-binding-service

http://stackoverflow.com/questions/1502298/how-much-effort-is-required-to-convert-an-asmx-to-wcf-web-service

  1. To do Kerberos/NTLM with WCF, follow: http://social.msdn.microsoft.com/Forums/vstudio/en-US/0aac0110-187e-4a00-a597-f15b768cf16c/passing-client-credentials-to-wcf-service
  2. Set up Kerberos Authentication with Delegation on IIS 7 w/ Windows Server 2008