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 9252451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:56:52+00:00 2026-06-18T10:56:52+00:00

I have a rest client that wraps HttpClient and whose methods are async. Besides

  • 0

I have a “rest client” that wraps HttpClient and whose methods are async.
Besides other reasons, I need to control signin/signout process with my rest client so that number of sessions is not exceeded.

The rest client implements IDisposable and upon disposing the client I need to check if the client is “still signed in” and sign out if it is.
Since doing any kind of external calls in Dispose method is considered bad practice, I have something as following

public class MappingsController : RestController
{
    [HttpGet]
    public async Task<HttpResponseMessage> GetYears()
    {
        return await ProcessRestCall(async rc => await rc.GetYearsAsync());
    }
}

public class RestController : ApiController
{
    protected async Task<HttpResponseMessage> ProcessRestCall<T>(Func<RestClient, Task<T>> restClientCallback)
    {
        RestClient restClient = null;
        try
        {
            var credentials = GetCredentialsFromRequestHeader();
            if (credentials == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Missing credentials from header!");
            }
            var username = credentials["Username"];
            var password = credentials["Password"];

            restClient = new RestClient(username, password);
            var authenticated = await restClient.SignInAsync();
            if (!authenticated)
            {
                return CreateErrorResponseWithRestStatus(HttpStatusCode.Unauthorized, restClient);
            }
            var result = await restClientCallback(restClient);
            // Following works, but since I need to do it in finally block in case exception happens, perhaps It should be done in finally anyways...
            //await restClient.SignOutAsync(); 
            var response = Request.CreateResponse(HttpStatusCode.OK, result);
            return response;
        }
        catch (Exception e)
        {
            return CreateErrorResponseWithRestStatus(HttpStatusCode.BadRequest, restClient, e);
        }
        finally
        {
            if (restClient != null)
            {
                if (restClient.IsSignedIn)
                {
                    //var signedOutOk = restClient.SignOutAsync();//.Result; //<-- problem - this blocks!!!
                    restClient.SignOutAsync().ConfigureAwait(false); // seems to work, but I am not sure if this is kosher + I can't get return var

                    //Logger.Warn(CultureInfo.InvariantCulture, m => m("Client was still signed in! Attempt to to sign out was {0}", signedOutOk ? "successful" : "unsuccessful"));
                }
                restClient.Dispose();
            }
        }
    }
}
  • 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-18T10:56:53+00:00Added an answer on June 18, 2026 at 10:56 am

    The use of .ConfigureAwait(false) is a non-issue. You aren’t awaiting on the task at all. Since you don’t await it, it doesn’t matter what await is configured to do.

    What you’re doing is just basic fire and forget (which may or may not be acceptable for you).

    You should remove the ConfigureAwait(false) no matter what, just because it does nothing and is confusing to the reader. If it’s okay for you to send the request to sign out but not actually sign out, then this is okay.

    If you need to ensure that restClient.Dispose(); isn’t called until the sign out request returns, then you have a bit of a…problem. The problem stems from the fact that the sign out request might be unsuccessful, or much worse, it might not respond at all. You’d need some way of dealing with that.

    You can’t use await in a finally block, but you can more or less mimic its behavior through continuations. You may need to do something like this:

    public static async Task DoStuff()
    {
        IDisposable disposable = null;
        try { }
        finally
        {
            var task = GenerateTask();
            var continuation = Task.WhenAny(task, Task.Delay(5000))
                .ContinueWith(t =>
                {
                    if (task.IsCompleted) //if false we timed out or it threw an exception
                    {
                        var result = task.Result;
                        //TODO use result
                    }
    
                    disposable.Dispose();
                });
        }
    }
    

    Note that since you aren’t using await the task returned from DoStuff will indicate that it is “done” as soon as it hits the finally block for the first time; not when the continuation fires and the object is disposed. That may or may not be acceptable.

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

Sidebar

Related Questions

I have a Jersey client that is successfully calling a REST service and populating
I have a Silverlight client that I need to call a web service. The
We have a Rest API that requires client certificate authentication. The API is used
I have a script that does this: require 'rubygems' require 'rest-client' require 'json' url
I'm looking to implement a REST client in PHP, and have previously been using
I have a REST server and a client application running on a mobile device.
I have a REST service consumed by a .Net WCF client. When an error
I have created a REST web service and successfully used it with a client.
I have a REST service that returns a JSON like this: [{@id:123,name:Name}] and I'm
I have client that built a website that is part static html and part

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.