问题
I have a self hosted C# WCF service which creates 20+ endpoints for various purposes. Each is configured in the code itself with a few basic config items in the app.config of the service such as port and address. The service works great for the tested clients, but has not been widely tested.
I was a little leary of the standard wcf config file approach because I was afraid the end user would mess things up and hence did everything in code.
Is it a better idea to do the configuration in the config file because then the end user could customize it to their needs or is the in code approach sufficient for most needs?
回答1:
One of the main benefits of WCF is the abstraction of the connection details away from the code. If you need to change any of the service parameters, doing so from the web.config can be done more readily, and without recompilation. You may need to change the "port and address," for instance. If you are doing this from within the code, you would need to rebuild, which may be impractical. Further, I'm not sure why the end user would be messing with the web.config normally, unless it's really necessary.
In short, unless you have a good reason not to use the config files, you should probably use them in order to get the full benefit of the abstraction that WCF provides.
回答2:
You need to ask yourself some questions
- do different developers have different values on their machine?
- will someone else that does not work with the c# have to change these setting values? (e.g. an admin)
- Will certain values (e.g. message size) need to be adjusted for performance later on?
If the answer is yes to any of those questions then you need to move the settings into a .config file. If your worry is that clients who understand other settings are going to mess up your wcf settings on accident you can always separate settings by adding the configSource
attribute to your web.config or app.config file for each tag and put parts of your configuration in separate files in a sub-directory to reduce the likelihood of an accidental change.
回答3:
Simply,
If you use the .config
file then you will be able to alter the configuration after the code is compiled. This can be good or bad, depending on your situation.
来源:https://stackoverflow.com/questions/10337573/pros-and-cons-of-wcf-config-file-vs-in-code-config