WCF:App.config中的配置是否比自定义Proxy类中的硬编码更好? [英] WCF: Is configuration in App.config better than in hard-coded in custom Proxy class?
问题描述
首先此不是重复的.我有一个不同的问题.
first of all this is not duplicate. I have a little bit different question.
在客户端(或在跨平台项目的情况下为客户端)的App.config文件中摆脱WCF的配置是一种好习惯,并用注入的 Proxy
类(来自不同的dll)替换它,为所有客户共享)?
Is it good practice to get rid of WCF's config in App.config file in the client (or clients in case of cross-platform project) and replace it with injected Proxy
class (from different dll, shared for all clients)?
我才刚开始,所以我的配置文件并不大,但这是一个示例:
I'm just starting so my config file is not big, but here is an example:
App.config(WCF部分):
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:8002/MyService"
binding="netTcpBinding"
contract="CallbackExample.Client.IMyService" />
</client>
</system.serviceModel>
我必须将此代码复制并粘贴到我创建的每个客户端中.使用Xamarin,我可以在单个VS解决方案中有许多用C#编写的客户端.因此,尽管如此,为什么不删除 system.serviceModel
部分并做到这一点呢?
I must have this code copy and pasted into every client I made. Working with Xamarin I can have many clients written in C# in single VS solution. So I though, why not just get rid of system.serviceModel
section and do that:
MyServiceProxy.cs:
public class MyServiceProxy : ClientBase<IMyService>, IMyService
{
public MyServiceProxy()
: base(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8002/MyService"))
{
}
public int JustAMethod()
{
return Channel.JustAMethod();
}
}
这样,我将把配置放在一个地方.
This way, I will have my configuration in single place.
因此,我的问题是:是否认为这是一种好习惯?有没有更好的方法?我不确定,因为WCF的全部目的在于通过App.config flie实现灵活性和易于配置.
So, my question is: Is it considered as a good practice? Is there a better way? I'm not sure, cause WCF is all about flexibility and easy config through App.config flie.
推荐答案
如果不需要其他配置,我总是选择纯代码解决方案.
If there is no need for additional configuration, I always go for pure code solutions.
过去,通过app.conf,WCF的配置稍微复杂一些.使用 ServiceHost
,您甚至可以用纯代码配置WCF服务.
Back in the day WCF was slightly more complex to configure, via app.conf. With the ServiceHost
you can even configure a WCF service in pure code.
这同样适用于WCF客户端imo;如果您不需要通过配置文件为用户配置它:我想说说纯代码解决方案.
The same applies to a WCF client imo; if you don't have the need to configure it via a config file for your users: I'd say go for the pure code solution.
此处另一个讨论同一主题的.(类似的推理,问题仍然存在;您是否需要配置灵活性).
Here another discussion of the same topic. (kind of the same reasoning, the question remains; do you need the configuration flexibility).
这篇关于WCF:App.config中的配置是否比自定义Proxy类中的硬编码更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!