C# 调用WCF服务

 1 using System;
 2 using System.Reflection;
 3 using System.ServiceModel;
 4 using System.ServiceModel.Channels;
 5 
 6 namespace ManageSystem.Common {
 7     public class WCFChannelFactory {
 8         /// <summary>
 9         /// 执行方法   WSHttpBinding
10         /// </summary>
11         /// <typeparam name="T">服务接口</typeparam>
12         /// <param name="uri">wcf地址</param>
13         /// <param name="methodName">方法名</param>
14         /// <param name="args">参数列表</param>
15         public static object ExecuteMethod<T>(string uri, string methodName, params object[] args) {
16             BasicHttpBinding binding = new BasicHttpBinding();
17             EndpointAddress endpoint = new EndpointAddress(uri);
18 
19             using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint)) {
20                 T instance = channelFactory.CreateChannel();
21                 using (instance as IDisposable) {
22                     try {
23                         Type type = typeof(T);
24                         MethodInfo mi = type.GetMethod(methodName);
25                         return mi.Invoke(instance, args);
26                     }
27                     catch (TimeoutException) {
28                         //(instance as ICommunicationObject).Abort();
29                         //throw;
30                     }
31                     catch (CommunicationException) {
32                         //(instance as ICommunicationObject).Abort();
33                         //throw;
34                     }
35                     catch (Exception) {
36                         //(instance as ICommunicationObject).Abort();
37                         //throw;
38                     }
39                 }
40             }
41             return null;
42         }
43    }
44 }

 

转载于:https://www.cnblogs.com/zhuhc/p/3456248.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值