views:

192

answers:

1

Doesn't configuring WCF in code require more coupling with the model and service libraries?

If I'm understanding it correctly, you configure the host in code in the same project as the utilized services. Then configure the channel on the client, but the client then has to know about the model which means if I have several client apps using the service they all have to have references to the model (or whatever youre using) assembly.

So I take it in code configuration/hosting isn't the best choice when multiple clients are involved?

A: 

You can configure WCF from code without creating more coupling than using configuration files.

I like to use the following partitioning:

Server:

  • One assembly for your data contracts (models/DTO).
  • One assembly for your service contracts (your WCF interfaces).
  • One assembly for your service implementations.
  • One assembly for your service host creation logic. That assembly will contain a class (WCFHostsLoader) that instantiate the service hosts for all your endpoints.
  • One program to wire up the WCF host. Under ASP.Net that could be the Globas.asax. In Globas.asax you would call WCFHostsLoader.Configure().

Client:

The client is a whole separate issue. You implied that the client would have to share the same assemblies (models) as the server. Although that is possible (this is what we do in our project); it is not mandatory.

Option 1 is to share the same model assembly and service contract assembly. As far as I'm concerned, this is the best approach if the client app is written in .Net (and written by you or by a party to whom your can give the two required assemblies). The main advantage is that on the client you don't end up with a bunch on ugly generated classes. I also think this approach is more elegant. Go look at the GoF Proxy pattern. In the pure proxy pattern, one does not proxy the parameters (models) to the service that is proxied.

Options 2 is to use a set of proxy classes generated from the server's WSDL. I think is much more common because a) Visual Studio's Add Service Reference makes this easy. b) If the client is not .Net, it is the only option. If you use the classes generated by Visual Studio's Add Service Reference you might have a hard time trying to take control of the channel factory creation process to configure everything from code instead of from config files (I have never tried to do it). Other tools might exist to generate proxy classes that would be more code configuration friendly.

I hope this helps

Sly
Yes, these would be the two that I thought of as well. I agree that the WSDL proxy is ugly, but it does decouple from the other assemblies to where you don't have to go in and update assembly references in multiple clients everytime something changes.
suedeuno
When using WSDL, the dependency is quite strong too. If you change the WSDL, it will most likely be a breaking change. Therefore, it is true that you don't have to update assembly references in multiple clients but you have to regenerate the service references (the proxies) in multiple clients. If you control the clients apps, updating the assemblies is probably faster and easier then regenerating the proxy classes.
Sly
Thanks for the reply. Yes I agree there is a dependency with the service reference that must be updated as well, but with applications sitting on different servers I think updating the service reference would be easier than manually moving assemblies. Not a huge difference but a difference none the less. I would rather not deal with WCF at all but with multiple clients and different servers it's probably for the best.
suedeuno