-1

Software Environment Details:

-Visual Studio 2017

-.NET Framework 4.5

We have a WCF Service and WCF Client Library C# library.

In order to adhere to proper coding practices, I want the WCFService that I'm coding to also implement the System.ServiceModel.IClientChannel interface.

However, if my WCF Service classes implements the System.ServiceModel.IClientChannel interface then I have to implement a whole bunch of methods that I don't need to use.

However, I am only interested in using

IClientChannel.Close();

and

IClientChannel.Abort();

I could resort to coding:

((IClientChannel)blahblahWcfChannel).Close()

((IClientChannel)blahblahWcfChannel).Abort()

However, invoking the aforementioned casting code looks messy.

Therefore, could someone please show me code that will allow me to invoke the IClientChannel Close() and/or Abort() , but without having to implement all the methods within the System.ServiceModel.IClientChannel interface and/or resorting to the messy casting?

Community
  • 1
  • 1
crazyTech
  • 1,379
  • 3
  • 32
  • 67

2 Answers2

2

There is no ability to not implement a required method from an interface. No way around that.

You can just add a stub function to cover the requirement that just calls this line:

throw new NotImplementedException();

That way in the future if someone tries to use that function not knowing that it was never fleshed out then they'll know it needs to be written still.

CarCar
  • 680
  • 4
  • 13
0

@CarCar We want the WCF Service implementation class code to implement IClientChannel’s Abort() and Close() functionality withOut having to implement all the other methods of the IClientChannel interface. It’s better coding practice to ensure that references to WCF Service are closed off after we finish using it. Therefore, The following 2 interfaces were created:

public interface IProjectWCFService : IServiceContract, IClientChannel

The WCF Service logic is within the WcfService class:

public class WcfService : IServiceContract

Within the WcfService class, we have implementations for Abort() and Close() methods like so:

    public void Close()
    {
        ((IClientChannel)this).Close();
    }

    public void Dispose()
    {
        this.Close();
    }

    public void Abort()
    {
        ((IClientChannel)this).Abort();
    }

Within the code, we can bind the IProjectWCFService interface to the WcfService class

We create the WCF Service url End Point Address string that ends with $"{nameof(WcfService)}.svc" , and then

using the System.ServiceModel. ChannelFactory < TChannel > (Binding binding, EndpointAddress remoteAddress) API, we can bind the IProjectWCFService interface to the WcfService class ( i.e.

new ChannelFactory < IProjectWCFService > (blablahBindingblahblah, http://blahblah/ WcfService.svc)

)

With the aforementioned programming technique, implement IClientChannel’s Abort() and Close() functionality withOut having to implement all the other methods of the IClientChannel interface

crazyTech
  • 1,379
  • 3
  • 32
  • 67