wcfservice-referencechannelfactory

When to use WCF ChannelFactory over service reference


I'm going through WCF tutorials and find that these tuts point me to make a mex endpoint available to allow for implicitly adding service references to clients. I guess this allows VS to create proxy classes when consuming the resultant WSDL file?

The codebase I'm trying to get comfortable on, however, does not use implicit service references. Instead clients have access to a DuplexChannelFactory. What are the benefits of service communication via explicit ChannelFactories vs implicit Service References?

This SO question here (How to re-write WCF client to use ChannelFactory instead of service reference) that mentions a company choosing ChannelFactories over service references but didn't mention why.


Solution

  • When to use ChannelFactories over service references

    Always use ChannelFactory, except when there is no other option. I cannot state this strongly enough. Service references are

    The only reasons you might want to use a service reference:

    1. You don't have binary access to the service definition and contract types in code. For example, you're calling a public service across the internet and there's no way the developers of the service are going to give them to you.
    2. That's it. That's the only reason.

    It could be argued that you should use a service reference if you have access to the service definition and contract types, but they are defined inside some uber-assembly with a whole bunch of other stuff you have no use for. However, I find this questionable because in this instance you could just re-create the service definition and types and leverage WCF's respect for contract equivalence.