如何为 wcf 服务配置客户端? [英] How to config clients for a wcf service?

查看:41
本文介绍了如何为 wcf 服务配置客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 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 clientProxy = chanFac.CreateChannel();

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

解决方案

使用 app.config根据您的需要进行调整):

<预><代码><配置><system.serviceModel><绑定><基本HttpBinding><绑定名称=用户名安全"><安全模式=消息"><message clientCredentialType="用户名"/></安全></binding></basicHttpBinding></绑定><客户><端点地址="http://localhost:8888/MyService" binding="basicHttpBinding"bindingConfiguration="UserNameSecurity" contract="IMyService"/><端点地址="net.tcp://localhost:8484/MyService/Mex"绑定=mexTcpBinding"绑定配置=""contract="IMetadataExchange" name="mexNetTcp"/></客户端></system.serviceModel></配置>

WCF 配置中详细记录了该部分及其可能的值和子部分.

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

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

马克

I am developing a wcf service . I have created two dll's one for message contracts and one for service contracts interfaces. I share these two dll's with server and client. I am not using AddServiceReference i am using ChannelFactory class to create proxies. Following is the code which i am using to create client proxies:

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

Now I have to create the binding and EndpointAddress in the code,what i want that this should come from app.config file , how can i do it so that i don't need to write binding and endpoint everytime in the code.... Any help is appreciated..

解决方案

Use an app.config like this (when you use "Add Service Reference" from Visual Studio, VS will typically create this for you automatically - and you just need to tweak it to your needs):

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

The section and its possible values and subsection are well documented in the WCF configuration.

Alternatively, in VS 2008 SP1, you can use the "WCF Service Configuration Editor" - see it in "Tools > WCF Service Configuration Editor".

It allows you to visually define and modify your client config settings. Once you've launched it from the Tools menu, after that, you can acutally even right-click on the app.config in your Solution Explorer and launch it from there (using that app.config as its basis).

Marc

这篇关于如何为 wcf 服务配置客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆