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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:57:08+00:00 2026-06-10T15:57:08+00:00

Lets say I have 3 assemblies, Example.Core, Example.Contracts, Example.WcfServices. In my contracts assembly I

  • 0

Lets say I have 3 assemblies, Example.Core, Example.Contracts, Example.WcfServices. In my contracts assembly I define an interface and add some operation, e.g. ICalculator, which has operation Add(double a, double b). In my WcfServices assembly I have an implementation of ICalculator explosed as a Wcf service.

Now my question is this….in my Example.Core assembly how do I program against that interface while keeping everything decoupled (to allow me to have an alternative implementation of the interface). If I have a class that needs an ICalculator I can create one from say a ChannelFactory and use it, or I can inject an instance in the constructor. If I create one in the class then I am putting dependencies in my class on ChannelFactory/Wcf and I really don’t want to do that. If I inject an instance in my constructor then how will the injecting class manage and tidy up the wcf service? It seems that although I have an interface I have no clean way of using it. I have looked at something like NInject, but I am not convinced that it would clean up the ChannelFactory if it faults (at least I haven’t found any documentation that shows it knows when to call Abort rather than Close on the channel).

What I have ended up doing is implmenting my interface again and using the method described in this question: creating WCF ChannelFactory<T> and just recalling the methods on the service. This “smells” a bit to me as I am wrapping all my calls again just to ensure the channel is properly closed/aborted.

Has anyone any patterns/methods that cleanly have two implmentations of an interface, one of which is a Wcf service?

Thanks,

Mike.

  • 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-10T15:57:10+00:00Added an answer on June 10, 2026 at 3:57 pm

    I have used a variation of the answer linked in Mark’s comment and this article here to come up with a solution that has worked for me.

    Given the service contract you defined, step 1 will be to define an interface implementing your service and IClientChannel.

    // Service Contract
    public interface ICalculator
    {
        Add(double a, double b);
    }
    
    // Interface to expose Close and Abort
    public interface ICalculatorChannel : ICalculator, IClientChannel { }
    

    Step 2 involves creating reusable code that will handle creating the proxies and implementing the code to close or abort connections.

    public class ServiceClient<T> where T : class, IClientChannel
    {
        private ProxyGenerator _generator = new ProxyGenerator();
    
        public T CreateProxy(string endpointConfigurationName)
        {
            return _generator.CreateInterfaceProxyWithoutTarget<T>
                (new WcfInterceptor<T>(endpointConfigurationName));
        }
    }
    

    The T in ServiceClient<T> will take the ICalculatorChannel defined in step 1. The ProxyGenerator is part of the Castle project. Its usage here is to allow us to intercept the calls to the WCF service and perform pre and post actions.

    public class WcfInterceptor<T> : IInterceptor where T : IClientChannel
    {
        private ChannelFactory<T> _factory = null;
    
        public WcfInterceptor(string endpointConfigurationName)
        {
            _factory = new ChannelFactory<T>(endpointConfigurationName);
        }
    
        public void Intercept(IInvocation invocation)
        {
            T channel = _factory.CreateChannel();
    
            try
            {
                invocation.ReturnValue = invocation.Method.Invoke
                    (channel, invocation.Arguments);
            }
            finally
            {
                closeChannel(channel);
            }
        }
    }
    

    As you can see, the Intercept method encapsulates the channel and the call to close the channel. The closeChannel method will handle the decision to call Close() or Abort().

        private void closeChannel(T channel)
        {
            if (channel != null)
            {
                try
                {
                    if (channel.State != CommunicationState.Faulted)
                    {
                        channel.Close();
                    }
                    else
                    {
                        channel.Abort();
                    }
                }
                catch
                {
                    channel.Abort();
                }
            }
        }
    

    Now we create a class to wrap up the usage of this ServiceClient.

    public class Calculator : ICalculator
    {
        private var _calcualtor = new ServiceClient<ICalculatorChannel>();
    
        public void Add(double a, double b)
        {
            var proxy = _calculator.CreateProxy("endpointConfigGoesHere");
    
            return proxy.Add(a, b);
        }
    }
    

    Notice that the implementation of add no longer has to deal with the WCF connection concerns. The consuming class only has to know about the Service Contract. The ugliness of of dealing with faulted connections is now taken care of for us by the ServiceClient class.

    The client need only take on one dependency now.

    public class MyClient
    {
        private ICalculator _calculator;
    
        public MyClient(ICalculator calculator)
        {
            _calculator = calculator;
        }
    }
    

    And IOC can populate the client:

    Bind<ICalculator>().To<Calculator>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say i have at least two lua script files. test1.lua test2.lua both define
Lets say I have multiple commands like 5000 which updates some column and row
Lets say I have a .c file with some methods, and I create a
Lets say I have a schema that defines the following XML: <Values> <Add Key=Key1>Value
In the process of learning WCF. To examplify, lets say I have 3 assemblies
Lets say I have a p tag like this: <div id = example> <p
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say we have a table here, populated with the following data: acc_id1 acc_id2
Lets say I have five tables named table1, table2 ... table5. I have already
Lets Say i have a table like this WEB_LIST_TABLE KEY Value ---------------------------------------- 134 google.com

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.