/// <summary> /// 实例化InvokeCaller /// </summary> /// <param name="appName"></param> /// <param name="service"></param> public InvokeCaller(string appName, IService service) { this.appName = appName; this.service = service; this.hostName = DnsHelper.GetHostName(); this.ipAddress = DnsHelper.GetIPAddress(); }
/// <summary> /// 发送邮件 /// </summary> /// <param name="ex"></param> /// <param name="to"></param> private void SendMail(Exception ex, string[] to) { if (to == null || to.Length == 0) { throw new ArgumentException("请传入收件人地址信息参数!"); } string title = string.Format("【{2}】({3}) - 异常邮件由【{0}({1})】发出", DnsHelper.GetHostName(), DnsHelper.GetIPAddress(), GetAppTitle(ex), GetServiceTitle(ex)); SmtpMail.Instance.SendExceptionAsync(ex, title, to); }
/// <summary> /// 发送邮件 /// </summary> /// <param name="log"></param> /// <param name="to"></param> private void SendMail(string log, string[] to) { if (to == null || to.Length == 0) { throw new ArgumentException("请传入收件人地址信息参数!"); } var body = CoreHelper.GetSubString(log, 20, "..."); string title = string.Format("{2} - 普通邮件由【{0}({1})】发出", DnsHelper.GetHostName(), DnsHelper.GetIPAddress(), body); SmtpMail.Instance.SendAsync(title, log, to); }
/// <summary> /// Gets the service. /// </summary> /// <param name="reqMsg"></param> /// <returns></returns> private IService ParseService(RequestMessage reqMsg) { IService service = null; string serviceKey = "Service_" + reqMsg.ServiceName; if (status.Container.Kernel.HasComponent(serviceKey)) { service = status.Container.Resolve <IService>(serviceKey); } if (service == null) { string body = string.Format("The server【{1}({2})】not find matching service ({0})." , reqMsg.ServiceName, DnsHelper.GetHostName(), DnsHelper.GetIPAddress()); //获取异常 throw IoCHelper.GetException(OperationContext.Current, reqMsg, body); } return(service); }
/// <summary> /// Query a status of Software Access Point /// </summary> /// <returns>A construct with status of the AP</returns> public HostedNetworkStatus getStatus() { var addressList = GetListArp(); IntPtr ppStatus = IntPtr.Zero; lastErrorCode = (int)NativeWLanAPI.WlanHostedNetworkQueryStatus(handle, out ppStatus, IntPtr.Zero); checkStatusAndThrow("Could not retrive SoftAP status", false); NativeWLanAPI._WLAN_HOSTED_NETWORK_STATUS qstat = (NativeWLanAPI._WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(ppStatus, typeof(NativeWLanAPI._WLAN_HOSTED_NETWORK_STATUS)); bool isActive = qstat.HostedNetworkState == NativeWLanAPI._WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_active ? true : false; int peersConnected = (int)qstat.dwNumberOfPeers; Guid guid = qstat.IPDeviceID; string networkMac = BitConverter.ToString(qstat.wlanHostedNetworkBSSID.address);//网卡地址 LinkedList <HostedNetworkPeer> peers = new LinkedList <HostedNetworkPeer>(); IntPtr offset = Marshal.OffsetOf(typeof(NativeWLanAPI._WLAN_HOSTED_NETWORK_STATUS), "PeerList"); for (int i = 0; i < peersConnected; i++) { IntPtr ppeer = new IntPtr(ppStatus.ToInt64() + offset.ToInt64()); NativeWLanAPI._WLAN_HOSTED_NETWORK_PEER_STATE peer = (NativeWLanAPI._WLAN_HOSTED_NETWORK_PEER_STATE)Marshal.PtrToStructure(ppeer, typeof(NativeWLanAPI._WLAN_HOSTED_NETWORK_PEER_STATE)); string mac = BitConverter.ToString(peer.PeerMacAddress.address); bool authenticated = ((int)peer.PeerAuthState == 1 ? true : false); string ipAddress = addressList.LastOrDefault(x => string.Equals(x.MacAddress, mac, StringComparison.CurrentCultureIgnoreCase))?.IpAddress; HostedNetworkPeer hpeer = new HostedNetworkPeer(mac, authenticated, DnsHelper.GetHostName(ipAddress), ipAddress); peers.AddLast(hpeer); offset += Marshal.SizeOf(peer); } NativeWLanAPI.WlanFreeMemory(ppStatus); return(new HostedNetworkStatus(isActive, guid, peersConnected, peers, networkMac)); }
/// <summary> /// Initializes a new instance of the <see cref="ServiceInvocationHandler"/> class. /// </summary> /// <param name="config"></param> /// <param name="container"></param> /// <param name="service"></param> /// <param name="serviceType"></param> /// <param name="cache"></param> public ServiceInvocationHandler(CastleFactoryConfiguration config, IServiceContainer container, IService service, Type serviceType, ICacheStrategy cache, IServiceLog logger) { this.config = config; this.container = container; this.serviceType = serviceType; this.service = service; this.cache = cache; this.logger = logger; this.hostName = DnsHelper.GetHostName(); this.ipAddress = DnsHelper.GetIPAddress(); this.cacheTimes = new Dictionary <string, int>(); var methods = CoreHelper.GetMethodsFromType(serviceType); foreach (var method in methods) { var contract = CoreHelper.GetMemberAttribute <OperationContractAttribute>(method); if (contract != null && contract.CacheTime > 0) { cacheTimes[method.ToString()] = contract.CacheTime; } } }
/// <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"); }
/// <summary> /// Runs the specified MSG. /// </summary> /// <param name="reqMsg">The MSG.</param> /// <returns>The msg.</returns> protected override ResponseMessage Run(RequestMessage reqMsg) { var resMsg = new ResponseMessage { TransactionId = reqMsg.TransactionId, ReturnType = reqMsg.ReturnType, ServiceName = reqMsg.ServiceName, MethodName = reqMsg.MethodName }; #region 获取相应的方法 var methodKey = string.Format("{0}${1}", reqMsg.ServiceName, reqMsg.MethodName); if (!hashtable.ContainsKey(methodKey)) { var m = CoreHelper.GetMethodFromType(serviceType, reqMsg.MethodName); if (m == null) { string message = string.Format("The server【{2}({3})】not find matching method. ({0},{1})." , reqMsg.ServiceName, reqMsg.MethodName, DnsHelper.GetHostName(), DnsHelper.GetIPAddress()); resMsg.Error = new WarningException(message); return(resMsg); } hashtable[methodKey] = m; } #endregion //容器实例对象 object instance = null; try { //定义Method var method = hashtable[methodKey] as System.Reflection.MethodInfo; //解析服务 instance = container.Resolve(serviceType); //返回拦截服务 var service = AspectFactory.CreateProxyService(serviceType, instance); if (reqMsg.InvokeMethod) { var objValue = reqMsg.Parameters["InvokeParameter"]; var jsonString = (objValue == null ? string.Empty : objValue.ToString()); //解析参数 reqMsg.Parameters = IoCHelper.CreateParameters(method, jsonString); } //参数赋值 object[] parameters = IoCHelper.CreateParameterValues(method, reqMsg.Parameters); //调用对应的服务 resMsg.Value = DynamicCalls.GetMethodInvoker(method).Invoke(service, parameters); //处理返回参数 IoCHelper.SetRefParameters(method, resMsg.Parameters, parameters); //返回结果数据 if (reqMsg.InvokeMethod) { resMsg.Value = new InvokeData { Value = SerializationManager.SerializeJson(resMsg.Value), Count = resMsg.Count, OutParameters = resMsg.Parameters.ToString() }; //清除参数集合 resMsg.Parameters.Clear(); } } catch (Exception ex) { //捕获全局错误 resMsg.Error = ex; } finally { //释放资源 container.Release(instance); instance = null; } return(resMsg); }
/// <summary> /// 获取本地服务 /// </summary> /// <typeparam name="IServiceInterfaceType"></typeparam> /// <returns></returns> private IServiceInterfaceType GetLocalService <IServiceInterfaceType>() { Type serviceType = typeof(IServiceInterfaceType); //定义异常 Exception ex = new ArgumentException("Generic parameter type - 【" + serviceType.FullName + "】 must be an interface marked with ServiceContractAttribute."); if (!serviceType.IsInterface) { throw ex; } else { bool markedWithServiceContract = false; var attr = CoreHelper.GetTypeAttribute <ServiceContractAttribute>(serviceType); if (attr != null) { markedWithServiceContract = true; } if (!markedWithServiceContract) { throw ex; } } //定义本地服务 IServiceInterfaceType ls = default(IServiceInterfaceType); //如果是本地配置,则抛出异常 if (config.Type != CastleFactoryType.Remote) { if (resolver != null) { ls = resolver.ResolveService <IServiceInterfaceType>(container); } if (ls == null) { var serviceKey = "Service_" + serviceType.FullName; //本地服务 if (container.Kernel.HasComponent(serviceKey)) { lock (hashtable.SyncRoot) { if (!hashtable.ContainsKey(serviceType)) { //返回本地服务 var service = container.Resolve <IService>(serviceKey); var handler = new LocalInvocationHandler(config, container, service, serviceType, cache, logger); var dynamicProxy = ProxyFactory.GetInstance().Create(handler, serviceType, true); hashtable[serviceType] = dynamicProxy; } } ls = (IServiceInterfaceType)hashtable[serviceType]; } } if (ls == null) { if (config.Type == CastleFactoryType.Local) { throw new WarningException(string.Format("The local【{1}({2})】not find matching service ({0})." , serviceType.FullName, DnsHelper.GetHostName(), DnsHelper.GetIPAddress())); } } } return(ls); }