ConfigureAwait(false) in .Net Core application - asynchronous

I am creating a cron job using .Net Core which will fetch data from API and insert data into database.
Should I use ConfigureAwait(false) while calling api in asynchronous mode?
I am confused after reading article - ConfigureAwait(false) relevant in ASP.NET Core?
Since I am having console app not a UI app so Please suggest Should be go with ConfigureAwait(false) or not

In short: if you are asking, then you don't need to use ConfigureAwait(false) in your application.
.NET Core framework has no SynchronizationContext. So in this contextless approach from .NET Core, the default asynchronous behavior is the same as we have, when using ConfigureAwait(false), so when an async handler resumes execution, a thread is taken from the thread pool and goes the work.
Source: https://itnext.io/a-deep-dive-into-configureawait-65f52b9605c2

Related

HttpWebRequest BeginRead issue on .net core 3.1 / .net core 2.1

I am moving my class library helper for HttpWebRequest (standard .net class) from .NET 4.6.2 to .NET core 3.1.
And I discovered the following issue with .NET core 3.1:
When I call responseStream.BeginRead it calls via the Stack!
So it is the reason of issue: when stack ends application fails due to stack overflow error. šŸ˜‚
.NET 4.6.2 behavior is different - it calls BeginRead on Thread Pool.
And it works right for years without any issue.
I cannot find the reason why .NET core 3.1
has different behavior for BeginRead.
If you know how to fix HttpWebRequest BeginRead or any ideas share with me, please.
It is standart behaviour for .net core and your code has to be ready for that.
You have to analyse in your code
ReadIAsyncResult.CompletedSynchronously == true/false
and decide what to do this that
if CompletedSynchronously == true your have to return from the function.
Check docs
https://learn.microsoft.com/en-us/dotnet/api/system.iasyncresult.completedsynchronously?view=net-7.0
Notes to Implementers
Most implementers of the IAsyncResult interface will not use this property and should return false. Beginning with the .NET Framework 4.5, a task that is created with the FromAsync(IAsyncResult, Action, TaskCreationOptions) method will not complete if this property is not implemented correctly. See Application Compatibility in 4.5.
Notes to Callers
Use this property to determine if the asynchronous operation completed synchronously. For example, this property can return true for an asynchronous I/O operation if the I/O request was small.
Find more details here
https://github.com/microsoft/referencesource/issues/177

Hangfire with multiple projects in .net solution

I am looking to implement hangfire into my asp.net web api and asp.net MVC website projects.
The way I have structured my solution is as follows:
Solution - My Solution
1: Model - (Project containing Entity Framework Objects and classes)
2: Services (Where I implement all my> business logic, changes etc.) This is where I will most likely make use of HangFire.
3: Web API (my asp.net api project)
4: Web UI ( mvc 5 Admin interface website)
Both project 3 and 4 make use of the 2:Services project to do work and call services which execute business logic. This is where most tasks will be spun off.
How would I go about implementing hangfire, so that they respective iis sites can both make use of the same "instance" of hangfire. but it will obviously run on the associated app pools?
or maybe it cant work like that and I have to have it running in one place?
What are my options, and furthermore what is the recomendd approach?
The biggest take-away for me was that HangFire will not continue past a work pool shutdown (i.e., idle timeout), which is my core problem anyway, and recommends altering the server configuration to never shut down work pools. If your app is going to be in constant use 24/7, then this shouldn't be an issue for you although your work pool could still be recycled for various reasons, but for an app that will experience peaks and troughs in users then you may want to consider an out-of-process HangFire server.
The approach I'm taking is the later. I'm building a proof-of-concept that has a Windows service (built using Topshelf - highly recommended for this) that hosts the HangFire server (and dashboard), a shared core library, and a client (which will be my WebAPI in production, but is a WPF app for the PoC). The client enqueues a job using a class instance from the shared library, which the HangFire server also has access to.
I'm assuming from your description that the WebAPI controller actions call corresponding methods in class from the service layer? If this is the case, then I would opt for a similar solution to mine, with the HangFire Windows service having access to your services and models as required.
If your app is going to be heavily trafficked and work pool recycles don't bother you, then I'd host the HangFire server in your WebAPI directly.

Why is HTTPContext.Current.Session null using SignalR 2.x libraries in a ASP .Net MVC application?

I'm attempting to migrate our ASP.Net MVC application from using Signal R1.x tgo SignalR 2.x. I just found issue which will certainly cause us problems in our quest to move forward.
Our web application is MVC based and makes heavy use of the HttpContext.Current.Session variable.
When running with SignalR 1.x, everything is fine and dandy with Session.
When we upgraded to SignalR 2.x, Session was suddenly null.
I did a little googling and found the following links regarding the issue:
HTTPContext.Current.Session is nul in SignalR...need alternate to Session state while using SignalR
Further investigation revealed the following tidbit of information:
http://www.asp.net/signalr/overview/signalr-20/troubleshooting-and-debugging/troubleshooting
HTTPContext.Current.Session is null
This behavior is by design. SignalR does not support the ASP.NET session state,
since enabling the session state would break duplex messaging.
I would imagine that this will break a great many ASP .Net MVC based applications.
Is there any workaround for this?
Is this truly by design, or is it a result of using oWin::>Startup::Configuration(){app.MapSignalR();} to initiate signalR in 2.x?
If possible, Iā€™d like to figure this out and if the solution is not so painful, they will still consider moving to 2.x.
Thoughts?
Session state is not supported from within SignalR as it interferes with the processing of simultaneous requests from the same user, and is not supported from WebSockets requests. In 2.0 the property is null because SignalR requests are handled before the session state module is initialized. This was a change from 1.0 in how SignalR is hosted inside of System.Web based ASP.NET applications (in 1.0 it used a route in the ASP.NET routes table, in 2.0 it uses the OWIN hosting module provided by Microsoft.Owin.Host.SystemWeb).
Note this only affects the use of session from within your SignalR classes (Hubs, etc.), not the rest of your application.

how to write a client application for monotorrent with asp.net

I'm trying to write a client app using monotorrent library with asp.net.
there is a clientapp sample provided by monotorrent but since I'm using asp.net I need to know how to keep the engine running all the time and get feedback, like download rate and ..
I tried to use the Task class provided in .NET 4, but the thread keeps shutting down randomly ( I think of course ).
Is there a way that I can keep the thread which engine is running in, always working?
or any idea on where to look for implementing the client in web app?
Thats impossible, as the ASP.NET worker process is recycled from time to time. What do you need is a desktop application or a windows service. And then build an WCF service to communicate with you ASP.net application.

Using .NET 4.0 Task in ASP.NET

Are there any caveats or short comings to using the new Task API in System.Threading.Task in ASP.NET hosted under IIS?
I know prior to .NET 4.0 working with any of the ThreadPool actions inside of IIS was always recommended to be avoided.
Any caveats to using ThreadPool with IIS would still be valid using System.Threading.Task, as the Task API is just an additional layer of abstraction over System.Threading.
For long-running tasks in the background, I use a ThreadPool inside a Windows Service. This keeps it outside of IIS.

Resources