smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
584 stars 178 forks source link

An asynchronous operation cannot be started at this time #63

Open KiarashS opened 11 years ago

KiarashS commented 11 years ago

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?

smsohan commented 11 years ago

I'll look at this. Thanks for reporting.

KiarashS commented 11 years ago

problem not solved?

BitFlipp3r commented 11 years ago

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

jamsoft commented 11 years ago

Yeah, I'm seeing this as well.

jamsoft commented 11 years ago

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

smsohan commented 11 years ago

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 notifications@github.com 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.

KiarashS commented 11 years ago

@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.

smsohan commented 11 years ago

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 notifications@github.com 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.

tylermercier commented 11 years ago

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.

smsohan commented 11 years ago

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 notifications@github.comwrote:

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.

tylermercier commented 11 years ago

@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

KiarashS commented 11 years ago

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???

jamsoft commented 11 years ago

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.

BitFlipp3r commented 11 years ago

@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

smsohan commented 11 years ago

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 notifications@github.com 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.

jamsoft commented 11 years ago

Good work guys! :)

KiarashS commented 11 years ago

@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?

tylermercier commented 11 years ago

@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.

BitFlipp3r commented 11 years ago

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

smtp from="some-email@gmail.com" network host="localhost" / /smtp

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

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

jamsoft commented 11 years ago

@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! ;)

KiarashS commented 11 years ago

@jamsoft yes i asked from you;)

JustMaier commented 11 years ago

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.

smsohan commented 11 years ago

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 notifications@github.com 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.

michaelydavid commented 11 years ago

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

smsohan commented 11 years ago

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 notifications@github.comwrote:

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

JustMaier commented 11 years ago

@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

KiarashS commented 11 years ago

@JustMaier you check that in .net 4.5?

JustMaier commented 11 years ago

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

KiarashS commented 11 years ago

@JustMaier I have not check that yet.

topherdavis commented 11 years ago

@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.

KiarashS commented 11 years ago

@JustMaier @topherdavis i tested your solution, @JustMaier your solution Worked for me. @topherdavis your solution not work for me.

we hope @smsohan fix this bug in source code with the best solution.

topherdavis commented 11 years ago

@kiyarash To get SendAsync to work I had to target framework 4.0. I didn't have to include the configuration if the machine didn't have 4.5 installed at all.

The task based, as far as I know, will only work if you are targeting 4.5.

KiarashS commented 11 years ago

@topherdavis yes,SendAsync work in framework 4.0 BUT not in 4.5. i don't know your target framework is 4.0,while you never said in your comment about your target framework.

anyway, "we hope @smsohan fix this bug in source code with the best solution".

KiarashS commented 11 years ago

@smsohan @tylermercier @JustMaier @topherdavis @BitFlipp3r I think this issue solved when i marked action with async and return Task<ActionResult> instead of ActionResult and awaiting for SendAsync method, like to this:

        public async Task<ActionResult> Send(User info)
        {
            // ...
            // send email
            await _userMailer.SendEmail(new EmailM{ Email = info.Email, UserName = info.UserName  })
                       .SendAsync();
            // ...

            return Json(new { IsSuccess = "true" });
        }

What is your opinion?

smsohan commented 11 years ago

If you are awaiting, then you may as well use Send, the synchronous call. — Sent from Mailbox for iPhone

On Sat, Apr 6, 2013 at 12:53 AM, kiarash notifications@github.com wrote:

@smsohan @tylermercier @JustMaier @topherdavis @BitFlipp3r I think this issue solved when i marked action with async and return Task<ActionResult> and awaiting for SendAsync method, like to this:

        public virtual async Task<ActionResult> Send(User info)
        {
            // ...
            // send activation email
            await _userMailer.Activation(new EmailM{ Email = info.Email, UserName = info.UserName  })
                       .SendAsync();
            // ...
            return Json(new { IsSuccess = "true" });
        }

What is your opinion?

Reply to this email directly or view it on GitHub: https://github.com/smsohan/MvcMailer/issues/63#issuecomment-15991920