0

I've created some WCF service on my server, now I've to implement the client side, and I've basically two way of doing it:

With ClientBase implementation

public class SampleServiceClient : ClientBase<ISampleService>, ISampleService
{
    public SampleServiceClient(Binding binding, EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }

    public string SampleMethod(string msg)
    {
        return base.Channel.SampleMethod(msg);
    }
}

With ChannelFactory

public  ISampleService GetService(Binding binding, EndpointAddress remoteAddress){
    ChannelFactory<ISampleService> channelFactory =   new ChannelFactory<ISampleService>(binding, address); 
    return channelFactory.CreateChannel(); 
}

What is the advantage of one over the other? Is there some additional functionality in the ClientBase? Is there a best practice?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • Does [this](https://stackoverflow.com/questions/44605603/whats-the-difference-between-channelfactoryt-and-clientbaset) help? – Christian.K Jul 07 '17 at 08:04
  • And [this](https://stackoverflow.com/questions/7783510/wcf-channel-factory-vs-client-base) ? – ibubi Jul 07 '17 at 08:06
  • @Christian.K So the only advantage is to be able to add those behavior? Do you think one of the behavior could restore the channel if faulted? – J4N Jul 07 '17 at 08:07
  • I don't know. Frankly, there are a number of Q&A on the site dealing with differences, issues, possible performance etc. Just search for "clientbase channelfactory". – Christian.K Jul 07 '17 at 08:09
  • @ibubi In my case I don't use the "Add service reference" but I'm still able to use the `ClientBase`. Is it true that most of the time is used in the factory creation and not in the CreateChannel? – J4N Jul 07 '17 at 08:11
  • Also, most articles I find on internet are more about "Auto-generated stuff with svcutils VS ChannelFactory", but since you can use the ClientBase without being autogenerated, I don't really find the advantage of one over the other – J4N Jul 07 '17 at 08:44
  • @J4N you can't restore a channel when its faulted, you need to create a new one. – Rabban Jul 07 '17 at 08:46
  • @Rabban I know. BUT I was hopping that if I'm using the `ClientBase`, which owns the channel, than it would be possible to give him a new channel. – J4N Jul 07 '17 at 08:47
  • @J4N unfortunately not. As i remember right, a channel will not fault but the client will. the `ChannelFactory` in your `ClientBase` has a Faulted event. You can subscribe to it and recreate the client when its faulted. It is up to you to ensure that the client will not fault through exceptions that will thrown on the serverside. All other exceptions like connections problems should fault the client. – Rabban Jul 07 '17 at 08:56
  • @Rabban I didn't know about the faulted part! This is great! But if my channel is faulted, can I use the same factory to create a new channel, or should I absolutely create a new factory too? Feel weird to have a "Status" on a Factory – J4N Jul 07 '17 at 09:04
  • @J4N You need to create a new Client, in your case a `SampleServiceClient`. You need everytime a new client when its faulted or the connection is closed. Check the `State` of your client, if its `Closing`, `Closed` or `Faulted`, you need a new one. – Rabban Jul 07 '17 at 09:09
  • @Rabban and in the case of using the ChannelFactory directly? – J4N Jul 07 '17 at 09:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148586/discussion-between-rabban-and-j4n). – Rabban Jul 07 '17 at 09:17

0 Answers0