/// <summary> /// 设置上下文 /// </summary> /// <param name="caller"></param> private void SetOperationContext(AppCaller caller) { //初始化上下文 OperationContext.Current = new OperationContext { Container = container, Caller = caller }; }
/// <summary> /// 创建AppCaller /// </summary> /// <param name="reqMsg"></param> /// <returns></returns> private AppCaller CreateCaller(RequestMessage reqMsg) { //创建AppCaller对象 var caller = new AppCaller { AppPath = AppDomain.CurrentDomain.BaseDirectory, AppName = reqMsg.AppName, IPAddress = reqMsg.IPAddress, HostName = reqMsg.HostName, ServiceName = reqMsg.ServiceName, MethodName = reqMsg.MethodName, Parameters = reqMsg.Parameters.ToString(), CallTime = DateTime.Now }; return caller; }
/// <summary> /// 设置上下文 /// </summary> /// <param name="client"></param> /// <param name="caller"></param> private void SetOperationContext(IScsServerClient client, AppCaller caller) { //实例化当前上下文 Type callbackType = null; if (callbackTypes.ContainsKey(caller.ServiceName)) { callbackType = callbackTypes[caller.ServiceName]; } OperationContext.Current = new OperationContext(client, callbackType) { Container = status.Container, Caller = caller }; }
/// <summary> /// 获取AppCaller /// </summary> /// <param name="client"></param> /// <param name="reqMsg"></param> /// <returns></returns> private AppCaller CreateCaller(IScsServerClient client, RequestMessage reqMsg) { //获取AppPath var appPath = (client.State == null) ? null : (client.State as AppClient).AppPath; //服务参数信息 var caller = new AppCaller { AppPath = appPath, AppName = reqMsg.AppName, IPAddress = reqMsg.IPAddress, HostName = reqMsg.HostName, ServiceName = reqMsg.ServiceName, MethodName = reqMsg.MethodName, Parameters = reqMsg.Parameters.ToString(), CallTime = DateTime.Now }; return caller; }
private IService CreateService(AppCaller appCaller) { //处理数据返回InvokeData var serviceKey = "Service_" + appCaller.ServiceName; var service = container.Resolve<IService>(serviceKey); //等待超时 var timeSpan = TimeSpan.FromSeconds(config.Timeout); if (callTimeouts.ContainsKey(appCaller.ServiceName)) { timeSpan = TimeSpan.FromSeconds(callTimeouts[appCaller.ServiceName]); } //启用异步调用服务 return new AsyncService(smart, container, service, timeSpan); }
/// <summary> /// 调用服务,并返回字符串 /// </summary> /// <param name="name"></param> /// <param name="parameters"></param> /// <returns></returns> public string CallMethod(string name, string parameters) { if (callers.ContainsKey(name)) { var caller = callers[name]; var message = new InvokeMessage { ServiceName = caller.Service.FullName, MethodName = caller.Method.ToString(), Parameters = parameters }; string thisKey = string.Format("{0}${1}${2}", message.ServiceName, message.MethodName, message.Parameters); var cacheKey = string.Format("HttpServiceCaller_{0}", IoCHelper.GetMD5String(thisKey)); var invokeData = CacheHelper.Get<InvokeData>(cacheKey); if (invokeData == null) { //获取当前调用者信息 var appCaller = new AppCaller { AppPath = AppDomain.CurrentDomain.BaseDirectory, AppName = "HttpServer", HostName = DnsHelper.GetHostName(), IPAddress = DnsHelper.GetIPAddress(), ServiceName = message.ServiceName, MethodName = message.MethodName, Parameters = message.Parameters, CallTime = DateTime.Now }; //创建服务 var service = CreateService(appCaller); //初始化上下文 SetOperationContext(appCaller); try { //使用Invoke方式调用 var invoke = new InvokeCaller(appCaller.AppName, service); invokeData = invoke.CallMethod(message); //插入缓存 if (invokeData != null && caller.CacheTime > 0) { CacheHelper.Insert(cacheKey, invokeData, caller.CacheTime); } } finally { //初始化上下文 OperationContext.Current = null; } } //如果缓存不为null,则返回缓存数据 if (invokeData != null) return invokeData.Value; } return "null"; }
private void ShowDialog(AppCaller caller) { var service = services.First(p => p.FullName == caller.ServiceName); var method = service.Methods.First(p => p.FullName == caller.MethodName); SingletonMul.Show(string.Format("FORM_{0}_{1}", service.FullName, method.FullName), () => { frmInvoke frm = new frmInvoke(defaultNode, service.FullName, method.FullName, method.Parameters, caller.Parameters); return frm; }); }
void PositionService(AppCaller caller) { listAssembly.SelectedIndex = -1; listService.SelectedIndex = -1; listMethod.SelectedIndex = -1; for (int index = 0; index < listAssembly.Items.Count; index++) { var item = listAssembly.Items[index]; var services = item.Source as IList<ServiceInfo>; if (services.Any(p => p.FullName == caller.ServiceName)) { listAssembly.SelectedIndex = index; break; } } for (int index = 0; index < listService.Items.Count; index++) { var item = listService.Items[index]; var service = item.Source as ServiceInfo; if (service.FullName == caller.ServiceName) { listService.SelectedIndex = index; break; } } for (int index = 0; index < listMethod.Items.Count; index++) { var item = listMethod.Items[index]; var method = item.Source as MethodInfo; if (method.FullName == caller.MethodName) { listMethod.SelectedIndex = index; break; } } //选中服务页 tabControl1.SelectedIndex = 0; }
private void AppendText(RichTextBox rich, AppCaller caller) { rich.Clear(); rich.SelectionIndent = 0; rich.SelectionColor = Color.Blue; rich.AppendText("AppPath:\r\n"); rich.SelectionColor = Color.Black; rich.SelectionIndent = 20; rich.AppendText(caller.AppPath ?? "未知路径"); rich.AppendText("\r\n\r\n"); rich.SelectionIndent = 0; rich.SelectionColor = Color.Blue; rich.AppendText("CallTime:\r\n"); rich.SelectionColor = Color.Black; rich.SelectionIndent = 20; rich.AppendText(caller.CallTime.ToString()); rich.AppendText("\r\n\r\n"); rich.SelectionIndent = 0; rich.SelectionColor = Color.Blue; rich.AppendText("AppName:\r\n"); rich.SelectionColor = Color.Black; rich.SelectionIndent = 20; rich.AppendText(caller.AppName ?? "未知应用"); rich.AppendText("\r\n\r\n"); rich.SelectionIndent = 0; rich.SelectionColor = Color.Blue; rich.AppendText("ServerName:\r\n"); rich.SelectionColor = Color.Black; rich.SelectionIndent = 20; rich.AppendText(caller.IPAddress + " => " + caller.HostName); rich.AppendText("\r\n\r\n"); rich.SelectionIndent = 0; rich.SelectionColor = Color.Blue; rich.AppendText("ServiceName:\r\n"); rich.SelectionColor = Color.Black; rich.SelectionIndent = 20; rich.AppendText(caller.ServiceName); rich.AppendText("\r\n\r\n"); rich.SelectionIndent = 0; rich.SelectionColor = Color.Blue; rich.AppendText("MethodName:\r\n"); rich.SelectionColor = Color.Black; rich.SelectionIndent = 20; rich.AppendText(caller.MethodName); rich.AppendText("\r\n\r\n"); rich.SelectionIndent = 0; rich.SelectionColor = Color.Blue; rich.AppendText("Parameters:\r\n"); rich.SelectionColor = Color.Black; rich.SelectionIndent = 20; rich.AppendText(caller.Parameters); }
private void PubSub(AppCaller caller, bool isSub) { if (!CheckMonitor()) return; try { if (isSub) { service.SubscribeApp(caller.AppName); InitAppTypes(service); MessageBox.Show("订阅成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { service.UnsubscribeApp(caller.AppName); InitAppTypes(service); MessageBox.Show("退订成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { SimpleLog.Instance.WriteLogForDir("Client", ex); MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }