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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:31:35+00:00 2026-06-15T01:31:35+00:00

I have recently started to write my own ChannelFactory wrapper to call my WCF

  • 0

I have recently started to write my own ChannelFactory wrapper to call my WCF service instead of relying on the auto generated code from visual studio

Previously, with the auto generated proxies, I was able to do this for error checking:

    protected AccessControlServiceClient AccessControlService
    {
        get
        {
            // Check if not initialized yet
            if (HttpContext.Current.Session["AccessControlServiceClient"] == null)
                HttpContext.Current.Session["AccessControlServiceClient"] = new AccessControlServiceClient();

            // If current client is 'faulted' (due to some error), create a new instance.
            var client = HttpContext.Current.Session["AccessControlServiceClient"] as AccessControlServiceClient;
            if (client.State == CommunicationState.Faulted)
            {
                try { client.Abort(); }
                catch { /* no action */ }

                client = new AccessControlServiceClient();
                HttpContext.Current.Session["AccessControlServiceClient"] = client;
            }

            return client;
        }
    }

How should I handle this for ChannelFactor? What are the best practices to handle, and re-create the channel should something go wrong? e.g., intermittent network connection, session time out, etc.

This is how my code looks like currently:

Snippet from ProxyBase.cs – creating the channel

private void Initialise()
{
    lock (_sync)
    {
        if (_channel != null) return;

        _channel = new ChannelFactory<T>(_serviceEndPointUri).CreateChannel();
    }
 }

UserManagementServiceClient.cs – IUserManagementService is the WCF contract

public class UserManagementServiceClient : ProxyBase<IUserManagementService>
{
    public UserManagementServiceClient(string serviceEndPointUri)
        : base(serviceEndPointUri)
    {
    }

    public TokenResponse GetToken(TokenRequest request)
    {            
        return Channel.GetToken(request);
    }


    public LoginResponse Login(LoginRequest request)
    {
        return Channel.Login(request);
    }


    public LogoutResponse Logout(LogoutRequest request)
    {
        return Channel.Logout(request);
    }
}

And finally, this is how I’m calling it in my MVC project

    protected UserManagementServiceClient UserManagementService
    {
        get
        {
            // Check if not initialized yet
            if (HttpContext.Current.Session["UserManagementServiceClient"] == null)
                HttpContext.Current.Session["UserManagementServiceClient"] = new UserManagementServiceClient("NetTcpBinding_UserManagementService");

            var client = HttpContext.Current.Session["UserManagementServiceClient"] as UserManagementServiceClient;

            return client;
        }
    }

So presently, whenever the default 10 mins session is up, I get an error because the channel has already closed.

How can I harden my code such that it takes care of network disconnections/session timeouts etc?

  • 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-15T01:31:37+00:00Added an answer on June 15, 2026 at 1:31 am

    I use code like this

    public T Channel {
        get {
            lock (_channelLock) {
                if (!object.Equals(_channel, default(T))) {
                    if (((ICommunicationObject)_channel).State == CommunicationState.Faulted) {
                        // channel has been faulted, we want to create a new one so clear it
                        _channel = default(T);
                    }
                }
    
                if (object.Equals(_channel, default(T))) {
                    // channel is null, create a new one
                    Debug.Assert(_channelFactory != null);
                    _channel = _channelFactory.CreateChannel();
                }
                return _channel;
            }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started developing a webpage and recently hired someone to write code to
I'm an avid vim user and have started to write some SQL code recently.
I have recently started to learn Objective-C and write my tests using OCUnit that
I have recently started using DocCheck for checking the validity of JavaDoc's in code
I have recently started managing my own VPS with Webmin. I uploaded my website
Recently I've started to write on asp.net mvc framework. I have a problem. I
I have recently started creating my own controls and I seem to have a
I have recently started to write unit tests in my projects and I notice
I have recently started trying to learn MPI to code in C. I am
I have been learning MVC recently and have started creating my own framework (for

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.