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

  • SEARCH
  • Home
  • 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 8266307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:06:46+00:00 2026-06-08T05:06:46+00:00

We have an asynchronous WCF service operation that gets log files from all of

  • 0

We have an asynchronous WCF service operation that gets log files from all of the different components of our system and sends them to the client. Since this could take a while if one of the components isn’t working right, it would be nice if this functionality won’t time out, but it shouldn’t cause the client to hang either.

My understanding of asynchronous WCF services is that when the client asks the server for something, the server immediately responds with a message saying, “I’m on it. Keep on doing your own stuff, and I’ll let you know when I’m finished.” Then the connection is freed up for the client to make other requests while the server spins up a new thread to do the bulk of its work. When the server is finished, it sends a message back to the client with the results. Because of this, the connection between the server and client is free, and regardless of how long the server takes, the connection should never time out. Is this correct?

If that’s the case, then our service isn’t working as expected. When I test the service, it works as expected as long as it takes less than a minute. If I force it to take longer than that, though, the client throws a TimeoutException. Since the service is asynchronous, shouldn’t it never time out? If so, what am I missing?

We wrote our asynchronous service using this page as a guide:
http://code.msdn.microsoft.com/windowsdesktop/How-to-Implement-a-WCF-2090bec8

Here is my code. This is the service contract:

[ServiceContract(CallbackContract = typeof(IInformationServiceCallBack), SessionMode = SessionMode.Required)]
public interface IInformationService
{
    //snip...

    [OperationContract(AsyncPattern=true)]
    [FaultContract(typeof(LogFileFault))]
    IAsyncResult BeginGetLogFiles(LogFileRequest[] logfileRequests,
        AsyncCallback callback, object state);

    LogFile[] EndGetLogFiles(IAsyncResult result);

    //snip...
}

This is the service implementation:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession, UseSynchronizationContext=false)]
public class InformationServiceImpl : IInformationService, IDisposable
{
    //snip...

    public IAsyncResult BeginGetLogFiles(LogFileRequest[] logfileRequests,
        AsyncCallback callback, object state)
    {
        var task = Task<LogFile[]>.Factory.StartNew((x) =>
            {
                return GetLogFilesHelper(logfileRequests);
            }, state);

        return task.ContinueWith(res => callback(task));
    }

    public LogFile[] EndGetLogFiles(IAsyncResult result)
    {
        var castResult = result as Task<LogFile[]>;
        return castResult.Result;
    }

    private LogFile[] GetLogFilesHelper(LogFileRequest[] logfileRequests)
    {
        //Long-running method that gets the log files
    }

    //snip...
}

Here is the client-side code:

public class InformationServiceConnection : WcfDurableConnection<IInformationService> //WcfDurableConnection is one of our internal classes
{
    //snip...

    public void GetServiceLogFiles(Action<LogFile[], WcfCommandResult> callback)
    {
        var logfileRequests = new LogFileRequest[]
        { 
            new LogFileRequest(/* snip */),
            new LogFileRequest(/* snip */),
            new LogFileRequest(/* snip */),
            new LogFileRequest(/* snip */)
        };

        ExecuteTask(x =>
            {
                LogFile[] logfile = null;
                WcfCommandResult wcfResult = null;

                var asyncCallback = new AsyncCallback((result) =>
                {
                    logfile = Channel.EndGetLogFiles(result);
                    callback(logfile, wcfResult);
                });

                wcfResult = RunCommand(y =>
                {
                    Channel.BeginGetLogFiles(logfileRequests, asyncCallback, null);
                }, x);
            });
    }

    /* ExecuteTask and RunCommand are both methods that take care of
     * multithreading issues for us. I included their code below in
     * case they make a difference, but the code I'm most interested
     * in is the GetServiceLogFiles method above. */

    //snip...
    protected CancellationTokenSource ExecuteTask(Action<CancellationToken> action)
    {
        CancellationTokenSource tokenSource = new CancellationTokenSource();

        ManualResetEvent lastTask;
        ManualResetEvent thisTask;
        lock (_objectLock)
        {
            lastTask = _syncTask;
            thisTask = new ManualResetEvent(false);
            _syncTask = thisTask;
        }

        tokenSource.Token.Register(x => ((ManualResetEvent)x).Set(), thisTask);

        var task = Task.Factory.StartNew((x) =>
        {
            try
            {
                lastTask.WaitOne();
                action((CancellationToken)x);
            }
            catch (Exception e)
            {
                LogUtility.Error(e);
            }
            finally
            {
                thisTask.Set();
            }
        }, tokenSource.Token, tokenSource.Token).HandleExceptions();

        return tokenSource;
    }

    //snip...

    protected WcfCommandResult RunCommand(Action<CancellationToken> action, CancellationToken token, bool isRestarting = false)
    {
        return RunCommand(x => { action(x); return true; }, token, isRestarting);
    }

    protected WcfCommandResult RunCommand(Func<CancellationToken, bool> action, CancellationToken token, bool isRestarting = false)
    {
        WcfCommandResult result = new WcfCommandResult();

        lock (_reconnectionLock)
        {
            if (_reconnecting && !isRestarting)
            {
                result.Completed = false;
                return result;
            }
        }


        lock (_channelLock)
        {
            if (Channel == null && !_closing)
            {
                token.ThrowIfCancellationRequested();
                Channel = GetNewChannel();
                var iChannel = (IClientChannel)Channel;
                var initResult = Initialize(token, false);

                if (initResult.Completed)
                {
                    Connected = true;
                    LogUtility.Info(string.Format("Connected to {0} at {1}", ServiceName, iChannel.RemoteAddress));
                }
                else
                    LogUtility.Info(string.Format("Failed to connect to {0} at {1}", ServiceName, iChannel.RemoteAddress));
            }
        }

        try
        {
            var channel = Channel;
            token.ThrowIfCancellationRequested();
            if (channel != null)
                result.Completed = action(token);
        }
        catch (FaultException e)
        {
            result.Exception = e;
            result.Detail = e.GetDetail<DurableFault>();
            LogUtility.Error(result.Exception);
        }
        catch (CommunicationException e)
        {
            Connected = false;
            result.Exception = e;
            IClientChannel channel = ((IClientChannel)Channel);
            if (channel != null)
                channel.Abort();
            Channel = null;
            if (!_reconnecting)
                LogUtility.Error(result.Exception);
        }
        catch (TimeoutException e)
        {
            Connected = false;
            result.Exception = e;
            IClientChannel channel = ((IClientChannel)Channel);
            if (channel != null)
                channel.Abort();
            Channel = null;
            if (!_reconnecting)
                LogUtility.Error(result.Exception);
        }
        catch (NullReferenceException e)
        {
            Connected = false;
            result.Exception = e;
            IClientChannel channel = ((IClientChannel)Channel);
            if (channel != null)
                channel.Abort();
            Channel = null;
            if (!_reconnecting)
                LogUtility.WriteException("Channel is null, it has either been disposed or not setup, call BeginSetupUser to create a new channel", e);
        }
        catch (ObjectDisposedException e)
        {
            Connected = false;
            result.Exception = e;
            IClientChannel channel = ((IClientChannel)Channel);
            if (channel != null)
                channel.Abort();
            Channel = null;
            if (!_reconnecting)
                LogUtility.Error(result.Exception);
        }
        catch (InvalidOperationException e)
        {
            Connected = false;
            result.Exception = e;
            IClientChannel channel = ((IClientChannel)Channel);
            if (channel != null)
                channel.Abort();
            Channel = null;
            if (!_reconnecting)
                LogUtility.Error(result.Exception);
        }

        return result;
    }

    //snip...
}
  • 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-06-08T05:06:49+00:00Added an answer on June 8, 2026 at 5:06 am

    There is a timeout set in your config file even for asynchronous calls. You should probably increase it if it will take a long time to respond. I think default is 1 minute. In visual studio, go to tools-> WCF Service Configuration Editor to easily change the value.

    This may also help you if you want to see what the configuration looks like: Increasing the timeout value in a WCF service

    You can set it in that config file, or in the code behind.

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

Sidebar

Related Questions

I have a WCF service that I call from a windows service. The WCF
I have a WCF service that provides a couple of methods. A Windows Forms
I have web-service (wcf) to analyze files. Size of files are 1-10 mb. File
I have inherited some code that queries a DB over a WCF service and
I have a simple WCF service serving up notifications to and from an app.
I have a WCF service client implemented using the asynchronous event (using /async and
I have a Silverlight 2 application that is consuming a WCF service. As such,
I have an asynchronous operation that for various reasons needs to be triggered using
I need to make multiple asynchronous calls from inside a wcf service hosted in
I have a Windows Service that is exposing a WCF service thru a net.tcp

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.