MvcMailer icon indicating copy to clipboard operation
MvcMailer copied to clipboard

An asynchronous operation cannot be started at this time

Open KiarashS opened this issue 12 years ago • 35 comments

Hi, when i want send mail Async in my controller with SendAsync method i get to this error:

System.InvalidOperationException An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

i use MVC4.

why?

KiarashS avatar Oct 02 '12 20:10 KiarashS

I'll look at this. Thanks for reporting.

smsohan avatar Oct 04 '12 17:10 smsohan

problem not solved?

KiarashS avatar Oct 07 '12 16:10 KiarashS

Any updates on this issue? Same problem here.. using MVC4

BitFlipp3r avatar Nov 06 '12 20:11 BitFlipp3r

Yeah, I'm seeing this as well.

jamsoft avatar Nov 07 '12 14:11 jamsoft

Maybe a solution to derive from AsyncController? Not sure if there is a Base version of that class off the top of my head.

jamsoft avatar Nov 07 '12 15:11 jamsoft

I was looking for a good solution, couldn't find one. You can probably invoke the method from your controller by wraping the Send method in an async block.

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Wed, Nov 7, 2012 at 8:51 AM, jamsoft [email protected] wrote:

Maybe a solution to derive from AsyncController? Not sure if there is a Base version of that class off the top of my head.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/63#issuecomment-10153019.

smsohan avatar Nov 07 '12 15:11 smsohan

@smsohan please see this link: http://stackoverflow.com/questions/13031144/async-keyword-and-choice-of-the-taskscheduler perhaps help to you for a good solution.

Please fix this problem soon.

KiarashS avatar Nov 07 '12 17:11 KiarashS

Thanks for this. I'll take a look at it.

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Wed, Nov 7, 2012 at 10:09 AM, kiyarash [email protected] wrote:

@smsohan https://github.com/smsohan please see this link: http://stackoverflow.com/questions/13031144/async-keyword-and-choice-of-the-taskscheduler perhaps help to you for a good solution.

Please fix this problem soon.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/63#issuecomment-10156249.

smsohan avatar Nov 07 '12 17:11 smsohan

If it looks viable, we could get together tonight or tomorrow to hack something out. Not sure if I'll have a windows VM next week.

tylermercier avatar Nov 07 '12 17:11 tylermercier

Ok, let's give it a shot tonight. Ping me when you are free.

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Wed, Nov 7, 2012 at 10:11 AM, Tyler Mercier [email protected]:

If it looks viable, we could get together tonight or tomorrow to hack something out. Not sure if I'll have a windows VM next week.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/63#issuecomment-10156328.

smsohan avatar Nov 07 '12 17:11 smsohan

@kiyarash @BitFlipp3r @jamsoft Can you guys have a look at this project using SendAsync? It seems to be working for me. Can you reproduce you error in the project if everything works for you?

https://github.com/tylermercier/MvcApplication3

tylermercier avatar Nov 09 '12 02:11 tylermercier

MVCMailer has problem, @tylermercier please use a real mail in web.config no local directory(c:\temp). @smsohan please open this issue because problem not solved. i don't know why @tylermercier closed this issue while problem not solved???

KiarashS avatar Nov 09 '12 07:11 KiarashS

Hi Tyler,

That doesn't appear to be a like for like test. I've used exactly the same code but my site is hosted in IIS and using a real mailbox, not a drop location.

jamsoft avatar Nov 09 '12 09:11 jamsoft

@tylermercier Just because everything works in your sample project does not mean the problem has solved. I'm doing exactly the same in my project like you did in yours and still get the error. Only difference I could see is that I'm targeting 4.5 instead of 4.0

BitFlipp3r avatar Nov 09 '12 13:11 BitFlipp3r

Yup, this looks like a significant difference. We will try targeting .net 4.5.


Sent from my iPhone Sohan SM

On 2012-11-09, at 6:37 AM, BitFlipp3r [email protected] wrote:

@tylermercier https://github.com/tylermercier Just because everything works in your sample project does not mean the problem has solved. I'm doing exactly the same in my project like you did in yours and still get the error. Only difference I could see is that I'm targeting 4.5 instead of 4.0

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/63#issuecomment-10226545.

smsohan avatar Nov 09 '12 13:11 smsohan

Good work guys! :)

jamsoft avatar Nov 10 '12 22:11 jamsoft

@jamsoft,this code(https://github.com/tylermercier/MvcApplication3) worked with a drop location Or with a real mail in sender and reciever?

I do not think,are you sure?

KiarashS avatar Nov 11 '12 08:11 KiarashS

@BitFlipp3r @jamsoft @kiyarash I'm sorry everyone, I didn't mean to close this. We are still trying to diagnose what the cause is and what we can do to reproduce and then fix it. Must of hit Close and Comment, instead of comment.

@kiyarash That project works for @smsohan and myself, but we didn't try sending. The issue appears to be with the HttpContext, so saving to disc or actually sending the mail shouldn't matter. Though the possibility of another issue further down the chain where actually sending fails could happen.

Right now it appears the problem is with .NET 4.5 and sending async.

tylermercier avatar Nov 11 '12 17:11 tylermercier

FYI I'm using the following setup in web.config with smtp4dev (http://smtp4dev.codeplex.com/) for testing.

smtp from="[email protected]" network host="localhost" / /smtp

Edit: Had to take the opening and closing brackets out because of markdown...

Works like a charm (... without asnyc :D).

BitFlipp3r avatar Nov 11 '12 18:11 BitFlipp3r

@kiyarash I'm not sure I understand the question. Are you asking me if it works or telling me it does?

@tylermercier I think we can forgive you for hitting the wrong button ... at least this time ;) But don't let me see you do it again! k! ;)

jamsoft avatar Nov 11 '12 18:11 jamsoft

@jamsoft yes i asked from you;)

KiarashS avatar Nov 12 '12 09:11 KiarashS

Any news on sending async? IIS 7.5, MVC4 with the latest MVCMailer from NuGet and I'm having the same issue with gmail set as my SMTP.

JustMaier avatar Dec 17 '12 08:12 JustMaier

Dud you try the example project? I think we saw it worked fine in .net 4, but had issues with 4.5.


Sent from my iPhone Sohan SM

On 2012-12-17, at 1:16 AM, Justin Maier [email protected] wrote:

Any news on sending async? IIS 7.5, MVC4 with the latest MVCMailer from NuGet and I'm having the same issue with gmail set as my SMTP.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/63#issuecomment-11433380.

smsohan avatar Dec 17 '12 14:12 smsohan

I'm using 4.5 and it doesn't work. Guessing lots of folks out there using 4.5.

michaelydavid avatar Dec 17 '12 14:12 michaelydavid

Yeah, I think for 4.5 I'd have to go for a Task based async model.

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Mon, Dec 17, 2012 at 7:59 AM, michaelydavid [email protected]:

I'm using 4.5 and it doesn't work. Guessing lots of folks out there using 4.5.

smsohan avatar Dec 17 '12 15:12 smsohan

@smsohan I tried the example project and the following seemed to work fine in the example and in my own MVC4 project:

Task.Factory.StartNew(() => {
    mail.Send();
});

It cut down the request time from 2.5s to 44ms so I'm pretty sure it works... Thanks

JustMaier avatar Dec 17 '12 18:12 JustMaier

@JustMaier you check that in .net 4.5?

KiarashS avatar Dec 17 '12 20:12 KiarashS

@kiyarash Yes, using VS2012 with Target Framework of .NET 4.5 seems to work fine. Is it not working for you still?

JustMaier avatar Dec 17 '12 20:12 JustMaier

@JustMaier I have not check that yet.

KiarashS avatar Dec 17 '12 21:12 KiarashS

@JustMaier @kiyarash I was having problems with both solutions in different environments.

I found this: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/79b408bb-2a1e-46e2-900e-3b1a40f815fe

If I set <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> then the task based solution works.

If I set <add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" /> then the original SendAsync works.

topherdavis avatar Jan 18 '13 04:01 topherdavis