WCF帮助类

 1 using BJSW.ZTFX.Client.Silverlight.MapBusinessService;
 2 using System.ServiceModel;
 3 using System.SL.Application;
 4 using System.Windows;
 5 
 6 namespace BJSW.ZTFX.Client.Silverlight.Common
 7 {
 8     public class WCFHandler : WCFHelper
 9     {
10         public static string WCF_URL = "http://localHost:8012/";
11 
12         /// <summary>
13         /// 地图业务数据(预警信息,雨情信息,河道水情,水库水情)
14         /// </summary>
15         /// <returns></returns>
16         public static MapBusinessDataClient GetMapBusinessDataClient()
17         {
18             if (Application.Current.IsRunningOutOfBrowser)
19                 return new MapBusinessDataClient(GetBindHttp("BasicHttpBinding_MapBusinessData"), new EndpointAddress(WCF_URL + "MapBusinessData.svc"));
20             else
21                 return new MapBusinessDataClient(GetBindHttp("BasicHttpBinding_MapBusinessData"), new EndpointAddress(WCF_URL + "MapBusinessData.svc"));
22 
23         }
24     }
25 }
View Code
 1 using System.Collections.Generic;
 2 using System.ServiceModel;
 3 using System.ServiceModel.Channels;
 4 using System.Text;
 5 
 6 namespace System.SL.Application
 7 {
 8     /// <summary>
 9     /// 生成WCF的代理客户端
10     /// @create date:2010/4/25
11     /// </summary>
12     public abstract class WCFHelper
13     {
14         public static string URL;
15         protected static BasicHttpBinding _BindHttp;
16         protected static CustomBinding _BindCustom;
17         static WCFHelper()
18         {
19             _BindHttp = new BasicHttpBinding()
20             {
21                 MaxReceivedMessageSize = 2147483647,
22                 MaxBufferSize = 2147483647,
23                 CloseTimeout = TimeSpan.FromMinutes(10),
24                 OpenTimeout = TimeSpan.FromMinutes(10),
25                 ReceiveTimeout = TimeSpan.FromMinutes(10),
26                 SendTimeout = TimeSpan.FromMinutes(10),
27                 TextEncoding = Encoding.UTF8,
28                 TransferMode = System.ServiceModel.TransferMode.Buffered,
29                 Name = "temp_http"
30             };
31         
32             var elements = new List<BindingElement>();
33             elements.Add(new BinaryMessageEncodingBindingElement());
34             elements.Add(new HttpTransportBindingElement() { MaxReceivedMessageSize = 2147483647, MaxBufferSize = 2147483647 });
35             _BindCustom= new CustomBinding(elements) 
36             { 
37                 ReceiveTimeout = TimeSpan.FromMinutes(10), 
38                 SendTimeout =  TimeSpan.FromMinutes(10),
39                 OpenTimeout = TimeSpan.FromMinutes(10), 
40                 CloseTimeout = TimeSpan.FromMinutes(10),
41                 Name = "temp_custom"
42             };
43         }
44 
45         #region 生成客户端Bind
46         protected static BasicHttpBinding GetBindHttp()
47         {
48             return _BindHttp;
49         }
50         protected static BasicHttpBinding GetBindHttp(string bindingName)
51         {
52             _BindHttp.Name = bindingName;
53             return _BindHttp;
54         }
55 
56         protected static CustomBinding GetBindCustom()
57         {
58             return _BindCustom;
59         }
60         protected static CustomBinding GetBindCustom(string bindingName)
61         {
62             _BindCustom.Name = bindingName;
63             return _BindCustom;
64         }
65         #endregion
66 
67         #region 生成WCF客户端代理
68         public static T GetWCFClient<T>() where T : new()
69         {
70             return new T();
71         }
72         #endregion
73     }
74 }
View Code


也可以不用设置地址

using EasySL.UI.MapAlyRef;
using EasySL.UI.MapBusinessService;
using System.ServiceModel;
using System.SL.Application;
using System.SL.Map;
using System.Windows;

namespace EasySL.UI.Common
{
    public class WCFHandler : WCFHelper
    {
        public static string WCF_URL = "http://localhost/EasySL.Web/WCF/";
    
        /// <summary>
        /// 地图业务数据(预警信息,雨情信息,河道水情,水库水情)
        /// </summary>
        /// <returns></returns>
        public static MapBusinessDataClient GetMapBusinessDataClient()
        {
            if (Application.Current.IsRunningOutOfBrowser)
                return new MapBusinessDataClient(GetBindHttp("BasicHttpBinding_MapBusinessData"), new EndpointAddress("../WCF/MapBusinessData.svc"));
            else
                return new MapBusinessDataClient(GetBindHttp("BasicHttpBinding_MapBusinessData"), new EndpointAddress("../WCF/MapBusinessData.svc"));

        }

        /// <summary>
        /// 等值分析
        /// </summary>
        /// <returns></returns>
        public static MapAlyClient GetMapAlyClient()
        {
            return new MapAlyClient(GetBindHttp("BasicHttpBinding_MapBusinessData"), new EndpointAddress(MapCfgHelper.BaseMapInfo.MapAnalysisUrl));
        }
    }
}

 调用

 1  var client = WCFHandler.GetMapBusinessDataClient();
 2             //MapBusinessDataClient client = new MapBusinessDataClient();
 3             client.OpenAsync();
 4             client.QueryZQaccpCompleted += (s, e) =>
 5             {
 6                 try
 7                 {
 8                     if (e.Error == null && e.Result != null)
 9                     {
10                         if (e.Result.Count == 0)
11                         {
12                          }
13                    }
14                 }
15                 catch (Exception ex)
16                 {
17                     MessageBox.Show(ex.ToString());
18                 }
19             };
20             client.QueryZQaccpAsync(View.BTime, View.ETime, View.type);
21             client.CloseAsync();
View Code
原文地址:https://www.cnblogs.com/zxbzl/p/4169400.html