.NET - 部署 WCF 客户端,无需 app.config

发布于 2024-12-08 16:57:44 字数 1989 浏览 2 评论 0原文

我正在将客户端写入 WCF 服务。这是一个较大系统中的单个应用程序,其中包括用 C#、C++、VB 和 Java 编写的模块。所有应用程序都共享通用的配置和日志记录机制,无论它们是用什么语言编写的。

我想弄清楚如何构建客户端应用程序,以便它可以在没有 app.config 的情况下运行。为什么?因为 app.config 中的大部分内容都是样板文件,不应允许系统管理员更改,并且应允许系统管理员更改哪些设置应该在系统范围的配置中,而不是在 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 插入的。我对文件进行了一项更改 - 我增加了 中的最大大小。但我不希望系统管理员扰乱这一变化。除了 之外,文件中没有任何其他内容是我希望系统管理员搞乱的。

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

那么,我该如何配置才能不需要它呢?

我可以将其作为资源嵌入到程序集中,并挂接到 app.config 加载进程中,就像处理所需 DLL 一样吗?

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

I'm writing a client to a WCF service. This is a single app in a larger system that includes modules written in C#, C++, VB, and Java. All of the apps share common configuration and logging mechanisms, regardless of what language they were written in.

I'd like to figure out how to build the client application so that it will run without an app.config. Why? Because most of what is in the app.config is boilerplate that the sysadmins shouldn't be allowed to change, and what settings the sysadmins should be allowed to change should be in the system-wide configuration, and not in an app.config file sitting in the bin directory.

Case in point - the client's app.config currently looks like this:

<?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>

It's a bunch of opaque boilerplate that the sysadmins shouldn't have to deal with. Most of it was inserted by Visual Studio. I've made one change in the file - I increased the max size in <readerQuotas/>. But that's a change that I don't want the sysadmins to mess with. And there's nothing else in the file I want the sysadmins to mess with except for <endpoint address=""/>.

And I'm pulling the endpoint address from the system-wide configuration, and setting it in code. There's nothing in this file that should be user-editable.

So, how do I configure things so that I don't need to have it present?

Can I embed it as a resource in the assembly, and hook into the app.config load process, the way I do with required DLLs?

Is the only choice to create code to configure things, the way I'm using code to set the endpoint address? Create the necessary bindings, etc., in code? How, then, do I know what code to write, given these chunks of opaque XML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2024-12-15 16:57:44

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

    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);

You can use the following code to creating the bindings which is what the config is doing. I am not sure whether that will let you remove the file altogether but the application won't use the config if this is the case. Put your own values in the timeouts, etc.

    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);
司马昭之心 2024-12-15 16:57:44

WCF 设置可以在代码中设置,无需外部配置文件。地址、绑定、端点、安全性等都可以在代码中配置。这是在所有 WCF 服务之间保持一致并且不允许用户篡改这些设置的最佳方式。

我建议您向用户公开地址(主机名和端口号)并允许他们以某种方式进行配置,因为地址特定于托管 WCF 服务的位置。

WCF settings can be set in your code, with no need for an external configuration file. Addresses, bindings, endpoints, security, etc. can all be configured in code. This is the best way to be consistent across all of your WCF services and not allow users to tamper with these settings.

I suggest you expose the address (host name and port number) to users and allow them to configure this somehow because addresses are specific to where your WCF services are hosted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文