如何为wcf服务配置客户端?
我正在开发wcf服务。 我创建了两个dll,一个用于消息契约,另一个用于服务契约接口。 我与服务器和客户端共享这两个dll。 我没有使用AddServiceReference,而是使用ChannelFactory类创建代理。 以下是我用来创建客户端代理的代码:如何为wcf服务配置客户端?,wcf,Wcf,我正在开发wcf服务。 我创建了两个dll,一个用于消息契约,另一个用于服务契约接口。 我与服务器和客户端共享这两个dll。 我没有使用AddServiceReference,而是使用ChannelFactory类创建代理。 以下是我用来创建客户端代理的代码: BasicHttpBinding binding = new BasicHttpBinding(); EndpointAddress endpoint = new EndpointAddress(new Uri ("http://l
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();
BasicHttpBinding=new BasicHttpBinding();
EndpointAddress endpoint=新的EndpointAddress(新Uri(“http://localhost:8989/HelloService/"));
ChannelFactory chanFac=新的ChannelFactory(绑定,端点);
IHello clientProxy=chanFac.CreateChannel();
现在我必须在代码中创建绑定和端点地址,我希望它来自app.config文件,我如何才能做到这一点,这样我就不需要每次在代码中编写绑定和端点。。。。
非常感谢您的帮助。使用类似的app.config(当您使用Visual Studio中的“添加服务引用”时,VS通常会自动为您创建此服务,您只需根据需要调整它即可):
该节及其可能的值和小节在WCF配置中有很好的文档记录
或者,在VS2008SP1中,您可以使用“WCF服务配置编辑器”-请参见“工具>WCF服务配置编辑器”
它允许您直观地定义和修改客户端配置设置。从“工具”菜单启动后,您甚至可以右键单击解决方案资源管理器中的app.config并从那里启动它(使用该app.config作为基础)
马克
<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>