Async call in Controller, calling a void method in model. (asp.net MVC C#)?Calling the base constructor in C#Cross-thread operation not valid: Control accessed from a thread other than the thread it was created onCompile Views in ASP.NET MVCHow do you create a dropdownlist from an enum in ASP.NET MVC?ASP.NET MVC - Set custom IIdentity or IPrincipalDistinct() with lambda?File Upload ASP.NET MVC 3.0How to safely call an async method in C# without awaitAsync Void, ASP.Net, and Count of Outstanding OperationsWhy not inherit from List<T>?
Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?
Why does cooking oatmeal starting with cold milk make it creamy?
Why do textbooks often include the solutions to odd or even numbered problems but not both?
Android Material and appcompat Manifest merger failed in react-native or ExpoKit
What happened to Steve's Shield in Iron Man 2?
Can Ogre clerics use Purify Food and Drink on humanoid characters?
I found a password with hashcat, but it doesn't work
Hit the Bulls Eye with T in the Center
What are Elsa's reasons for selecting the Holy Grail on behalf of Donovan?
Did the CIA blow up a Siberian pipeline in 1982?
What does it mean to not be able to take the derivative of a function multiple times?
Do I have to explain the mechanical superiority of the player-character within the fiction of the game?
Do I need a shock-proof watch for cycling?
Do I have any obligations to my PhD supervisor's requests after I have graduated?
Is there any proof that high saturation and contrast makes a picture more appealing in social media?
How do I professionally let my manager know I'll quit over an issue?
Can White Castle?
What can I do with a research project that is my university’s intellectual property?
Count All Possible Unique Combinations of Letters in a Word
Shooting someone's past self using special relativity
How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?
How many people are necessary to maintain modern civilisation?
Term or phrase for simply moving a problem from one area to another
career in signal processing
Async call in Controller, calling a void method in model. (asp.net MVC C#)?
Calling the base constructor in C#Cross-thread operation not valid: Control accessed from a thread other than the thread it was created onCompile Views in ASP.NET MVCHow do you create a dropdownlist from an enum in ASP.NET MVC?ASP.NET MVC - Set custom IIdentity or IPrincipalDistinct() with lambda?File Upload ASP.NET MVC 3.0How to safely call an async method in C# without awaitAsync Void, ASP.Net, and Count of Outstanding OperationsWhy not inherit from List<T>?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
BACKGROUND: I'm trying to make an async Task in my MVCcontroller class that will call a method in my modelclass which will insert a bunch of data into multiple sql tables using an already existing sql table.
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Once it is done with it's operations it should write to a file that I will see and understand that all computations are done.
Yes, there might be smarter ways of calling the Task but that's for later, for now I just wish to figure out how to do the async set-up.
PROBLEM/QUESTION:
Controller says that the await_part can not await void, can I just return something like Json(success)? Or how do I do this set up? I'm kind of a rookie.
In the model method I wish to look at the existing sql-table and use it to create more sql-tables that I will persist in the database, not really interested in sending anything back besides something that says "I'm done with the calculations".
Controller calling the model method that will perform actions.
public async Task GenerateBackEndCalculations(string sqlTableName)
if (nameOfTable == "sqlTableName")
await _context.BackEndCalculations();
the model method that looks like this
public void BackEndCalculations ()
Do something here, and maybe return something if I have to???
c# asp.net asp.net-mvc asynchronous task
add a comment |
BACKGROUND: I'm trying to make an async Task in my MVCcontroller class that will call a method in my modelclass which will insert a bunch of data into multiple sql tables using an already existing sql table.
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Once it is done with it's operations it should write to a file that I will see and understand that all computations are done.
Yes, there might be smarter ways of calling the Task but that's for later, for now I just wish to figure out how to do the async set-up.
PROBLEM/QUESTION:
Controller says that the await_part can not await void, can I just return something like Json(success)? Or how do I do this set up? I'm kind of a rookie.
In the model method I wish to look at the existing sql-table and use it to create more sql-tables that I will persist in the database, not really interested in sending anything back besides something that says "I'm done with the calculations".
Controller calling the model method that will perform actions.
public async Task GenerateBackEndCalculations(string sqlTableName)
if (nameOfTable == "sqlTableName")
await _context.BackEndCalculations();
the model method that looks like this
public void BackEndCalculations ()
Do something here, and maybe return something if I have to???
c# asp.net asp.net-mvc asynchronous task
use Task.Run(()=>BackEndCalculations()).Wait();
– brykneval
Jul 22 '17 at 7:13
Async should only return void for event handlers. In your case it should not return void. Furthermore, even if this is done async, keep in mind that the response will not return to the front end until the whole operation is completed. The front end can issue the request asyn and then do other things until the repsonse is returned from the server but you need to do that on the front end side.
– CodingYoshi
Jul 22 '17 at 7:19
1
Don't useTask.Run(()=>BackEndCalculations()).Wait();
Wrapping already asynchronous method withTask.Run
is waste of resources(threads) and you will loose all benefits of asynchronous methods.
– Fabio
Jul 22 '17 at 7:19
...............................
– Johan
Jul 23 '17 at 16:40
add a comment |
BACKGROUND: I'm trying to make an async Task in my MVCcontroller class that will call a method in my modelclass which will insert a bunch of data into multiple sql tables using an already existing sql table.
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Once it is done with it's operations it should write to a file that I will see and understand that all computations are done.
Yes, there might be smarter ways of calling the Task but that's for later, for now I just wish to figure out how to do the async set-up.
PROBLEM/QUESTION:
Controller says that the await_part can not await void, can I just return something like Json(success)? Or how do I do this set up? I'm kind of a rookie.
In the model method I wish to look at the existing sql-table and use it to create more sql-tables that I will persist in the database, not really interested in sending anything back besides something that says "I'm done with the calculations".
Controller calling the model method that will perform actions.
public async Task GenerateBackEndCalculations(string sqlTableName)
if (nameOfTable == "sqlTableName")
await _context.BackEndCalculations();
the model method that looks like this
public void BackEndCalculations ()
Do something here, and maybe return something if I have to???
c# asp.net asp.net-mvc asynchronous task
BACKGROUND: I'm trying to make an async Task in my MVCcontroller class that will call a method in my modelclass which will insert a bunch of data into multiple sql tables using an already existing sql table.
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Once it is done with it's operations it should write to a file that I will see and understand that all computations are done.
Yes, there might be smarter ways of calling the Task but that's for later, for now I just wish to figure out how to do the async set-up.
PROBLEM/QUESTION:
Controller says that the await_part can not await void, can I just return something like Json(success)? Or how do I do this set up? I'm kind of a rookie.
In the model method I wish to look at the existing sql-table and use it to create more sql-tables that I will persist in the database, not really interested in sending anything back besides something that says "I'm done with the calculations".
Controller calling the model method that will perform actions.
public async Task GenerateBackEndCalculations(string sqlTableName)
if (nameOfTable == "sqlTableName")
await _context.BackEndCalculations();
the model method that looks like this
public void BackEndCalculations ()
Do something here, and maybe return something if I have to???
c# asp.net asp.net-mvc asynchronous task
c# asp.net asp.net-mvc asynchronous task
asked Jul 22 '17 at 7:10
JohanJohan
209
209
use Task.Run(()=>BackEndCalculations()).Wait();
– brykneval
Jul 22 '17 at 7:13
Async should only return void for event handlers. In your case it should not return void. Furthermore, even if this is done async, keep in mind that the response will not return to the front end until the whole operation is completed. The front end can issue the request asyn and then do other things until the repsonse is returned from the server but you need to do that on the front end side.
– CodingYoshi
Jul 22 '17 at 7:19
1
Don't useTask.Run(()=>BackEndCalculations()).Wait();
Wrapping already asynchronous method withTask.Run
is waste of resources(threads) and you will loose all benefits of asynchronous methods.
– Fabio
Jul 22 '17 at 7:19
...............................
– Johan
Jul 23 '17 at 16:40
add a comment |
use Task.Run(()=>BackEndCalculations()).Wait();
– brykneval
Jul 22 '17 at 7:13
Async should only return void for event handlers. In your case it should not return void. Furthermore, even if this is done async, keep in mind that the response will not return to the front end until the whole operation is completed. The front end can issue the request asyn and then do other things until the repsonse is returned from the server but you need to do that on the front end side.
– CodingYoshi
Jul 22 '17 at 7:19
1
Don't useTask.Run(()=>BackEndCalculations()).Wait();
Wrapping already asynchronous method withTask.Run
is waste of resources(threads) and you will loose all benefits of asynchronous methods.
– Fabio
Jul 22 '17 at 7:19
...............................
– Johan
Jul 23 '17 at 16:40
use Task.Run(()=>BackEndCalculations()).Wait();
– brykneval
Jul 22 '17 at 7:13
use Task.Run(()=>BackEndCalculations()).Wait();
– brykneval
Jul 22 '17 at 7:13
Async should only return void for event handlers. In your case it should not return void. Furthermore, even if this is done async, keep in mind that the response will not return to the front end until the whole operation is completed. The front end can issue the request asyn and then do other things until the repsonse is returned from the server but you need to do that on the front end side.
– CodingYoshi
Jul 22 '17 at 7:19
Async should only return void for event handlers. In your case it should not return void. Furthermore, even if this is done async, keep in mind that the response will not return to the front end until the whole operation is completed. The front end can issue the request asyn and then do other things until the repsonse is returned from the server but you need to do that on the front end side.
– CodingYoshi
Jul 22 '17 at 7:19
1
1
Don't use
Task.Run(()=>BackEndCalculations()).Wait();
Wrapping already asynchronous method with Task.Run
is waste of resources(threads) and you will loose all benefits of asynchronous methods.– Fabio
Jul 22 '17 at 7:19
Don't use
Task.Run(()=>BackEndCalculations()).Wait();
Wrapping already asynchronous method with Task.Run
is waste of resources(threads) and you will loose all benefits of asynchronous methods.– Fabio
Jul 22 '17 at 7:19
...............................
– Johan
Jul 23 '17 at 16:40
...............................
– Johan
Jul 23 '17 at 16:40
add a comment |
4 Answers
4
active
oldest
votes
If i understand you correctly, you can change void to Task (and add async keyword) any where and as long as there is a await in your function. If you don`t have any async method to await, just return some completed Task. For example Task.CompletedTask;
public async Task BackEndCalculations ()
Do something async
Or
public Task BackEndCalculations ()
Do something sync
return Task.CompletedTask;
No this will not do.see my comment to the OP question.
– CodingYoshi
Jul 22 '17 at 7:33
Well I'm just lost now, I'll take this thread to a colleague and show it as I don't get the flag and file thing either. Thanks for helping out even though I don't really get all of it :)
– Johan
Jul 23 '17 at 18:01
add a comment |
If you want to use await
in BackEndCalculations
it cannot return void, it needs to return a Task
.
Here are your options:
public Task BackEndCalculations() ...
public Task<JsonObject> BackEndCalculations() ...
public async Task BackEndCalculations() ...
public async Task<JsonObject> BackEndCalculations() ...
Depending on how the body of your function looks like
Not this will not help. See my comment to question.
– CodingYoshi
Jul 22 '17 at 7:34
Will not work because? How is your comment in OP's question related to this solution?
– MondKin
Jul 22 '17 at 20:27
Returning a Task will not help because the server will not return a response yo the client until the whole operation is completed. The OP is trying to use async to return to the client right away and thats not possible. See the answer from @stephencleary below.
– CodingYoshi
Jul 23 '17 at 8:01
As I said in the question I will write to a text file once the operations is done, not send anything back to the front end so async will help as I understand it. Thanks for answers.
– Johan
Jul 23 '17 at 16:30
@johan no it will not. That is not how async works. What you could do is to write a flag to a file and return the response to the client right away. Have another process that polls the file and if the flag is set, does the processing of the "million items" as you say. Or you can start the process async from the client. Your client can do other things while you are processing the "million items"
– CodingYoshi
Jul 23 '17 at 17:49
|
show 1 more comment
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Asynchronous code is not going to help you (see the section "Not a Silver Bullet"). In summary, async
on ASP.NET is a way to yield to the server's thread pool - it is not a way to yield (or return) to the client/browser.
There are a few approaches to returning early from ASP.NET requests, but most of them are dangerous to some degree or another. Since you already have a database, I'd recommend looking at HangFire. Note that your HangFire processing should be done outside ASP.NET - e.g., in an Azure Worker Role or Win32 Service.
add a comment |
For making any synchronous
method to behave like async
, just add two things, in method signature replace void
with Task
. and at the end of the method write return Task.CompletedTask;
.
Final implementation is like
public Task ASimpleSynchronousMethod()
// TO DO
// Add this additional line at the end of the method body
return Task.CompletedTask;
Hope this will work.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45251780%2fasync-call-in-controller-calling-a-void-method-in-model-asp-net-mvc-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
If i understand you correctly, you can change void to Task (and add async keyword) any where and as long as there is a await in your function. If you don`t have any async method to await, just return some completed Task. For example Task.CompletedTask;
public async Task BackEndCalculations ()
Do something async
Or
public Task BackEndCalculations ()
Do something sync
return Task.CompletedTask;
No this will not do.see my comment to the OP question.
– CodingYoshi
Jul 22 '17 at 7:33
Well I'm just lost now, I'll take this thread to a colleague and show it as I don't get the flag and file thing either. Thanks for helping out even though I don't really get all of it :)
– Johan
Jul 23 '17 at 18:01
add a comment |
If i understand you correctly, you can change void to Task (and add async keyword) any where and as long as there is a await in your function. If you don`t have any async method to await, just return some completed Task. For example Task.CompletedTask;
public async Task BackEndCalculations ()
Do something async
Or
public Task BackEndCalculations ()
Do something sync
return Task.CompletedTask;
No this will not do.see my comment to the OP question.
– CodingYoshi
Jul 22 '17 at 7:33
Well I'm just lost now, I'll take this thread to a colleague and show it as I don't get the flag and file thing either. Thanks for helping out even though I don't really get all of it :)
– Johan
Jul 23 '17 at 18:01
add a comment |
If i understand you correctly, you can change void to Task (and add async keyword) any where and as long as there is a await in your function. If you don`t have any async method to await, just return some completed Task. For example Task.CompletedTask;
public async Task BackEndCalculations ()
Do something async
Or
public Task BackEndCalculations ()
Do something sync
return Task.CompletedTask;
If i understand you correctly, you can change void to Task (and add async keyword) any where and as long as there is a await in your function. If you don`t have any async method to await, just return some completed Task. For example Task.CompletedTask;
public async Task BackEndCalculations ()
Do something async
Or
public Task BackEndCalculations ()
Do something sync
return Task.CompletedTask;
answered Jul 22 '17 at 7:21
AmirRaptoRAmirRaptoR
112
112
No this will not do.see my comment to the OP question.
– CodingYoshi
Jul 22 '17 at 7:33
Well I'm just lost now, I'll take this thread to a colleague and show it as I don't get the flag and file thing either. Thanks for helping out even though I don't really get all of it :)
– Johan
Jul 23 '17 at 18:01
add a comment |
No this will not do.see my comment to the OP question.
– CodingYoshi
Jul 22 '17 at 7:33
Well I'm just lost now, I'll take this thread to a colleague and show it as I don't get the flag and file thing either. Thanks for helping out even though I don't really get all of it :)
– Johan
Jul 23 '17 at 18:01
No this will not do.see my comment to the OP question.
– CodingYoshi
Jul 22 '17 at 7:33
No this will not do.see my comment to the OP question.
– CodingYoshi
Jul 22 '17 at 7:33
Well I'm just lost now, I'll take this thread to a colleague and show it as I don't get the flag and file thing either. Thanks for helping out even though I don't really get all of it :)
– Johan
Jul 23 '17 at 18:01
Well I'm just lost now, I'll take this thread to a colleague and show it as I don't get the flag and file thing either. Thanks for helping out even though I don't really get all of it :)
– Johan
Jul 23 '17 at 18:01
add a comment |
If you want to use await
in BackEndCalculations
it cannot return void, it needs to return a Task
.
Here are your options:
public Task BackEndCalculations() ...
public Task<JsonObject> BackEndCalculations() ...
public async Task BackEndCalculations() ...
public async Task<JsonObject> BackEndCalculations() ...
Depending on how the body of your function looks like
Not this will not help. See my comment to question.
– CodingYoshi
Jul 22 '17 at 7:34
Will not work because? How is your comment in OP's question related to this solution?
– MondKin
Jul 22 '17 at 20:27
Returning a Task will not help because the server will not return a response yo the client until the whole operation is completed. The OP is trying to use async to return to the client right away and thats not possible. See the answer from @stephencleary below.
– CodingYoshi
Jul 23 '17 at 8:01
As I said in the question I will write to a text file once the operations is done, not send anything back to the front end so async will help as I understand it. Thanks for answers.
– Johan
Jul 23 '17 at 16:30
@johan no it will not. That is not how async works. What you could do is to write a flag to a file and return the response to the client right away. Have another process that polls the file and if the flag is set, does the processing of the "million items" as you say. Or you can start the process async from the client. Your client can do other things while you are processing the "million items"
– CodingYoshi
Jul 23 '17 at 17:49
|
show 1 more comment
If you want to use await
in BackEndCalculations
it cannot return void, it needs to return a Task
.
Here are your options:
public Task BackEndCalculations() ...
public Task<JsonObject> BackEndCalculations() ...
public async Task BackEndCalculations() ...
public async Task<JsonObject> BackEndCalculations() ...
Depending on how the body of your function looks like
Not this will not help. See my comment to question.
– CodingYoshi
Jul 22 '17 at 7:34
Will not work because? How is your comment in OP's question related to this solution?
– MondKin
Jul 22 '17 at 20:27
Returning a Task will not help because the server will not return a response yo the client until the whole operation is completed. The OP is trying to use async to return to the client right away and thats not possible. See the answer from @stephencleary below.
– CodingYoshi
Jul 23 '17 at 8:01
As I said in the question I will write to a text file once the operations is done, not send anything back to the front end so async will help as I understand it. Thanks for answers.
– Johan
Jul 23 '17 at 16:30
@johan no it will not. That is not how async works. What you could do is to write a flag to a file and return the response to the client right away. Have another process that polls the file and if the flag is set, does the processing of the "million items" as you say. Or you can start the process async from the client. Your client can do other things while you are processing the "million items"
– CodingYoshi
Jul 23 '17 at 17:49
|
show 1 more comment
If you want to use await
in BackEndCalculations
it cannot return void, it needs to return a Task
.
Here are your options:
public Task BackEndCalculations() ...
public Task<JsonObject> BackEndCalculations() ...
public async Task BackEndCalculations() ...
public async Task<JsonObject> BackEndCalculations() ...
Depending on how the body of your function looks like
If you want to use await
in BackEndCalculations
it cannot return void, it needs to return a Task
.
Here are your options:
public Task BackEndCalculations() ...
public Task<JsonObject> BackEndCalculations() ...
public async Task BackEndCalculations() ...
public async Task<JsonObject> BackEndCalculations() ...
Depending on how the body of your function looks like
answered Jul 22 '17 at 7:15
MondKinMondKin
10.4k63861
10.4k63861
Not this will not help. See my comment to question.
– CodingYoshi
Jul 22 '17 at 7:34
Will not work because? How is your comment in OP's question related to this solution?
– MondKin
Jul 22 '17 at 20:27
Returning a Task will not help because the server will not return a response yo the client until the whole operation is completed. The OP is trying to use async to return to the client right away and thats not possible. See the answer from @stephencleary below.
– CodingYoshi
Jul 23 '17 at 8:01
As I said in the question I will write to a text file once the operations is done, not send anything back to the front end so async will help as I understand it. Thanks for answers.
– Johan
Jul 23 '17 at 16:30
@johan no it will not. That is not how async works. What you could do is to write a flag to a file and return the response to the client right away. Have another process that polls the file and if the flag is set, does the processing of the "million items" as you say. Or you can start the process async from the client. Your client can do other things while you are processing the "million items"
– CodingYoshi
Jul 23 '17 at 17:49
|
show 1 more comment
Not this will not help. See my comment to question.
– CodingYoshi
Jul 22 '17 at 7:34
Will not work because? How is your comment in OP's question related to this solution?
– MondKin
Jul 22 '17 at 20:27
Returning a Task will not help because the server will not return a response yo the client until the whole operation is completed. The OP is trying to use async to return to the client right away and thats not possible. See the answer from @stephencleary below.
– CodingYoshi
Jul 23 '17 at 8:01
As I said in the question I will write to a text file once the operations is done, not send anything back to the front end so async will help as I understand it. Thanks for answers.
– Johan
Jul 23 '17 at 16:30
@johan no it will not. That is not how async works. What you could do is to write a flag to a file and return the response to the client right away. Have another process that polls the file and if the flag is set, does the processing of the "million items" as you say. Or you can start the process async from the client. Your client can do other things while you are processing the "million items"
– CodingYoshi
Jul 23 '17 at 17:49
Not this will not help. See my comment to question.
– CodingYoshi
Jul 22 '17 at 7:34
Not this will not help. See my comment to question.
– CodingYoshi
Jul 22 '17 at 7:34
Will not work because? How is your comment in OP's question related to this solution?
– MondKin
Jul 22 '17 at 20:27
Will not work because? How is your comment in OP's question related to this solution?
– MondKin
Jul 22 '17 at 20:27
Returning a Task will not help because the server will not return a response yo the client until the whole operation is completed. The OP is trying to use async to return to the client right away and thats not possible. See the answer from @stephencleary below.
– CodingYoshi
Jul 23 '17 at 8:01
Returning a Task will not help because the server will not return a response yo the client until the whole operation is completed. The OP is trying to use async to return to the client right away and thats not possible. See the answer from @stephencleary below.
– CodingYoshi
Jul 23 '17 at 8:01
As I said in the question I will write to a text file once the operations is done, not send anything back to the front end so async will help as I understand it. Thanks for answers.
– Johan
Jul 23 '17 at 16:30
As I said in the question I will write to a text file once the operations is done, not send anything back to the front end so async will help as I understand it. Thanks for answers.
– Johan
Jul 23 '17 at 16:30
@johan no it will not. That is not how async works. What you could do is to write a flag to a file and return the response to the client right away. Have another process that polls the file and if the flag is set, does the processing of the "million items" as you say. Or you can start the process async from the client. Your client can do other things while you are processing the "million items"
– CodingYoshi
Jul 23 '17 at 17:49
@johan no it will not. That is not how async works. What you could do is to write a flag to a file and return the response to the client right away. Have another process that polls the file and if the flag is set, does the processing of the "million items" as you say. Or you can start the process async from the client. Your client can do other things while you are processing the "million items"
– CodingYoshi
Jul 23 '17 at 17:49
|
show 1 more comment
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Asynchronous code is not going to help you (see the section "Not a Silver Bullet"). In summary, async
on ASP.NET is a way to yield to the server's thread pool - it is not a way to yield (or return) to the client/browser.
There are a few approaches to returning early from ASP.NET requests, but most of them are dangerous to some degree or another. Since you already have a database, I'd recommend looking at HangFire. Note that your HangFire processing should be done outside ASP.NET - e.g., in an Azure Worker Role or Win32 Service.
add a comment |
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Asynchronous code is not going to help you (see the section "Not a Silver Bullet"). In summary, async
on ASP.NET is a way to yield to the server's thread pool - it is not a way to yield (or return) to the client/browser.
There are a few approaches to returning early from ASP.NET requests, but most of them are dangerous to some degree or another. Since you already have a database, I'd recommend looking at HangFire. Note that your HangFire processing should be done outside ASP.NET - e.g., in an Azure Worker Role or Win32 Service.
add a comment |
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Asynchronous code is not going to help you (see the section "Not a Silver Bullet"). In summary, async
on ASP.NET is a way to yield to the server's thread pool - it is not a way to yield (or return) to the client/browser.
There are a few approaches to returning early from ASP.NET requests, but most of them are dangerous to some degree or another. Since you already have a database, I'd recommend looking at HangFire. Note that your HangFire processing should be done outside ASP.NET - e.g., in an Azure Worker Role or Win32 Service.
As the frontend will time out when called from there if it's not async (It makes millions of operations) I wish to use this controller task to run async operations.
Asynchronous code is not going to help you (see the section "Not a Silver Bullet"). In summary, async
on ASP.NET is a way to yield to the server's thread pool - it is not a way to yield (or return) to the client/browser.
There are a few approaches to returning early from ASP.NET requests, but most of them are dangerous to some degree or another. Since you already have a database, I'd recommend looking at HangFire. Note that your HangFire processing should be done outside ASP.NET - e.g., in an Azure Worker Role or Win32 Service.
answered Jul 22 '17 at 12:26
Stephen ClearyStephen Cleary
298k49493621
298k49493621
add a comment |
add a comment |
For making any synchronous
method to behave like async
, just add two things, in method signature replace void
with Task
. and at the end of the method write return Task.CompletedTask;
.
Final implementation is like
public Task ASimpleSynchronousMethod()
// TO DO
// Add this additional line at the end of the method body
return Task.CompletedTask;
Hope this will work.
add a comment |
For making any synchronous
method to behave like async
, just add two things, in method signature replace void
with Task
. and at the end of the method write return Task.CompletedTask;
.
Final implementation is like
public Task ASimpleSynchronousMethod()
// TO DO
// Add this additional line at the end of the method body
return Task.CompletedTask;
Hope this will work.
add a comment |
For making any synchronous
method to behave like async
, just add two things, in method signature replace void
with Task
. and at the end of the method write return Task.CompletedTask;
.
Final implementation is like
public Task ASimpleSynchronousMethod()
// TO DO
// Add this additional line at the end of the method body
return Task.CompletedTask;
Hope this will work.
For making any synchronous
method to behave like async
, just add two things, in method signature replace void
with Task
. and at the end of the method write return Task.CompletedTask;
.
Final implementation is like
public Task ASimpleSynchronousMethod()
// TO DO
// Add this additional line at the end of the method body
return Task.CompletedTask;
Hope this will work.
answered Mar 25 at 8:09
Tahir AlviTahir Alvi
4892832
4892832
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45251780%2fasync-call-in-controller-calling-a-void-method-in-model-asp-net-mvc-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
use Task.Run(()=>BackEndCalculations()).Wait();
– brykneval
Jul 22 '17 at 7:13
Async should only return void for event handlers. In your case it should not return void. Furthermore, even if this is done async, keep in mind that the response will not return to the front end until the whole operation is completed. The front end can issue the request asyn and then do other things until the repsonse is returned from the server but you need to do that on the front end side.
– CodingYoshi
Jul 22 '17 at 7:19
1
Don't use
Task.Run(()=>BackEndCalculations()).Wait();
Wrapping already asynchronous method withTask.Run
is waste of resources(threads) and you will loose all benefits of asynchronous methods.– Fabio
Jul 22 '17 at 7:19
...............................
– Johan
Jul 23 '17 at 16:40