Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An asynchronous operation cannot be started at this time #63

Open
KiarashS opened this issue Oct 2, 2012 · 35 comments
Open

An asynchronous operation cannot be started at this time #63

KiarashS opened this issue Oct 2, 2012 · 35 comments

Comments

@KiarashS
Copy link

KiarashS commented Oct 2, 2012

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
Copy link
Owner

smsohan commented Oct 4, 2012

I'll look at this. Thanks for reporting.

@KiarashS
Copy link
Author

KiarashS commented Oct 7, 2012

problem not solved?

@BitFlipp3r
Copy link

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

@jamsoft
Copy link

jamsoft commented Nov 7, 2012

Yeah, I'm seeing this as well.

@jamsoft
Copy link

jamsoft commented Nov 7, 2012

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
Copy link
Owner

smsohan commented Nov 7, 2012

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//issues/63#issuecomment-10153019.

@KiarashS
Copy link
Author

KiarashS commented Nov 7, 2012

@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
Copy link
Owner

smsohan commented Nov 7, 2012

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//issues/63#issuecomment-10156249.

@tylermercier
Copy link
Contributor

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
Copy link
Owner

smsohan commented Nov 7, 2012

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//issues/63#issuecomment-10156328.

@tylermercier
Copy link
Contributor

@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
Copy link
Author

KiarashS commented Nov 9, 2012

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
Copy link

jamsoft commented Nov 9, 2012

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
Copy link

@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
Copy link
Owner

smsohan commented Nov 9, 2012

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//issues/63#issuecomment-10226545.

@jamsoft
Copy link

jamsoft commented Nov 10, 2012

Good work guys! :)

@KiarashS
Copy link
Author

@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 tylermercier reopened this Nov 11, 2012
@tylermercier
Copy link
Contributor

@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
Copy link

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
Copy link

jamsoft commented Nov 11, 2012

@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
Copy link
Author

@jamsoft yes i asked from you;)

@KiarashS KiarashS reopened this Nov 20, 2012
@JustMaier
Copy link

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
Copy link
Owner

smsohan commented Dec 17, 2012

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//issues/63#issuecomment-11433380.

@michaelydavid
Copy link

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

@smsohan
Copy link
Owner

smsohan commented Dec 17, 2012

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
Copy link

@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
Copy link
Author

@JustMaier you check that in .net 4.5?

@JustMaier
Copy link

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

@KiarashS
Copy link
Author

@JustMaier I have not check that yet.

@topherdavis
Copy link

@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
Copy link
Author

@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
Copy link

@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
Copy link
Author

@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
Copy link
Author

KiarashS commented Apr 6, 2013

@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
Copy link
Owner

smsohan commented Apr 6, 2013

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:
#63 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants