+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / SendAsync from a User Control

SendAsync from a User Control

9 posts, 0 answered
  1. chris_cf
    chris_cf avatar
    32 posts
    Registered:
    04 Jul 2007
    10 Apr 2009
    Link to this post
    Hi,

    How would I go about using SendAsync from a User Control?

    I keep getting the error:

    Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.
  2. chris_cf
    chris_cf avatar
    32 posts
    Registered:
    04 Jul 2007
    10 Apr 2009
    Link to this post
    I am trying to send an email using SmtpClient.

    object userState = msg; 
    SmtpClient smtp = new SmtpClient(); 
    smtp.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted); 
    smtp.SendAsync(msg, userState); 

  3. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    13 Apr 2009
    Link to this post
    Hello Chris_cf,

    The problems here are several.
    1. You use an internal cms page, which does not have <@Page Async="True" .. /> attribute.
    2. smtp.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);
    You are calling a delegate, but what will happen with the event when the main thread completes?

    There are several solutions:
    1. To make the page Asynchronous. There are two ways for doing this, but it is not a good solution, since the client will have no response from the server until all threads related to that page are completed. Sending the e-mails can take quite a lot of time, and we do not want the client browser to wait that long. Still, here are the solutions:
    • Assign this page a master page, having the Async="True attribute
    • Creating a new class in the App_Code directory, inheriting from InternalPage class. Then, making your cms internal pages to inherit your new class by tweaking the Sitefinity\cmsentrypoint.aspx:
      <%@ Page Inherits="YourInternalPageClass" MasterPageFile="~/Sitefinity/Dummy.master" %> 
      Then you can check if the user is on the page you want, and add the Async attribute (make the page assynchronous)
    2. To use the web service. This is the preffered choice - you can read the status of the sending and notify the user at any given time. By this approach, you will not make the user to wait until the sending is complete.

    3. By Ajax requests. On each call you will send one e-mail - the user interface will be still responsive.

    Here are a couple of resources that might help you as well:
    • http://stackoverflow.com/questions/672237/running-an-asynchronous-operation-triggered-by-an-asp-net-web-page-request/672275
    • http://bytes.com/groups/net-asp/600033-how-set-page-async-value-without-aspx-file

    I hope you find this information helpful.

    Kind regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  4. chris_cf
    chris_cf avatar
    32 posts
    Registered:
    04 Jul 2007
    14 Apr 2009
    Link to this post
    Hi Georgi,

    I ended up using the IIS SMTP Server as this will just relay the mails and not throw errors if mailboxes are unavailable, etc.

    However, I may go the Web Service route in the future.

    Thanks a mil.
  5. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    15 Apr 2009
    Link to this post
    Hello Chris,

    I am glad that you have a solution for your case.
    Let me know if I could of further help.

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Isaac
    Isaac avatar
    63 posts
    Registered:
    01 Dec 2008
    26 Aug 2010
    Link to this post
    I realize that this is an old thread but hopefully it is still being watched on some level.

    Could someone please provide me more information about how to implement method 1 listed by Georgi?  I am trying to add an Asynchronous User Control I have created to a CMS page and am having no luck.  I am getting the error message:

    An error occurred in the application:
    Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.
    Stack Trace:
    at
    System.Web.AspNetSynchronizationContext.OperationStarted() at
    System.ComponentModel.AsyncOperationManager.CreateOperation(Object userSuppliedState) at
    System.Web.Services.Protocols.SoapHttpClientProtocol.InvokeAsync(String methodName, Object[] parameters, SendOrPostCallback callback, Object userState) at
    ContactWebService.GetPropertiesTabAsync(Int64 contactID, Int64 tabID, Int32 getPropinfoOrPropxml, Object userState) in f:\wwwroot\Admissions\App_Code\ContactWebService.cs:line 775 at
    UserControls_Admissions_DiversityLeadershipScholarshipApp.Submit_Click(Object sender, EventArgs e) in f:\wwwroot\Admissions\UserControls\Admissions\DiversityLeadershipScholarshipApp.ascx.cs:line 62 at
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) at
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    Basically, during the execution of the User Control I am making 6 or 7 calls to a web service and I am doing those asynchronously.  Basically starting them as early as possible during the execution, then allowing the rest of the pages processing to complete while waiting for the web service calls to come back.  In this case I do want to wait to display stuff to the user until the operations complete so method 1 is the correct one for me to use.  However I have run into two problems

    1) Master pages do not have an Async attribute in the @Master directive
    2) I need help on creating an asynchronous page class.  I have the following:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Summary description for admisasyncpage
    /// </summary>
    public class admisasyncpage : Telerik.Cms.Web.InternalPage
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
        }
    }

    but I can't see anywhere in there on how to make that Asynchronous.  Do I have to create a .aspx page somehow?  I've spent several hours looking for help online and haven't been able to come up with anything.  If anyone could point me in the right direction, or give me instructions I would appreciate it. 

    I also tried editing the cmsentrypoint.aspx page to include the 'async="true"' and that didn't work.  Even with that line I still got the error. (And page.isasync still returned false).

    I checked the two links at the bottom of Georgi's post and I was unable to see how the first helped (likely a failing on my part) and the second appeared to suggest adding 'async="true"' to the @page directive in the .aspx page even though the post supposedly was about making a page async without having a .aspx page.

    Any help anyone can offer would be greatly appreciated.

    Thank you

    -nathan
  7. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    27 Aug 2010
    Link to this post
    Hello Nathan,

    Thank you for using our services.

    Setting the Async property to true in the page directinve in ~/Sitefinity/cmsentrypoint.ascx should make your Sitefinity pages asynchronous.

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Isaac
    Isaac avatar
    63 posts
    Registered:
    01 Dec 2008
    27 Aug 2010
    Link to this post
    Radoslov,

    Please see the following line in my above post:

    "I also tried editing the cmsentrypoint.aspx page to include the 'async="true"' and that didn't work.  Even with that line I still got the error. (And page.isasync still returned false)."

    I have already tried your suggestion, with no luck.
  9. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    30 Aug 2010
    Link to this post
    Hello Nathan,

    I am unable to reproduce this on my side. I have created a custom internal page which only writes if the async property is true or false:
    using Telerik.Cms.Web;
     
    /// <summary>
    /// Summary description for CustomInternalPage
    /// </summary>
    public class CustomInternalPage:InternalPage
    {
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            Response.Write("This page's async property is set to " + this.Page.IsAsync.ToString());       
        }
    }

    The I have set the Async property in cmsentrypoint.aspx:
    <%@ Page Inherits="CustomInternalPage" MasterPageFile="~/Sitefinity/Dummy.master"  Async="true"%>

    The response is true. I am even checking the property value when in the Page_Load event of a user control and is true there too.

    Kind regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
9 posts, 0 answered