/// <summary> /// 重新连接wcf服务 /// </summary> /// <param name="mainfrm"></param> public static void ReConnection() { try { NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_WCFHandlerService"); //binding.OpenTimeout = TimeSpan.FromSeconds(10); //binding.TransferMode = TransferMode.Buffered; DuplexChannelFactory <IWCFHandlerService> mChannelFactory = new DuplexChannelFactory <IWCFHandlerService>(AppGlobal.cache.GetData("ClientService") as IClientService, binding, System.Configuration.ConfigurationSettings.AppSettings["WCF_endpoint"]); IWCFHandlerService wcfHandlerService = mChannelFactory.CreateChannel(); using (var scope = new OperationContextScope(wcfHandlerService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); wcfHandlerService.Heartbeat(AppGlobal.cache.GetData("WCFClientID").ToString()); } if (AppGlobal.cache.Contains("WCFService")) { AppGlobal.cache.Remove("WCFService"); } AppGlobal.cache.Add("WCFService", wcfHandlerService); } catch (Exception err) { throw new Exception(err.Message); } }
/// <summary> /// 卸载连接 /// </summary> public static void UnConnection() { if (AppGlobal.cache.GetData("WCFClientID") == null) { return; } bool b = false; string mClientID = AppGlobal.cache.GetData("WCFClientID").ToString(); IWCFHandlerService mWcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; if (mClientID != null) { using (var scope = new OperationContextScope(mWcfService as IContextChannel)) { try { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", "http://www.efwplus.cn/", AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); var cmd = System.ServiceModel.Channels.MessageHeader.CreateHeader("CMD", "http://www.efwplus.cn/", "Quit"); OperationContext.Current.OutgoingMessageHeaders.Add(cmd); b = mWcfService.UnDomain(mClientID); } catch { } } } }
/// <summary> /// 发送心跳 /// </summary> /// <returns></returns> public static bool Heartbeat() { try { bool ret = false; IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; using (var scope = new OperationContextScope(_wcfService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); ret = _wcfService.Heartbeat(AppGlobal.cache.GetData("WCFClientID").ToString()); } if (ret == false)//表示服务主机关闭过,丢失了clientId,必须重新创建连接 { CreateConnection(AppGlobal.cache.GetData("ClientService") as IClientService); } return(ret); } catch { ReConnection();//连接服务主机失败,重连 return(false); } }
/// <summary> /// 发送心跳 /// </summary> /// <returns></returns> public static bool Heartbeat() { try { bool ret = false; IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; using (var scope = new OperationContextScope(_wcfService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); ret = _wcfService.Heartbeat(AppGlobal.cache.GetData("WCFClientID").ToString()); if (WcfClientManage.ServerConfigRequestState == false) { //重新获取服务端配置,如:是否压缩Json、是否加密Json string serverConfig = _wcfService.ServerConfig(); WcfClientManage.IsHeartbeat = serverConfig.Split(new char[] { '#' })[0] == "1" ? true : false; WcfClientManage.HeartbeatTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[1]); WcfClientManage.IsMessage = serverConfig.Split(new char[] { '#' })[2] == "1" ? true : false; WcfClientManage.MessageTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[3]); WcfClientManage.IsCompressJson = serverConfig.Split(new char[] { '#' })[4] == "1" ? true : false; WcfClientManage.IsEncryptionJson = serverConfig.Split(new char[] { '#' })[5] == "1" ? true : false; if (WcfClientManage.IsHeartbeat) { //开启发送心跳 if (timer == null) { StartTimer(); } else { timer.Start(); } } else { if (timer != null) { timer.Stop(); } } WcfClientManage.ServerConfigRequestState = true; } } if (ret == false) //表示服务主机关闭过,丢失了clientId,必须重新创建连接 { mChannelFactory.Abort(); //关闭原来通道 CreateConnection(AppGlobal.cache.GetData("ClientService") as IClientService); } return(ret); } catch { WcfClientManage.ServerConfigRequestState = false; ReConnection(false);//连接服务主机失败,重连 return(false); } }
private bool ServerConfigRequestState = false; //获取服务端配置读取状态 /// <summary> /// 创建连接 /// </summary> public void CreateConnection() { IWCFHandlerService wcfHandlerService = mChannelFactory.CreateChannel(); mConn.WcfService = wcfHandlerService; string serverConfig = null; AddMessageHeader(wcfHandlerService as IContextChannel, "", (() => { mConn.ClientID = wcfHandlerService.CreateDomain(mConn.ClientName);//创建连接获取ClientID if (ServerConfigRequestState == false) { //重新获取服务端配置,如:是否压缩Json、是否加密Json serverConfig = wcfHandlerService.ServerConfig(); ServerConfigRequestState = true; } })); if (!string.IsNullOrEmpty(serverConfig)) { IsHeartbeat = serverConfig.Split(new char[] { '#' })[0] == "1" ? true : false; HeartbeatTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[1]); IsMessage = serverConfig.Split(new char[] { '#' })[2] == "1" ? true : false; MessageTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[3]); IsCompressJson = serverConfig.Split(new char[] { '#' })[4] == "1" ? true : false; IsEncryptionJson = serverConfig.Split(new char[] { '#' })[5] == "1" ? true : false; serializeType = (SerializeType)Convert.ToInt32(serverConfig.Split(new char[] { '#' })[6]); if (IsHeartbeat) { //开启发送心跳 if (timer == null) { StartTimer(); } else { timer.Start(); } } else { if (timer != null) { timer.Stop(); } } } if (backConfig != null) { backConfig(IsMessage, MessageTime); } //创建连接成功后回调 if (createconnAction != null) { createconnAction.BeginInvoke(null, null); } }
public static List <dwPlugin> GetWcfServicesAllInfo() { IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; using (var scope = new OperationContextScope(_wcfService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); string ret = _wcfService.WcfServicesAllInfo(); return(JsonConvert.DeserializeObject <List <dwPlugin> >(ret)); } }
/// <summary> /// 获取所有服务插件的控制器和方法 /// </summary> /// <returns></returns> public List <EFWCoreLib.WcfFrame.ServerController.dwPlugin> GetWcfServicesAllInfo() { IWCFHandlerService _wcfService = mConn.WcfService; List <EFWCoreLib.WcfFrame.ServerController.dwPlugin> list = new List <EFWCoreLib.WcfFrame.ServerController.dwPlugin>(); AddMessageHeader(_wcfService as IContextChannel, "", (() => { string ret = _wcfService.WcfServicesAllInfo(); list = JsonConvert.DeserializeObject <List <EFWCoreLib.WcfFrame.ServerController.dwPlugin> >(ret); })); return(list); }
/// <summary> /// 向服务发送请求 /// </summary> /// <param name="controller">控制器名称</param> /// <param name="method">方法名称</param> /// <param name="jsondata">数据</param> /// <returns>返回Json数据</returns> public static string Request(string controller, string method, string jsondata) { IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; string retJson; using (var scope = new OperationContextScope(_wcfService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); retJson = _wcfService.ProcessRequest(AppGlobal.cache.GetData("WCFClientID").ToString(), controller, method, jsondata); } return(retJson); }
/// <summary> /// 向服务发送异步请求 /// 客户端建议不要用多线程,都采用异步请求方式 /// </summary> /// <param name="controller">控制器名称</param> /// <param name="method">方法名称</param> /// <param name="jsondata">数据</param> /// <returns>返回Json数据</returns> public static IAsyncResult RequestAsync(string controller, string method, string jsondata, Action <string> action) { try { if (WcfClientManage.IsCompressJson) //开启压缩 { jsondata = ZipComporessor.Compress(jsondata); //压缩传入参数 //jsondata = JsonComporessor.Compress(jsondata);//压缩传入参数 } IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; //string retJson; IAsyncResult result = null; using (var scope = new OperationContextScope(_wcfService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); AsyncCallback callback = delegate(IAsyncResult r) { string retJson = _wcfService.EndProcessRequest(r); if (WcfClientManage.IsCompressJson) { retJson = ZipComporessor.Decompress(retJson); //retJson = JsonComporessor.Decompress(retJson); } action(retJson); }; result = _wcfService.BeginProcessRequest(AppGlobal.cache.GetData("WCFClientID").ToString(), controller, method, jsondata, callback, null); } if (WcfClientManage.IsHeartbeat == false)//如果没有启动心跳,则请求发送心跳 { WcfClientManage.ServerConfigRequestState = false; Heartbeat(); } //return retJson; return(result); } catch (Exception e) { WcfClientManage.ServerConfigRequestState = false; ReConnection(true);//连接服务主机失败,重连 throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!"); } }
public void DistributedAllCacheSync(List <CacheObject> cachelist) { if (mConn == null) { throw new Exception("还没有创建连接!"); } try { IWCFHandlerService _wcfService = mConn.WcfService; _wcfService.DistributedAllCacheSync(cachelist); } catch (Exception e) { throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!"); } }
public CacheIdentify DistributedCacheSyncIdentify(CacheIdentify cacheId) { if (mConn == null) { throw new Exception("还没有创建连接!"); } try { IWCFHandlerService _wcfService = mConn.WcfService; return(_wcfService.DistributedCacheSyncIdentify(cacheId)); } catch (Exception e) { throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!"); } }
/// <summary> /// 注册远程插件 /// </summary> /// <param name="ServerIdentify"></param> /// <param name="plugin"></param> public void RegisterReplyPlugin(string ServerIdentify, string[] plugin) { if (mConn == null) { throw new Exception("还没有创建连接!"); } try { IWCFHandlerService _wcfService = mConn.WcfService; _wcfService.RegisterReplyPlugin(ServerIdentify, plugin); } catch (Exception e) { throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!"); } }
/// <summary> /// 重新连接wcf服务,服务端存在ClientID /// </summary> /// <param name="isRequest">是否请求调用</param> private void ReConnection(bool isRequest) { try { IWCFHandlerService wcfHandlerService = mChannelFactory.CreateChannel(); mConn.WcfService = wcfHandlerService; if (isRequest == true) //避免死循环 { Heartbeat(); //重连之后必须再次调用心跳 } } catch { //throw new Exception(err.Message); } }
/// <summary> /// 重新连接wcf服务,服务端存在ClientID /// </summary> /// <param name="mainfrm"></param> public static void ReConnection(bool isRequest) { try { IWCFHandlerService wcfHandlerService = mChannelFactory.CreateChannel(); if (AppGlobal.cache.Contains("WCFService")) { AppGlobal.cache.Remove("WCFService"); } AppGlobal.cache.Add("WCFService", wcfHandlerService); if (isRequest == true) //避免死循环 { Heartbeat(); //重连之后必须再次调用心跳 } } catch { //throw new Exception(err.Message); } }
/// <summary> /// 向服务发送请求 /// </summary> /// <param name="controller">控制器名称</param> /// <param name="method">方法名称</param> /// <param name="jsondata">数据</param> /// <returns>返回Json数据</returns> public static IAsyncResult RequestAsync(string controller, string method, string jsondata, Action <string> action) { IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; //string retJson; IAsyncResult result = null; using (var scope = new OperationContextScope(_wcfService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); AsyncCallback callback = delegate(IAsyncResult r) { string retJson = _wcfService.EndProcessRequest(r); action(retJson); }; result = _wcfService.BeginProcessRequest(AppGlobal.cache.GetData("WCFClientID").ToString(), controller, method, jsondata, callback, null); } //return retJson; return(result); }
/// <summary> /// 卸载连接 /// </summary> public void UnConnection() { if (mConn == null) { return; } string mClientID = mConn.ClientID; IWCFHandlerService mWcfService = mConn.WcfService; if (mClientID != null) { try { AddMessageHeader(mWcfService as IContextChannel, "Quit", (() => { mWcfService.UnDomain(mClientID); })); //mChannelFactory.Close();//关闭通道 (mWcfService as IContextChannel).Close(); if (timer != null)//关闭连接必须停止心跳 { timer.Stop(); } } catch { if ((mWcfService as IContextChannel) != null) { (mWcfService as IContextChannel).Abort(); } } mConn = null; } }
/// <summary> /// 向服务发送异步请求 /// 客户端建议不要用多线程,都采用异步请求方式 /// </summary> /// <param name="controller">插件名@控制器名称</param> /// <param name="method">方法名称</param> /// <param name="jsondata">数据</param> /// <returns>返回Json数据</returns> public IAsyncResult RequestAsync(string controller, string method, Action <ClientRequestData> requestAction, Action <ServiceResponseData> action) { if (mConn == null) { throw new Exception("还没有创建连接!"); } try { ClientRequestData requestData = new ClientRequestData(IsCompressJson, IsEncryptionJson, serializeType); if (requestAction != null) { requestAction(requestData); } string jsondata = requestData.GetJsonData(); //获取序列化的请求数据 if (requestData.Iscompressjson) //开启压缩 { jsondata = ZipComporessor.Compress(jsondata); //压缩传入参数 } IWCFHandlerService _wcfService = mConn.WcfService; IAsyncResult result = null; AddMessageHeader(_wcfService as IContextChannel, "", requestData.Iscompressjson, requestData.Isencryptionjson, requestData.Serializetype, (() => { AsyncCallback callback = delegate(IAsyncResult r) { string retJson = _wcfService.EndProcessRequest(r); if (requestData.Iscompressjson) { retJson = ZipComporessor.Decompress(retJson); } string retData = ""; object Result = JsonConvert.DeserializeObject(retJson); int ret = Convert.ToInt32((((Newtonsoft.Json.Linq.JObject)Result)["flag"]).ToString()); string msg = (((Newtonsoft.Json.Linq.JObject)Result)["msg"]).ToString(); if (ret == 1) { throw new Exception(msg); } else { retData = ((Newtonsoft.Json.Linq.JObject)(Result))["data"].ToString(); } ServiceResponseData responsedata = new ServiceResponseData(); responsedata.Iscompressjson = requestData.Iscompressjson; responsedata.Isencryptionjson = requestData.Isencryptionjson; responsedata.Serializetype = requestData.Serializetype; responsedata.SetJsonData(retData); action(responsedata); }; result = _wcfService.BeginProcessRequest(mConn.ClientID, mConn.PluginName + "@" + controller, method, jsondata, callback, null); })); new Action(delegate() { if (IsHeartbeat == false)//如果没有启动心跳,则请求发送心跳 { ServerConfigRequestState = false; Heartbeat(); } }).BeginInvoke(null, null);//异步执行 return(result); } catch (Exception e) { ServerConfigRequestState = false; ReConnection(true);//连接服务主机失败,重连 throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!"); } }
/// <summary> /// 创建wcf服务连接 /// </summary> /// <param name="mainfrm"></param> public static void CreateConnection(IClientService client) { try { NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_WCFHandlerService"); //binding.OpenTimeout = TimeSpan.FromSeconds(10); //binding.TransferMode = TransferMode.Buffered; DuplexChannelFactory <IWCFHandlerService> mChannelFactory = new DuplexChannelFactory <IWCFHandlerService>(client, binding, System.Configuration.ConfigurationSettings.AppSettings["WCF_endpoint"]); IWCFHandlerService wcfHandlerService = mChannelFactory.CreateChannel(); string routerID; string mProxyID; using (var scope = new OperationContextScope(wcfHandlerService as IContextChannel)) { // 注意namespace必须和ServiceContract中定义的namespace保持一致,默认是:http://tempuri.org //var myNamespace = "http://www.efwplus.cn/"; // 注意Header的名字中不能出现空格,因为要作为Xml节点名。 routerID = Guid.NewGuid().ToString(); var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, routerID); OperationContext.Current.OutgoingMessageHeaders.Add(router); mProxyID = wcfHandlerService.CreateDomain(getLocalIPAddress()); } if (AppGlobal.cache.Contains("WCFClientID")) { AppGlobal.cache.Remove("WCFClientID"); } if (AppGlobal.cache.Contains("WCFService")) { AppGlobal.cache.Remove("WCFService"); } if (AppGlobal.cache.Contains("ClientService")) { AppGlobal.cache.Remove("ClientService"); } if (AppGlobal.cache.Contains("routerID")) { AppGlobal.cache.Remove("routerID"); } AppGlobal.cache.Add("routerID", routerID); AppGlobal.cache.Add("WCFClientID", mProxyID); AppGlobal.cache.Add("WCFService", wcfHandlerService); AppGlobal.cache.Add("ClientService", client); //开启发送心跳 if (timer == null) { StartTimer(); } else { timer.Start(); } } catch (Exception err) { throw new Exception(err.Message); } }
/// <summary> /// 发送心跳 /// </summary> /// <returns></returns> private bool Heartbeat() { IWCFHandlerService _wcfService = mConn.WcfService; try { bool ret = false; string serverConfig = null; AddMessageHeader(_wcfService as IContextChannel, "", (() => { ret = _wcfService.Heartbeat(mConn.ClientID); if (ServerConfigRequestState == false) { //重新获取服务端配置,如:是否压缩Json、是否加密Json serverConfig = _wcfService.ServerConfig(); ServerConfigRequestState = true; } })); if (!string.IsNullOrEmpty(serverConfig)) { IsHeartbeat = serverConfig.Split(new char[] { '#' })[0] == "1" ? true : false; HeartbeatTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[1]); IsMessage = serverConfig.Split(new char[] { '#' })[2] == "1" ? true : false; MessageTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[3]); IsCompressJson = serverConfig.Split(new char[] { '#' })[4] == "1" ? true : false; IsEncryptionJson = serverConfig.Split(new char[] { '#' })[5] == "1" ? true : false; serializeType = (SerializeType)Convert.ToInt32(serverConfig.Split(new char[] { '#' })[6]); if (backConfig != null) { backConfig(IsMessage, MessageTime); } if (IsHeartbeat) { //开启发送心跳 if (timer == null) { StartTimer(); } else { timer.Start(); } } else { if (timer != null) { timer.Stop(); } } } if (ret == false)//表示服务主机关闭过,丢失了clientId,必须重新创建连接 { //mChannelFactory.Abort();//关闭原来通道 (_wcfService as IContextChannel).Abort(); CreateConnection(); } return(ret); } catch { ServerConfigRequestState = false; ReConnection(false);//连接服务主机失败,重连 return(false); } }
/// <summary> /// 创建wcf服务连接 /// </summary> /// <param name="mainfrm"></param> public static IWCFHandlerService CreateConnection(IClientService client) { try { //NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_WCFHandlerService"); mChannelFactory = new DuplexChannelFactory <IWCFHandlerService>(client, "myendpoint"); IWCFHandlerService wcfHandlerService = mChannelFactory.CreateChannel(); string routerID; string mProxyID; using (var scope = new OperationContextScope(wcfHandlerService as IContextChannel)) { // 注意namespace必须和ServiceContract中定义的namespace保持一致,默认是:http://tempuri.org routerID = Guid.NewGuid().ToString(); var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, routerID); OperationContext.Current.OutgoingMessageHeaders.Add(router); mProxyID = wcfHandlerService.CreateDomain(getLocalIPAddress()); if (WcfClientManage.ServerConfigRequestState == false) { //重新获取服务端配置,如:是否压缩Json、是否加密Json string serverConfig = wcfHandlerService.ServerConfig(); WcfClientManage.IsHeartbeat = serverConfig.Split(new char[] { '#' })[0] == "1" ? true : false; WcfClientManage.HeartbeatTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[1]); WcfClientManage.IsMessage = serverConfig.Split(new char[] { '#' })[2] == "1" ? true : false; WcfClientManage.MessageTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[3]); WcfClientManage.IsCompressJson = serverConfig.Split(new char[] { '#' })[4] == "1" ? true : false; WcfClientManage.IsEncryptionJson = serverConfig.Split(new char[] { '#' })[5] == "1" ? true : false; if (WcfClientManage.IsHeartbeat) { //开启发送心跳 if (timer == null) { StartTimer(); } else { timer.Start(); } } else { if (timer != null) { timer.Stop(); } } WcfClientManage.ServerConfigRequestState = true; } } if (AppGlobal.cache.Contains("WCFClientID")) { AppGlobal.cache.Remove("WCFClientID"); } if (AppGlobal.cache.Contains("WCFService")) { AppGlobal.cache.Remove("WCFService"); } if (AppGlobal.cache.Contains("ClientService")) { AppGlobal.cache.Remove("ClientService"); } if (AppGlobal.cache.Contains("routerID")) { AppGlobal.cache.Remove("routerID"); } AppGlobal.cache.Add("routerID", routerID); AppGlobal.cache.Add("WCFClientID", mProxyID); AppGlobal.cache.Add("WCFService", wcfHandlerService); AppGlobal.cache.Add("ClientService", client); return(wcfHandlerService); } catch (Exception err) { throw new Exception(err.Message); } }