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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:23:25+00:00 2026-06-01T04:23:25+00:00

This is how I used to make method calls: SvcHelper.Using<SomeWebServiceClient>(proxy => { proxy.SomeMethod(); }

  • 0

This is how I used to make method calls:

SvcHelper.Using<SomeWebServiceClient>(proxy =>
{
   proxy.SomeMethod();
}

public class SvcHelper
{
   public static void Using<TClient>(Action<TClient> action) where TClient : ICommunicationObject, IDisposable, new()
   {
   }
}

This is how I make method calls:

ChannelFactory<ISomethingWebService> cnFactory = new ChannelFactory<ISomethingWebService>("SomethingWebService");
ISomethingWebService client = cnFactory.CreateChannel();

using (new OperationContextScope((IContextChannel)client))
{
   client.SomeMethod();
}

My question is: Instead of replacing every instance of my original method call approach; Is there a way to modify my SvcHelper and do the creation of the channel in the SvcHelper constructor and then simply pass the interface like the following:

SvcHelper.Using<ISomethingWebService>(client =>
{
   client.SomeMethod();
}

Hope this makes sense and thanks in advance.

  • 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-01T04:23:26+00:00Added an answer on June 1, 2026 at 4:23 am

    First, you don’t want to create a new ChannelFactory<T> every call to the Using helper method. They are the most costly thing to construct in the WCF universe. So, at bare minimum, you will want to use a caching approach there.

    Second, you don’t want to tie yourself to “client” types at all anymore. Just work straight with the service contract interfaces.

    Starting from what you’ve got, here’s where I’d go based on how I’ve done this in the past:

    public class SvcHelper
    {
        private static ConcurrentDictionary<ChannelFactoryCacheKey, ChannelFactory> ChannelFactories = new ConcurrentDictionary<ChannelFactoryCacheKey, ChannelFactory>();
    
        public static void Using<TServiceContract>(Action<TServiceContract> action) where TServiceContract : class
        {
            SvcHelper.Using<TServiceContract>(action, "*");
        }
    
        public static void Using<TServiceContract>(Action<TServiceContract> action, string endpointConfigurationName) where TServiceContract : class
        {
            ChannelFactoryCacheKey cacheKey = new ChannelFactoryCacheKey(typeof(TServiceContract), endpointConfigurationName);
    
            ChannelFactory<TServiceContract> channelFactory = (ChannelFactory<TServiceContract>)SvcHelper.ChannelFactories.GetOrAdd(
                                                                                                                                    cacheKey,
                                                                                                                                    missingCacheKey => new ChannelFactory<TServiceContract>(missingCacheKey.EndpointConfigurationName));
    
            TServiceContract typedChannel = channelFactory.CreateChannel();
            IClientChannel clientChannel = (IClientChannel)typedChannel;
    
            try
            {
                using(new OperationContextScope((IContextChannel)typedChannel))
                {
                    action(typedChannel);
                }
            }
            finally
            {
                try
                {
                    clientChannel.Close();
                }
                catch
                {
                    clientChannel.Abort();
                }
            }
    
        }
    
        private sealed class ChannelFactoryCacheKey : IEquatable<ChannelFactoryCacheKey>
        {
            public ChannelFactoryCacheKey(Type channelType, string endpointConfigurationName)
            {
                this.channelType = channelType;
                this.endpointConfigurationName = endpointConfigurationName;
            }
    
            private Type channelType;
    
            public Type ChannelType
            {
                get
                {
                    return this.channelType;
                }
            }
    
            private string endpointConfigurationName;
    
            public string EndpointConfigurationName
            {
                get
                {
                    return this.endpointConfigurationName;
                }
            }
    
            public bool Equals(ChannelFactoryCacheKey compareTo)
            {
                return object.ReferenceEquals(this, compareTo)
                           ||
                       (compareTo != null
                           &&
                       this.channelType == compareTo.channelType
                           &&
                       this.endpointConfigurationName == compareTo.endpointConfigurationName);
            }
    
            public override bool Equals(object compareTo)
            {
                return this.Equals(compareTo as ChannelFactoryCacheKey);
            }
    
            public override int GetHashCode()
            {
                return this.channelType.GetHashCode() ^ this.endpointConfigurationName.GetHashCode();
            }
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Codeigniter platform to make a website I used this .load script to
I do not get the last version of node-http-proxy working (this used to work
Possible Duplicates: Why does StyleCop recommend prefixing method or property calls with “this”? When
I have a class WebServiceCaller that uses NSURLConnection to make asynchronous calls to a
In my understanding the strategy pattern is used to make behaviour interchangable. This involves
I have a class that creates an object inside one public method. The object
Because of C++ nature of static-binding for methods, this affects the polymorphic calls. From
I need to make multiple asynchronous service calls in the application:didFinishLaunchingWithOptions: method from my
I swear this used to work, but it's not in this case. I'm trying
I want to use opendiff as default diff-tool for git diff. This used to

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.