日期:2014-05-18 浏览次数:21085 次
定义一个绑定方法 这里使用的是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.方法()
------解决方案--------------------
假设你已经根据某个endpoint生成了代理类,通常情况下,你是这样调用wcf的:
var client = new MyWCFService.MyServiceClient();
client.MyMethod();
现在因为endpoint地址是动态的,你只需要加这一句:
client.Endpoint.Address = new EndpointAddress(new Uri(endpointAddress));