Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4239368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:01:35+00:00 2026-05-21T03:01:35+00:00

I try to implement the ‘AsyncPattern’ within a WCF data service. I define the

  • 0

I try to implement the ‘AsyncPattern’ within a WCF data service. I define the 2 methods BeginGetExperiments(…) and EndGetExperiments(…) in the interface and implement the methods as can be seen below.

public class GmdProfileService : IGmdProfileService
{
 IAsyncResult IGmdProfileService.BeginGetExperiments(AsyncCallback callback, object state)
    {
        //IAsyncResult res =  Experiment.GetExperimentsAsync(callback, state, Properties.Settings.Default.gmdConnectionString);
        //return res;

        System.Data.SqlClient.SqlConnectionStringBuilder csb = new System.Data.SqlClient.SqlConnectionStringBuilder(Properties.Settings.Default.gmdConnectionString);
        csb.AsynchronousProcessing = true;
        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(csb.ConnectionString);
        conn.Open();
        System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
        cmd = conn.CreateCommand();
        cmd.CommandText = "SELECT id, name, comment, date, doi FROM tf.TagList WITH(NOLOCK) WHERE proprietary=0;";
        cmd.CommandType = System.Data.CommandType.Text;
        return new SqlCommandAsyncResult(cmd, callback, state);
    }

    public List<Experiment> EndGetExperiments(IAsyncResult result)
    {
        List<Experiment> res = new List<Experiment>();
        SqlCommandAsyncResult myresult = result as SqlCommandAsyncResult;
        using (System.Data.SqlClient.SqlDataReader reader = myresult.cmd.EndExecuteReader(myresult.originalState as IAsyncResult))
        {
            try
            {
                while (reader.Read())
                {
                    res.Add(new Experiment(reader));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                // Closing the reader also closes the connection, because this reader was created using the CommandBehavior.CloseConnection value.
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        return res;
    }

BeginGetExperiments returns a class SqlCommandAsyncResult implementing the IAsyncResult interface in addition to holding a reference to my SqlCommand for later access.

public class SqlCommandAsyncResult : IAsyncResult
{
    public SqlCommand cmd { get; private set; }
    public IAsyncResult originalState { get; private set; }

    public SqlCommandAsyncResult(SqlCommand cmd, AsyncCallback callback, object state)
    {
        this.cmd = cmd;
        this.originalState = cmd.BeginExecuteReader(callback,
            state,
            System.Data.CommandBehavior.SequentialAccess |  // doesn't load whole column into memory
            System.Data.CommandBehavior.CloseConnection   // close connection immediately after read
            );
    }

    public object AsyncState
    {
        get { return originalState.AsyncState; }
    }

    public WaitHandle AsyncWaitHandle
    {
        get { return originalState.AsyncWaitHandle; }
    }

    public bool CompletedSynchronously
    {
        get { return false; }
    }

    public bool IsCompleted
    {
        get { return AsyncWaitHandle.WaitOne(0); }
    }
}

The difficulties I face are in the EndGetExperiments method. I dont know how to access the SqlCommand to call EndExecuteReader(...).
Normally I would use the state object in the BeginExecutereader to pass on the command. But if I do so, I get the exception:
“IAsyncResult’s State must be the state argument passed to your Begin call.”

So I try to use the IAsyncResult to pass the SqlCommand forward to EndGetExperiments. Here, the point I don’t understand is that in EndGetExperiments the variable result is either of type IAsyncResult or of type SqlCommandAsyncResult depending on the value of CompletedSynchronously in the SqlCommandAsyncResult class.
Setting CompletedSynchronously = false makes my code fail because I cant’t access the SqlCommand whereas setting CompletedSynchronously = true the code works like a charm but I have an odd feeling that something might go wrong under the hood.

I appreciate any help, guidance and example code how to make this code working and even more important in helping me to understand the problem at hand.

Thank you very much.
Jan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:01:36+00:00Added an answer on May 21, 2026 at 3:01 am

    Today WCF Data Services doesn’t support asynchronous processing on the server. Please vote/add a feature request for it here: http://data.uservoice.com/forums/72027-wcf-data-services-feature-suggestions/topics/72603-wcf-data-services-feature-suggestions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to define a schema for XML documents I receive. The documents look
I try implement advance on my question form here: send parameter from view-model to
Hi I try implement solution from this site im my WPF app for global
I try to implement PrepareContainerForItemOverride method of ItemsControl. It will put items to TextBox.
I try to implement a hover effect (effect when button is pressed) through putting
I try to implement the abstract DbCommand class (like OdbcCommand, OleDbCommand, ...) but a
I try to implement Incognito k-anonymization algorithm in Java. A part of this algorithm
i try to implement a redo undo in my windows form application. i build
I try to implement a singleton pattern with the capability of passing arguments to
Could somebody try to implement given animation into WebGL shader example? It would be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.