WCF服务的方法名固定,但是地址是动态的。怎么动态调用WCF服务?

zengjd 2012-04-16 03:24:26
WCF服务的方法名固定,但是地址是动态的。怎么动态调用WCF服务?
就是说动态引用WCF服务。通过参数在运行时调用的改变WCF地址。
应该怎么做?
...全文
426 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
假设你已经根据某个endpoint生成了代理类,通常情况下,你是这样调用wcf的:
var client = new MyWCFService.MyServiceClient();
client.MyMethod();

现在因为endpoint地址是动态的,你只需要加这一句:

client.Endpoint.Address = new EndpointAddress(new Uri(endpointAddress));
ycg_893 2012-04-16
  • 打赏
  • 举报
回复
这里错了,变成我的了,应该是

客户端引用对象 cilent = new 客户端引用对象(回调对象, GetNetTcpBinding(true), endadd);
ycg_893 2012-04-16
  • 打赏
  • 举报
回复
以上的方法不需要配置文件,你就算删了配置文件都没有问题.
ycg_893 2012-04-16
  • 打赏
  • 举报
回复
当然可以

定义一个绑定方法 这里使用的是tcp,实际根据你的情况
private static NetTcpBinding GetNetTcpBinding(bool TransactionFlow)
{
NetTcpBinding tcp = new NetTcpBinding();
tcp.TransactionFlow = TransactionFlow;
tcp.TransactionProtocol = System.ServiceModel.TransactionProtocol.WSAtomicTransactionOctober2004;
tcp.CloseTimeout = new TimeSpan(0, 1, 0);
tcp.OpenTimeout = new TimeSpan(0, 1, 0);
tcp.ReceiveTimeout = new TimeSpan(0, 20, 0);
tcp.SendTimeout = new TimeSpan(0, 1, 0);
tcp.TransferMode = TransferMode.Buffered;
tcp.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
tcp.MaxBufferPoolSize = int.MaxValue;
tcp.MaxBufferSize = int.MaxValue;
tcp.MaxReceivedMessageSize = int.MaxValue;
tcp.ReaderQuotas.MaxArrayLength = int.MaxValue;
tcp.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
tcp.ReaderQuotas.MaxDepth = 32;
tcp.ReaderQuotas.MaxNameTableCharCount = 16384;
tcp.ReaderQuotas.MaxStringContentLength = int.MaxValue;
tcp.MaxConnections = 10; //最大连接数
tcp.ListenBacklog = 10; //最大挂起
return tcp;
}

//定义一个终结点与地址:服务名需要根据发布的hex确定
EndpointAddress endadd = new EndpointAddress("net.tcp://服务器址址:端口/服务名");

//回调对象根据实际,有些没有
客户端引用对象 cilent = new TaskService.TaskServiceClient(回调对象, GetNetTcpBinding(true), endadd);

//若需要身份验证
NetworkCredential credential = cilent.ChannelFactory.Credentials.Windows.ClientCredential;
credential.UserName = "用户名";
credential.Password = "密码";

cilent.方法()
5035080 2012-04-16
  • 打赏
  • 举报
回复
wcf没搞过,不错webservice搞过,打开app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="RTXServiceSoap" 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>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://172.18.238.252/rtxservice.asmx" binding="basicHttpBinding"
bindingConfiguration="RTXServiceSoap" contract="ServiceReference1.RTXServiceSoap"
name="RTXServiceSoap" />
</client>
</system.serviceModel>
</configuration>
修改ip地址就ok
烟波钓 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

搞个配置文件 读取动态配置的东西 然后反射


不用那么复杂吧!!
[/Quote]
又想动态、智能;又想要性能、代码编写阶段简单 这明明就是鱼和熊掌不可兼得的东西

软件复杂度守恒定律
玲cc 2012-04-16
  • 打赏
  • 举报
回复
2楼正解。
zengjd 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

搞个配置文件 读取动态配置的东西 然后反射
[/Quote]

不用那么复杂吧!!
烟波钓 2012-04-16
  • 打赏
  • 举报
回复
搞个配置文件 读取动态配置的东西 然后反射

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧