WCF Exposing Metadata or having the client implement the interface?
What are the pro / cons to having the client consume a WCF service either vi开发者_高级运维a adding a reference to the service (and basically everything is generated for you) or having the client implement a shared interface and them having to code class manually?
Thanks!
In general, if you don't use code generation, then you'll have to write by hand what would otherwise be generated for you.
The "maintenance issue" that Andrew mentions is solved by simply using "Update Service Reference" when the service contract changes. If this becomes a hassle, then create a separate project to contain all of the proxy classes. You then only need to use "Update Service Reference" in one place.
Of course, if the service contract or related contracts change in an incompatable manner, then your client code will have to change. That's true regardless of which technique you use.
If you generate the code automatically then you have a maintenance issue. You have to regenerate it again whenever you change the interface or any of the server configuration.
For this reason, I NEVER generate the client from exposed metadata.
The interface should be defined in one library. Let's call this library MyContractsLib. The service implementation should be in a separate assembly (which I'll call MyContractsImplementation). The client should go in another assembly.
The client should then use a ChannelFactory to create the service.
var cf = new ChannelFactory<MyContractsLib.MyContract>(this.EndpointName);
MyContractsLib.MyContract serviceProxy = cf.CreateChannel();
The only scenario where it's warranted is if the service is developed by a third-party, and you independently writing the client application.
If you have the time and inclination, See this presentation goes into this in depth.
精彩评论