65.9K
CodeProject 正在变化。 阅读更多。
Home

获取 WCF 中的客户端配置

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.69/5 (9投票s)

2007年8月18日

CPOL

2分钟阅读

viewsIcon

64096

downloadIcon

1177

本文提供了一个解决方案,用于解决开发 WCF 服务和使用 Visual Studio 进行开发时配置文件覆盖的问题。

引言

本文提供了一个解决方案,以解决开发人员在使用 Visual Studio 开发涉及 WCF 服务的项目时面临的最常见问题之一。

背景

为了让您了解背景…当您使用 WCF 服务并使用 Visual Studio 进行开发时,Visual Studio 如何搞乱配置设置会让人感到烦恼。 这是一个小问题,甚至有一个更小的解决方案。 因此,这可能是最短的文章。

Using the Code

本文有两个附件。

  1. NoConfigWS.zip 是 Web 服务。 您需要解压缩内容并使用 IIS 创建一个 Web 应用程序。 我不会详细介绍如何做到这一点,如果您不熟悉,您可以通过在互联网上搜索自行解决。
  2. NoConfigClient.zip 是使用 NoConfigWS Web 服务的客户端。 这是一个控制台应用程序,它与服务通信并将结果显示回来。 您需要做的就是解压缩内容并更改 *app.config* 文件以指向您的服务。

问题

此示例将帮助您更好地了解情况。

更新前(添加服务引用并更改一些值)

<binding 
  name="BasicHttpBinding_IHelloWorldService" 
  closeTimeout="00:01:00" 
  openTimeout="00:01:00" 
  receiveTimeout="00:10:00" 
  sendTimeout="00:01:00" 
  allowCookies="false" 
  bypassProxyOnLocal="false" 
  hostNameComparisonMode="StrongWildcard" 
  maxBufferSize="131072" 
  maxBufferPoolSize="524288" 
  maxReceivedMessageSize="131072" 
  messageEncoding="Text" 
  textEncoding="utf-8" 
  transferMode="Buffered" 
  useDefaultWebProxy="true">
  <readerQuotas 
    maxDepth="32" 
    maxStringContentLength="8192" 
    maxArrayLength="16384" 
    maxBytesPerRead="4096" 
    maxNameTableCharCount="16384" />
    <security mode="None">
    <transport 
      clientCredentialType="None" 
      proxyCredentialType="None" 
      realm="" />
    <message 
      clientCredentialType="UserName" 
      algorithmSuite="Default" />
  </security>
</binding>

更改后(并更新服务引用)

<binding 
  name="BasicHttpBinding_IHelloWorldService" 
  closeTimeout="00:01:00" 
  openTimeout="00:01:00" 
  receiveTimeout="00:10:00" 
  sendTimeout="00:01:00" 
  allowCookies="false" 
  bypassProxyOnLocal="false" 
  hostNameComparisonMode="StrongWildcard" 
  maxBufferSize="65536" 
  maxBufferPoolSize="524288" 
  maxReceivedMessageSize="65536" 
  messageEncoding="Text" 
  textEncoding="utf-8" 
  transferMode="Buffered" 
  useDefaultWebProxy="true">
  <readerQuotas 
    maxDepth="32" 
    maxStringContentLength="8192" 
    maxArrayLength="16384" 
    maxBytesPerRead="4096" 
    maxNameTableCharCount="16384" />
  <security mode="None">
    <transport 
      clientCredentialType="None" 
      proxyCredentialType="None" 
      realm="" />
    <message 
      clientCredentialType="UserName" 
      algorithmSuite="Default" />
  </security>
</binding>

如果您必须将某些服务的某些设置(例如,maxReceivedMessageSizemaxStringContentLength)更改为与默认值不同的值,这将变得非常痛苦。 每次更新服务引用时,Visual Studio 都会将更新的设置替换为默认值。 对于多个开发人员开发服务的情况,这个问题只会成倍增长。

解决方案

对于这个问题有几种解决方案,这里介绍的只是其中之一。

无论您使用什么开发环境来创建您的服务,无论是使用 Visual Studio 还是直接生成它,SVCUtil 都会为代理类生成多个构造函数(确切地说是五个)。 猜猜看,其中一个将 BindingEndpointAddress 作为参数。

public HelloWorldServiceClient()
public HelloWorldServiceClient(string endpointConfigurationName)
public HelloWorldServiceClient(string endpointConfigurationName, string remoteAddress)
public HelloWorldServiceClient
	(string endpointConfigurationName, EndpointAddress remoteAddress)
public HelloWorldServiceClient(Binding binding, EndpointAddress remoteAddress)

现在,我们的工作是创建这些对象并通过传递这些对象来创建代理。

HelloWorldServiceClient helloWorldClient = new HelloWorldServiceClient(
      ProxyHelper.GetBinding(),
      ProxyHelper.GetEndpoint() );

GetBinding()GetEndpoint() 是我用来创建这些对象的辅助方法。 本文的完整源代码可作为下载文件获得。

使用这种方法,您最终的 *Config* 文件将如下所示

<configuration>
  <appSettings>
    <add 
      key="webserviceURL"
      value="https:///NoConfigWS/Service.svc"/>
  </appSettings>
</configuration>

摘要

我认为,这对于您的最终用户来说更容易维护,顺便说一句,谁比开发人员(当然,还有高级网络管理员)更了解高级配置设置呢?

一旦完成,您就可以安息了…… 我的意思是您可以继续开发您的服务,而无需再担心 Visual Studio 篡改您的配置文件了。

历史

  • 2007 年 8 月 18 日:初始帖子
© . All rights reserved.