Exemplo n.º 1
0
        /// <summary>
        /// 截获从Client端发送的消息转发到目标终结点并获得返回值给Client端
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public Message ProcessMessage(Message requestMessage)
        {
            //Binding binding = null;
            EndpointAddress endpointAddress = null;

            GetServiceEndpoint(requestMessage, out endpointAddress);
            IDuplexRouterCallback callback = OperationContext.Current.GetCallbackChannel <IDuplexRouterCallback>();
            NetTcpBinding         tbinding = new NetTcpBinding("netTcpExpenseService_ForSupplier");

            using (DuplexChannelFactory <IRouterService> factory = new DuplexChannelFactory <IRouterService>(new InstanceContext(null, new DuplexRouterCallback(callback)), tbinding, endpointAddress))
            {
                factory.Endpoint.Behaviors.Add(new MustUnderstandBehavior(false));
                IRouterService proxy = factory.CreateChannel();

                using (proxy as IDisposable)
                {
                    // 请求消息记录
                    IClientChannel clientChannel = proxy as IClientChannel;
                    //Console.WriteLine(String.Format("Request received at {0}, to {1}\r\n\tAction: {2}", DateTime.Now, clientChannel.RemoteAddress.Uri.AbsoluteUri, requestMessage.Headers.Action));
                    if (WcfServerManage.IsDebug)
                    {
                        hostwcfMsg(DateTime.Now, String.Format("路由请求消息发送:  {0}", clientChannel.RemoteAddress.Uri.AbsoluteUri));
                    }
                    // 调用绑定的终结点的服务方法
                    Message responseMessage = proxy.ProcessMessage(requestMessage);

                    // 应答消息记录
                    //Console.WriteLine(String.Format("Reply received at {0}\r\n\tAction: {1}", DateTime.Now, responseMessage.Headers.Action));
                    //Console.WriteLine();
                    //hostwcfMsg(DateTime.Now, String.Format("应答消息: {0}", responseMessage.Headers.Action));
                    return(responseMessage);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 截获从Client端发送的消息转发到目标终结点并获得返回值给Client端
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public Message ProcessMessage(Message requestMessage)
        {
            try
            {
                begintime();
                IRouterService  proxy = null;
                HeaderParameter para  = HeaderOperater.GetHeaderValue(requestMessage);

                if (RouterServerManage.routerDic.ContainsKey(para.routerid))
                {
                    proxy = RouterServerManage.routerDic[para.routerid];
                    para.replyidentify = RouterServerManage.headParaDic[para.routerid].replyidentify;
                }
                else
                {
                    //Binding binding = null;
                    EndpointAddress endpointAddress = null;
                    Uri             touri           = null;
                    para = RouterServerManage.AddClient(requestMessage, para, out endpointAddress, out touri);
                    requestMessage.Headers.To = touri;

                    IDuplexRouterCallback callback = OperationContext.Current.GetCallbackChannel <IDuplexRouterCallback>();
                    NetTcpBinding         tbinding = new NetTcpBinding("NetTcpBinding_WCFHandlerService");
                    DuplexChannelFactory <IRouterService> factory = new DuplexChannelFactory <IRouterService>(new InstanceContext(new DuplexRouterCallback(callback)), tbinding, endpointAddress);
                    proxy = factory.CreateChannel();

                    //缓存会话
                    RouterServerManage.routerDic.Add(para.routerid, proxy);
                    RouterServerManage.headParaDic.Add(para.routerid, para);
                }

                Message responseMessage = null;
                HeaderOperater.AddMessageHeader(requestMessage, para);//增加自定义消息头
                responseMessage = proxy.ProcessMessage(requestMessage);

                if (para.cmd == "Quit")
                {
                    //关闭连接释放缓存会话
                    RouterServerManage.RemoveClient(para);
                }

                double outtime = endtime();
                // 请求消息记录
                if (WcfServerManage.IsDebug)
                {
                    RouterServerManage.hostwcfMsg(Color.Black, DateTime.Now, String.Format("路由请求消息发送(耗时[" + outtime + "]):  {0}", requestMessage.Headers.Action));
                }


                return(responseMessage);
            }
            catch (Exception e)
            {
                return(Message.CreateMessage(requestMessage.Version, FaultCode.CreateReceiverFaultCode("error", RouterServerManage.ns), e.Message, requestMessage.Headers.Action));
            }
        }
Exemplo n.º 3
0
        public Message ProcessMessage(Message requestMessage)
        {
            Binding         binding         = null;
            EndpointAddress endpointAddress = null;

            GetServiceEndpoint(requestMessage, out binding, out endpointAddress);

            using (ChannelFactory <IRouterService> factory = new ChannelFactory <IRouterService>(binding, endpointAddress))
            {
                OperationContext              context  = OperationContext.Current;
                MessageProperties             prop     = context.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                string src_ip   = endpoint.Address;
                int    src_port = endpoint.Port;



                factory.Endpoint.Behaviors.Add(new MustUnderstandBehavior(false));
                IRouterService proxy = factory.CreateChannel();

                using (proxy as IDisposable)
                {
                    // log request message
                    IClientChannel clientChannel = proxy as IClientChannel;
                    var            threadId      = Thread.CurrentThread.ManagedThreadId;
                    string         message       = String.Format("Thread id {3},Source Ip:{4}: Request received at {0}, to {1}\r\tAction: {2}.", DateTime.Now, clientChannel.RemoteAddress.Uri.AbsoluteUri, requestMessage.Headers.Action, threadId, string.Format("{0}:{1}", src_ip, src_port));
                    Console.WriteLine(message);
                    LogManager.GetLogger().Info(message);

                    // invoke service
                    Message responseMessage = proxy.ProcessMessage(requestMessage);

                    // log response message
                    message = String.Format("Thread id {2},Source Ip:{3}: Reply received at {0}\r\tAction: {1}.", DateTime.Now, responseMessage.Headers.Action, threadId, string.Format("{0}:{1}", src_ip, src_port));
                    Console.WriteLine(message);
                    LogManager.GetLogger().Info(message);
                    Console.WriteLine();

                    return(responseMessage);
                }
            }
        }
Exemplo n.º 4
0
        public Message ProcessMessage(Message requestMessage)
        {
            RegistrationList.Add("http://www.thatindigogirl.com/samples/2008/01/IServiceA", "ServiceA");  // services registration
            RegistrationList.Add("http://www.thatindigogirl.com/samples/2008/01/IServiceB", "ServiceB");

            /*Uri httpBase = new Uri(string.Format("http://localhost:{0}",FindFreePort()));
             * Uri tcpBase = new Uri(string.Format("net.tcp://localhost:{0}",FindFreePort()));
             * Uri netPipeBase = new Uri(string.Format("net.pipe://localhost/{0}",Guid.NewGuid().ToString()));
             *
             * ServiceHost host = new ServiceHost(typeof(ServiceA), httpBase, tcpBase, netPipeBase);*/

            //header
            string contractNamespace = requestMessage.Headers.Action.Substring(0, requestMessage.Headers.Action.LastIndexOf("/"));
            string configurationName = RouterService.RegistrationList[contractNamespace];

            var contractNamespaceHeaders = requestMessage.Headers.GetHeader <string>("Route", "http://www.thatindigogirl.com/samples/2008/01");

            //body
            //После прочтения сообщение невозможно перенаправить службам приложений,
            //поскольку сообщение может быть только один раз прочитано, записано или скопировано.
            MessageBuffer       messageBuffer = requestMessage.CreateBufferedCopy(int.MaxValue);
            Message             messageCopy   = messageBuffer.CreateMessage(); // create new instance of message base on buffered copy
            XmlDictionaryReader bodyReader    = messageCopy.GetReaderAtBodyContents();

            XmlDocument doc = new XmlDocument();

            doc.Load(bodyReader);
            XmlNodeList elements   = doc.GetElementsByTagName("LicenseKey");
            string      licenseKey = elements[0].InnerText;


            using (var factory = new ChannelFactory <IRouterService>(configurationName))
            {
                factory.Endpoint.Behaviors.Add(new MustUnderstandBehavior(false)); // solve problem with response messages contains security headers
                using (IRouterService proxy = factory.CreateChannel())
                {
                    return(proxy.ProcessMessage(requestMessage));
                }
            }
        }