Call or schedule rest api from Hangfire scheduler - hangfire

I have a .net web api and controllers for example say i have a HomeController and post method that insert to table. I want to call the HomeController in HangFire scheduler with in the same api. The scheduler will run every hour. Is it possible to call the rest api (HomeController) post method by scheduling the Hangfire in the same api.
a. I have web api with HomeController with post method
b. I configured the hangfire in the web api (startup.cs) etc...
c. Now setup the recurring job!!!
RecurringJob.AddOrUpdate(() => http://localhost:58664/api/home, Cron.Hourly);
I can generate the values in for loop to post.
Is this possible? calling the rest api with in the same web api using hangfire.

create an instance of home controller and call the method in your loop
//Ex :
//oEmail = set values here
WebApiRole.SendController oController = new WebApiRole.SendController();
RecurringJob.AddOrUpdate(() => oController.SendEmail(oEMail), Cron.Hourly);//inside the loop
//SendController is the controller name and SendEmail is the method to which I am passing //oEmail as param

Related

ASP.NET Transfer data from controller action

There is a sales service implemented as a Telegram bot. I need to create a website control panel for this service. Since the service is a .NET application I am thinking to use ASP.NET Core technology.
How do I transfer data from the controller action to the Program class containing all the functionality of the service (maybe it is worth defining the Program as a static class)?
You may have misunderstood Asp.Net Core. .net core adopts the pipeline mode, that is, when you call the action in the controller, it will enter the middleware pipeline of Program.cs(.net 5 is Startup.cs), and execute in sequence according to the order of your middleware, adopting the principle of first in, last out. This means that if you follow the normal .net core logic, the value you get in the controller (except the parameters defined in the URL), you cannot pass it into Program.cs. When you successfully enter the action of the controller, Program.cs has been executed.
Not sure what your sales service looks like, but I think you can register it as a service and use it in your controllers using dependency injection.
Helpful link: ASP.NET Core Middleware.

DotNet Core 6 and ApiController with scopped services for Db Context need Async/await?

I am not sure if the Await/Asyn method is needed in API Controller with scoped service injection?
For example, I got a controller that returns a list of users. The controller action injects a userService then call a function. The userService then inject my DbContext and ask for users in the sql DB.
I see many demos without any Asycn/Await on the server side. I read somewhere that the gain of performance is minor in a scoped service task and if I got a very long task, only the scoped service will be blocked.
But I am not sure of that. Any advice on that?
My client side is angular and the HTTP post or Get are asynchronous, but I am wondering if I need to keep async all the way down the controller->Service to be sure of maximum performance.

Appropriate approach to track database changes in a .Net Core Web API and send corresponding push notification to client using SignalR

I have a project in .net core 5 Web API which is using EF Core as ORM. I would like to send notification to specific/all clients whenever any successful insert/update/delete is performed on specific tables in the database. This Web API project would be the only way to access the database.
To send push notification, I am using SignalR.
Due to performance related issues I do not intend to use trigger, change tracker etc mechanisms that are directly associated with the database.
Any action method in my web api controller would have the following flow -
EmployeeService : BaseService {
...
}
EmployeeController:BaseController {
private EmployeeService empService;
...
[HttpPost]
public Task<EmployeeDto> EditEmployee(Guid id, EmployeeUpdateDto model) {
....
// Here I call the corresponding method in EmployeeService which in turn calls appropriate
// Repository method
..
}
}
So in controller method , after the appropriate service call, I would be able to understand whether the table has been actually updated or not and accordingly I can call SignlaR to send push notification.
My query is whether this is the best approach to handle the scenario ?
And if it is the best approach then shall I use a ResultFilter or another service (that I inject in Every Controller ) to send the SignalR notification.
In case this approach does not seem to be an efficient one, please suggest how can I achieve the same in an alternate manner.

asp.net 3 core api - use of async in console hosted

I've been going through the aspnet core 3 Rest API tutorial here (Code below)...
https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio
I want to host this in a console app. My understanding is that this uses "Kestrel" as a web server.
Lets assume a request takes 10 seconds.
If two requests came in at the same time, what exactly would be different between two scenarios where one used Async Await Task , and one did not ( returned normal ActionResult) in my Console App?
[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
var todoItem = await _context.TodoItems.FindAsync(id);
if (todoItem == null)
{
return NotFound();
}
return todoItem;
}
If two requests came in at the same time, what exactly would be different between two scenarios where one used Async Await Task , and one did not ( returned normal ActionResult) in my Console App?
In my opinion,what you mean is the differences between async and sync method in asp.net core web api.
Synchronous means two or more operations are running in a same context (thread) so that one may block another.
If an API call is synchronous, it means that code execution will block (or wait) for the API call to return before continuing. This means that until a response is returned by the API, your application will not execute any further, which happens sequentially.
Making an API call synchronously can be beneficial, however, if there if code in your app that will only execute properly once the API response is received.
Asynchronous means two or more operations are running in different contexts (thread) so that they can run concurrently and do not block each other.
For more details,refer to
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/
Not much difference between ASP.NET Core sync and async controller actions
WebApi async vs sync
What's a sync and async method?

How to make ASP.NET MVC Controller Methods Async

I'm launching multiple ajax calls to various MVC controllers to load different parts of my page. However it seems that when this gets to the controller only one runs at a time. I'm guessing this is because by default ASP.Net MVC controllers are synchronous? I've also tested loading a page on 2 browser tabs and the second tab always waits for the first.
To get round this I've attempted to make the controller methods in question asynchronous. I've done this by doing the following
Append Async to controller method name
Make the controller methods return async Task
Used the Task.Factory.StartNew method to do the body of work in the method in a separate thread.
For example the controller methods in question now look like this...
public async Task<JsonResult> GetUser(int userId)
{
var result = await Task.Factory.StartNew(() => Task.Run(() =>
{
return userService.GetUser(userId);
})).Result;
return new JsonResult()
{
Data = result,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
However it still seems to be synchronous. Am I missing something or going about this completely the wrong way? I've not really used the Task Library much so may be missing something big?
No, your assumptions are most likely wrong. Your problem is likely one (or both) of two problems.
First, most web browsers have request limits that only allow a certain number of request to the same server at a time.
Second, you are probably running into a limitation of the Session object that causes multiple requests that use session to be serialized, because Session is not, itself, multi-threaded.
See http://tech-journals.com/jonow/2011/10/22/the-downsides-of-asp-net-session-state
The short answer is that if you don't use session in your action method, simply add this to the method...
[SessionState(SessionStateBehavior.Disabled)]
public class AjaxTestController : Controller
{
//...As above
}
If you only need to read the session, then do this:
[SessionState(SessionStateBehavior.ReadOnly)]
public class AjaxTestController : Controller
{
//...As above
}
There's not much you can do about the browser limitations though, since different browsers have specific request limits. These can be changed with registry (or browser config) entries (usually), but you can't force your users to do that in most cases.
One of the important feature introduced in MVC 4.0 was of Asynchronous controllers which enables to write the asynchronous action methods. Asynchronous controller allows an operation to get performed without making the working thread idle.
When an asynchronous action is invoked, the following steps occur:
The Web server gets a thread from the thread pool (the worker thread) and schedules it to handle an incoming request. This worker thread initiates an asynchronous operation. The worker thread is returned to the thread pool to service another Web request. When the asynchronous operation is complete, it notifies ASP.NET. The Web server gets a worker thread from the thread pool (which might be a different thread from the thread that started the asynchronous operation) to process the remainder of the request, including rendering the response.
Converting Synchronous Action Methods to Asynchronous Action Methods
Following is the example of synchronous action method and the its asynchronous equivalent version.
Synchronous Controller:
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
}
Asynchronous variant of above operation:
public class TestController : AsyncController
{
public void IndexAsync()
{
return View();
}
public ActionResult IndexCompleted()
{
return View();
}
}
Steps:
Synchronous Controllers are the classes derived from the Controller
class to implement an AsyncController instead of deriving the
controller from Controller, derive it from AsyncController class.
Controllers that derive from AsyncController enable ASP.NET to
process asynchronous requests, and they can still service synchronous
action methods.
Corresponding to the synchronous action method in Synchronous
controller you need to create two methods for the action in
asynchronous controller.First method that initiates the asynchronous
process must have a name that consists of the action and the suffix
"Async". The other method that is invoked when the asynchronous
process finishes (the callback method) must have a name that consists
of the action and the suffix "Completed".
In the above sample example, the Index action has been turned into two methods in asynchronous controller: IndexAsync and IndexCompleted.
The IndexAsync method returns void while the IndexCompleted method returns an ActionResult instance. Although the action consists of two methods, it is accessed using the same URL as for a synchronous action method (for example, Controller/Index).
Note the following about asynchronous action methods:
If the action name is Sample, the framework will look for
SampleAsync and SampleCompleted methods.
View pages should be named Sample.aspx rather than SampleAsync.aspx
or SampleCompleted.aspx. (The action name is Sample, not
SampleAsync)
A controller cannot contain an asynchronous method named SampleAsync
and a synchronous method named Sample. If it does, an
AmbiguousMatchException exception is thrown because the SampleAsync
action method and the Sample action method have the same request
signature.
For more details click here : http://www.counsellingbyabhi.com/2014/05/asynchronous-controllers-in-aspnet-mvc.html
mvc controllers are async in nature, how did you determine it's synchronous? The only reason could only be some lock implemented within your userService.
You can try by making a couple of hundreds of ajax calls to your web services using jquery