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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:00:03+00:00 2026-06-13T07:00:03+00:00

In our WCF project, we are using singleton pattern to obtain client proxy. Basically

  • 0

In our WCF project, we are using singleton pattern to obtain client proxy.

Basically because-

  1. Any enhancements required later, on the client object adding
    Binding or Endpoint, would require minimal changes.
  2. We do not call multiple service, at the same time.

To make sure that connection is closed properly after each service call, We are planning to implement IDisposable in singleton as below –

public class ClientSingleton : IDisposable
{
    static Service1Client client;
    private ClientSingleton()
    {
        client = new Service1Client();
    }
    public Service1Client getInstance()
    {
        if (client != null)
            return client;
        else
        {
            client = new Service1Client();
            return client;
        }
    }
    public void Dispose()
    {
        client.Close();
    }
}

Does this violates Singleton Design-Pattern principles? Any advice to improve this would be helpful.

edit:

Consider using block to dispose the client object as below –

using (Service1Client client = new Service1Client())
{
    client.Operation1();
}

That means WCF proxies implement IDisposable interface. So I don’t see any harm in implementing this interface here.

Thanks!

  • 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-13T07:00:05+00:00Added an answer on June 13, 2026 at 7:00 am

    I have been using an extension method in my project that takes care of closing the service connection properly. (I stole the extension method from some blog and I forgot that blog link)

    public static void CloseConnection(this ICommunicationObject client)
    {
      if (client.State != CommunicationState.Opened)
      {
        return;
      }
    
      try
      {
        client.Close();
      }
      catch (CommunicationException)
      {
        client.Abort();
        throw;
      }
      catch (TimeoutException)
      {
        client.Abort();
        throw;
      }
      catch (Exception)
      {
        client.Abort();
        throw;
      }
    }
    

    Unlike your approach (which is specific to a particular proxy) this can be used on any proxy to close the connection safely.

    Example usage

    Service1Client client = null
    
    try
    {
      client = new Service1Client();
    }
    finally
    {
      if(client != null)
         client.CloseConnection();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As mentioned in another question we are using WCF-RIA Services in our project in
We are using Oracle 10g database, NHibernate, WCF and Silverlight 3.0 in our project
Our WCF services occaisionally spawn a worker thread to handle something that the client
We are looking at switching from using WCF for our service layer in applications
We will write WCF service for our windows mobile client. Although we have the
We're about to set off our first larger WCF project, and we're having some
In one of our project we're using Linq-to-SQL to get data from our database.
In our project we are consuming WCF webservices exposed at Central location as services
We are using MVVM Light Silverlight project for WCF Ria Service based project. After
I need to transfer large Excel files over a WCF service. Our project requires

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.