using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Reflection; namespace wcf.wcfbase
{
///<summary>
/// 使用ChannelFactory为wcf客户端创建独立通道
///</summary>
public class WcfChannelFactory
{
public WcfChannelFactory()
{
} ///<summary>
/// 执行方法 WSHttpBinding
///</summary>
///<typeparam name="T">服务接口</typeparam>
///<param name="uri">wcf地址</param>
///<param name="methodName">方法名</param>
///<param name="args">参数列表</param>
public static object ExecuteMetod<T>(string uri, string methodName, paramsobject[] args)
{
//BasicHttpBinding binding = new BasicHttpBinding(); //出现异常:远程服务器返回错误: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.。
WSHttpBinding binding = new WSHttpBinding();
EndpointAddress endpoint = new EndpointAddress(uri); using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint))
{
T instance = channelFactory.CreateChannel();
using (instance as IDisposable)
{
try
{
Type type = typeof(T);
MethodInfo mi = type.GetMethod(methodName);
return mi.Invoke(instance, args);
}
catch (TimeoutException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (CommunicationException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (Exception vErr)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
}
} //nettcpbinding 绑定方式
public static object ExecuteMethod<T>(string pUrl, string pMethodName,paramsobject[] pParams)
{
EndpointAddress address = new EndpointAddress(pUrl);
Binding bindinginstance = null;
NetTcpBinding ws = new NetTcpBinding();
ws.MaxReceivedMessageSize = ;
ws.Security.Mode = SecurityMode.None;
bindinginstance = ws;
using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance, address))
{
T instance = channel.CreateChannel();
using (instance as IDisposable)
{
try
{
Type type = typeof(T);
MethodInfo mi = type.GetMethod(pMethodName);
return mi.Invoke(instance, pParams);
}
catch (TimeoutException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (CommunicationException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (Exception vErr)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
}
} //调用示例:
string uri = "http://localhost:9998/mywcf/Service";
object o = ExecuteMetod<IService>(uri, "Add",12.0,13.0);
Console.WriteLine(o.ToString());
Console.ReadKey(); //简单方法,不考虑太多,直接调用:
EndpointAddress address1 = new EndpointAddress("http://localhost:9998/mywcf/Service");
ServiceClient service1 = new ServiceClient(new WSHttpBinding(), address1);
Console.WriteLine(service1.Add(12.0, 13.0).ToString());
}
}

最新文章

  1. codevs 2021 中庸之道
  2. MySQL索引结构--由 B-/B+树看
  3. CreateFile函数详解
  4. IE8的Textarea滚动条乱跳的解决方案
  5. 【索引】用PS3手柄在安卓设备上玩游戏系列
  6. Java Web 入门(一)使用 Intellij IDEA 14.1.5 创建 Maven Web项目
  7. hdu Diophantus of Alexandria(素数的筛选+分解)
  8. 一句话搞定-phpStudy安装yaf扩展
  9. UniCode 下 CString 转 char* 的方法(转)
  10. django路由系统URLS
  11. 读书笔记-iOS核心动画高级技巧
  12. eclipse library jar包 使用总结 MD
  13. Python MySQLdb select(选择) 封装
  14. BZOJ3672: [Noi2014]购票【CDQ分治】【点分治】【斜率优化DP】
  15. netcore问题总结
  16. 【Android】3.25 示例25--调启百度地图
  17. Shrio03 Authenticator、配置多个Realm、SecurityManager认证策略
  18. QTcpServer实现多客户端连接
  19. Python2和Python3共存安装
  20. JsonHelper修改4.0

热门文章

  1. 【数论】【素数判定】CODEVS 2851 菜菜买气球
  2. 一年的天数 Exercise06_16
  3. 对三个数排序 Exercise06_05
  4. 十. 图形界面(GUI)设计10.菜单
  5. 【IntellJ IDEA】idea忽略隐藏文件、文件夹的设置操作
  6. ASP.NET Core 1.0基础之诊断
  7. http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SVYGqy364vDfv6AY4ERPa
  8. mysql 5.7 安装手册(for linux)
  9. 解读Spark Streaming RDD的全生命周期
  10. [转]Nginx调用远程php-fpm