Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which WCF configuration approach is smarter?

Tags:

c#

wcf

Im reading 2 books about WCf and one of them is showing Configuration as App.Config and the other is showing via Code in C#

I want to know which approach should i use and what are the benefits for each other. Does the Wcf Configuration Wizard supports also the c# code behind configuration ?

thanks for the answers.

like image 499
Royi Namir Avatar asked Aug 05 '11 11:08

Royi Namir


3 Answers

Configuration files can be changed without a rebuild (handy, say, to add a custom inspector or serializer), is pretty easy to copy/paste between client/server, has support from the IDE tooling.

Code is handy if you are configuring the system at runtime (i.e. getting the information from another server), or are running as a library (dll) and can't guarantee that a configuration file will a: exist, or b: have your configuration. Code also has intellisense / static checking to avoid brain-dead errors (typos in the xml etc).

I'd use a file until you know you have a scenario that doesn't work well with a file.

Also consider: how hard is it for you to deploy a code change vs a configuration change? for me they're about the same, but that might not be the case for you. Maybe it is easier to just change the config on the machines? maybe not.

like image 131
Marc Gravell Avatar answered Nov 17 '22 23:11

Marc Gravell


I guess it depends on your needs. I personally tend to configure wcf with code, especially for things that are unlikely to ever change. That might include error handlers/logger, behaviors, authentication modules, service host factories, etc

For more dynamic stuff, like connection strings, passwords, file paths, etc are configured in .config files.

One of the biggest advantages of using code is that your code now can support things like IOC/Dependency injection, compile time checking, etc.

I don't buy into the idea that everything should be in a config file because it's easier to change it. In most cases I've seen it never changes in production.

like image 25
Henning Avatar answered Nov 17 '22 23:11

Henning


The configuration file approach is better, it gives more flexibility. For example i change the authentication type (username password/windows) by changing config files.

like image 1
croisharp Avatar answered Nov 18 '22 01:11

croisharp