【问题标题】:How to config clients for a wcf service?如何为 wcf 服务配置客户端?
【发布时间】:2009-08-23 07:20:55
【问题描述】:

我正在开发 wcf 服务。 我创建了两个 dll,一个用于消息合同,一个用于服务合同接口。 我与服务器和客户端共享这两个 dll。 我没有使用 AddServiceReference 我使用 ChannelFactory 类来创建代理。 以下是我用来创建客户端代理的代码:

BasicHttpBinding binding = new BasicHttpBinding(); 
EndpointAddress endpoint = new EndpointAddress(new Uri   ("http://localhost:8989/HelloService/"));
ChannelFactory<IHello> chanFac = new ChannelFactory<IHello>(binding, endpoint);
IHello clientProxy = chanFac.CreateChannel();

现在我必须在代码中创建绑定和端点地址,我希望这应该来自 app.config 文件,我该怎么做才能不需要每次都在代码中编写绑定和端点…… 任何帮助表示赞赏..

【问题讨论】:

    标签: wcf


    【解决方案1】:

    使用这样的 app.config(当您使用 Visual Studio 中的“添加服务引用”时,VS 通常会自动为您创建它 - 您只需根据需要对其进行调整):

    <configuration>
        <system.serviceModel>
            <bindings>
              <basicHttpBinding>
                <binding name="UserNameSecurity">
                  <security mode="Message">
                    <message clientCredentialType="UserName"/>
                  </security>
                </binding>
              </basicHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://localhost:8888/MyService" binding="basicHttpBinding"
                    bindingConfiguration="UserNameSecurity" contract="IMyService" />
                <endpoint address="net.tcp://localhost:8484/MyService/Mex" 
                          binding="mexTcpBinding"
                          bindingConfiguration="" 
                          contract="IMetadataExchange" name="mexNetTcp" />
            </client>
        </system.serviceModel>
    </configuration>
    

    该部分及其可能的值和子部分在 WCF 配置中有详细记录。

    或者,在 VS 2008 SP1 中,您可以使用“WCF 服务配置编辑器” - 在“工具 > WCF 服务配置编辑器”中查看它。

    它允许您直观地定义和修改您的客户端配置设置。从“工具”菜单启动它后,您甚至可以在解决方案资源管理器中右键单击 app.config 并从那里启动它(使用该 app.config 作为其基础)。

    马克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 2016-11-28
      • 2011-07-01
      • 2012-04-08
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多