将app.config中的WCF服务客户端设置导入代码结构

时间:2014-04-29 15:17:28

标签: c# vb.net web-services wcf

我已经编写了一个hello world WCF Web服务。我的用于使用该服务的客户端应用程序的App.config文件如下所示。我希望在客户端应用程序的代码中设置所有这些数据。我怎样才能做到这一点?我想编写一个类,它封装了我需要在代码中设置的所有属性和方法。我已经捕获了几乎所有的Endpoint和Binding属性,但是我在使用Contract设置时遇到了一些困难。如果有人能够在没有配置文件的情况下使用它,我会感兴趣。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IHelloWorldService" allowCookies="true"
             maxReceivedMessageSize="20000000" maxBufferSize="20000000"                         maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="32"
                maxArrayLength="200000000"
                maxStringContentLength="200000000"/>
      <security mode="None">
        <transport clientCredentialType="None" />
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>

 <endpoint address="http://localhost/HelloWorldService.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHelloWorldService"
      contract="HelloWordServiceReference.IHelloWorldService"
      name="BasicHttpBinding_IHelloWorldService" />

</client>
</system.serviceModel>

</configuration>

1 个答案:

答案 0 :(得分:0)

如果您想添加所有客户端属性,服务类,wcf服务会提供服务代理。因此,当您创建wcf服务(自托管或Web服务器)并通过url visual studio添加服务引用时会发生什么,将自动生成代理类,该类将包含从客户端使用Web服务所需的所有内容。但是,如果您想手动创建客户端代理,可以按照此网址http://msdn.microsoft.com/en-us/library/ms734691(v=vs.110).aspx

进行操作
相关问题