I have found 2 ways of consuming a WCF service without the help from svcutil.exe
:
ClientBase<IService>
ChannelFactory<IService>
I know that ClientBase
probably uses ChannelFactory
. But I'm talking about choosing between writing:
public sealed class ServiceClient
: ClientBase<IService>, IService
{
ReturnType IService.MethodName(ParameterType parameterName)
{
return Channel.MethodName(parameterName);
}
}
// later
IService client = new ServiceClient();
var result = client.MethodName(parameterName);
or
ChannelFactory<IMyService> channelFactory = new ChannelFactory<IMyService>();
channelFactory.Open();
var channel = channelFactory.CreateChannel();
var result = channel .MethodName(parameterName);
channelFactory.Close();
Which one should I choose?
What are your criteria for choosing?
Functionally, in the end, both approaches work just fine and basically give you the same result.
What do you want to base your decision on?
My advice: pick the style that you feel more comfortable with! The approach with the ChannelFactory<IService>
probably requires you to write less and less mundane code - so maybe that would be a little advantage for that approach.
Both approaches require that you have .NET on both ends of the channel, and that service and client share a common assembly with the service and operation contracts in them - since the client must know at least the service interface in order to be able to use either of the two ways of connecting to the service.