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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:44:50+00:00 2026-05-24T01:44:50+00:00

What are the best practices for accessing a WCF web service from an ASP.NET

  • 0

What are the best practices for accessing a WCF web service from an ASP.NET application? Currently, in each page I need to access the service I have a field

private readonly _serviceClient = new WCFServiceClient();

and access it’s methods repeatedly, closing it on each call and creating a new instance everytime it’s state is Faulted. However, I don’t know whether I should do it like this or, instead, create a new instance per method call. What are the best practices regarding this?

  • 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-24T01:44:51+00:00Added an answer on May 24, 2026 at 1:44 am

    I’ve done something similar to the article RubbleFord linked to in his comment, but since I was dealing with multiple services I used ChannelFactory and cached the returned object upon initial creation. I then would create new channels as needed, use them, then close/abort as needed. The helper methods are in a separate DLL (I’ll use Common for the example):

    // bindingName refers to the Web.config binding section's name
    public static T GetFactoryChannel<T>(string address, string bindingName)
    {
        string key = typeof(T).Name;
    
        // OpenChannels is a property that refers to a Dictionary<string, object> holding the key and the channel factory
        if (!OpenChannels.ContainsKey(key))
        {  
            ChannelFactory<T> factory = new ChannelFactory<T>();
    
            factory.Endpoint.Address = new EndpointAddress(new System.Uri(address));
            factory.Endpoint.Binding = new NetTcpBinding(bindingName);
    
            OpenChannels.Add(key, factory);
        }
    
        T channel = ((ChannelFactory<T>)OpenChannels[key]).CreateChannel();
    
        ((IClientChannel(channel)).Open();
    
        return channel;
    }
    

    In my client code, I’d do something like this (with the helper methods in Common):

    IMyContract myContract = Common.GetChannelFactory<IMyContract>("net.tcp://someaddress/service", "MyNetTcpBinding");
    
    myContract.SomeMethod();
    
    Common.CloseChannel(myContract); // handles the necessary work to close or abort the channel.
    

    I developed this based on some articles posted on the web around a year ago, when I first started working with WCF, and it’s served me well. The OpenChannels dictionary object is stored (in my case, with the AppDomain as most of my WCF services are WCF libraries) so I only need to create each channel factory once during a given app’s lifetime.

    You can add any necessary logic (for example, credentials, or different types of bindings) to the GetFactoryChannel method as desired. I should also note that I don’t add service references to the projects, but use the generated proxy files from svcutil. This is in 3.5, btw.

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

Sidebar

Related Questions

I am building a c# - linq - sql server winforms/asp.net application, accessing a
I have converted my web service to wcf service which has some datacontracts. As
What is the best practice to accessing a single running mnesia node from another
this is a best practices type of question. I want to access the attributes
I've been researching all morning trying to find best practices for accessing your IoC.
Just curious what the best practice is for accessing an instance variable from within
One of the best practice is accessing data via stored procedures. I understand why
My question is pertaining to the best practice for accessing a child object's parent.
Best practices or tools for installing a SQL Server database I have a SQL
Any best practices/recommendations on the installation folder for the Android SDK? Following http://developer.android.com/sdk/installing.html on

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.