您的位置:首页 > 其它

知道WCF的地址用工厂通道方式快速调用WCF

2012-06-27 14:03 239 查看
1 using System;
2 using System.ServiceModel;
3 using System.ServiceModel.Description;
4 using System.ServiceModel.Channels;
5
6 namespace ZhiYuan.ServiceProxy
7 {
8 public class WCFClient<T>
9 {

public static T CreateService(Binding bind, EndpointAddress address)
{
ChannelFactory<T> channelFactory = new ChannelFactory<T>(bind);
return channelFactory.CreateChannel(address);
}
public static T CreateService(string uri)
{

#region TCP/IP方案
// NetTcpBinding bind = new NetTcpBinding();
// EndpointAddress address = new EndpointAddress("net.tcp://127.0.0.1:1785/Service");
// EndpointAddress metaAddress = new EndpointAddress("net.tcp://127.0.0.1:1785/Service/MEX");
#endregion

BasicHttpBinding bind = new BasicHttpBinding();
bind.MaxBufferSize = int.MaxValue;
bind.MaxReceivedMessageSize = int.MaxValue;
bind.MaxBufferPoolSize = int.MaxValue;
bind.ReaderQuotas.MaxArrayLength = int.MaxValue;
bind.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
bind.ReaderQuotas.MaxDepth = int.MaxValue;
bind.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
bind.ReaderQuotas.MaxStringContentLength = int.MaxValue;

bind.SendTimeout = new TimeSpan(0,5,60);
EndpointAddress address = new EndpointAddress(uri);

ChannelFactory<T> channelFactory = new ChannelFactory<T>(bind);
return CreateService(bind, address);
}

/// <summary>
/// 采用通道工厂的方式生成客户端服务对象实例
/// </summary>
/// <param name="bind"></param>
/// <param name="address"></param>
public ZhiYuan.ServiceContract.Member.ILoginService CreateLoginService()
{
return WCFClient<ZhiYuan.ServiceContract.Member.ILoginService>.CreateService("http://localhost:1785/Member/LoginService.svc");

}

public ZhiYuan.ServiceContract.Member.IMemberService CreateMemberService()
{

return WCFClient<ZhiYuan.ServiceContract.Member.IMemberService>.CreateService("http://localhost:1785/Member/MemberService.svc");

}

}63 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: