我正在向WCF服务编写客户端。这是大型系统中的单个应用程序,其中包括用C#,C++,VB和Java编写的模块。所有应用程序共享通用的配置和日志记录机制,而不论使用哪种语言编写。

我想弄清楚如何构建客户端应用程序,使其无需app.config即可运行。为什么?因为app.config中的大多数内容都是样板,所以不应允许sysadmins进行更改,并且应允许sysadmins进行哪些设置更改应在系统范围的配置中,而不是在app.config文件中坐在bin目录中。

案例-客户端的app.config当前如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="WSHttpBinding_ICourierService">
          <security defaultAlgorithmSuite="Default" authenticationMode="SecureConversation"
            ...
          </security>
          <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
            messageVersion="Default" writeEncoding="utf-8">
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
              maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          </textMessageEncoding>
          <httpTransport manualAddressing="false"
            ...
            useDefaultWebProxy="true" />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:57102/MyService.svc"
        ...
        >
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

这是一堆不透明的样板,系统管理员不必处理。大部分是由Visual Studio插入的。我对文件进行了更改-我增加了<readerQuotas/>的最大大小。但这是我不希望系统管理员惹的祸。除了<endpoint address=""/>之外,文件中没有其他我想要系统管理员弄乱的东西。

我正在从系统范围的配置中提取端点地址,并将其设置为代码。该文件中没有任何内容是用户可编辑的。

那么,我该如何配置事物以使我不需要展示它?

我可以将其作为资源嵌入到程序集中吗,并可以像我对所需DLL的处理方式那样挂接到app.config加载过程中吗?

创建代码配置事物的唯一选择是我使用代码设置端点地址的方式吗?在代码中创建必要的绑定(bind)等?那么,考虑到这些不透明的XML块,我如何知道要编写什么代码?

最佳答案

您可以使用以下代码来创建配置正在执行的绑定(bind)。我不确定这是否会让您完全删除文件,但是在这种情况下,应用程序将不会使用配置。将自己的值放在超时等中。

    var binding = new WSHttpBinding();
    binding.SendTimeout = new TimeSpan(0, 0, 0, 0, 100000);
    binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, 100000);
    binding.MaxReceivedMessageSize = 10000;
    binding.ReaderQuotas.MaxStringContentLength = 10000;
    binding.ReaderQuotas.MaxDepth = 10000;
    binding.ReaderQuotas.MaxArrayLength = 10000;
    var endpoint = new EndpointAddress("http://localhost:57102/MyService.svc");
    var myClient = new WebServiceclient(binding, endpoint);

关于.net - .NET-在没有app.config的情况下部署WCF客户端,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7688798/

10-14 15:54
查看更多