コード例 #1
0
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            MessageInfo messageInfo = new MessageInfo
            {
                ServerTimeStamp = DateTime.Now,
                Platform = "WCF"
            };

            OperationDescription operationDesc = GetOperationDescription(ref messageInfo, OperationContext.Current);
            if (operationDesc != null)
            {
                Type contractType = operationDesc.DeclaringContract.ContractType;
                messageInfo.Action = operationDesc.Name;
                messageInfo.TypeName = contractType.FullName;
                messageInfo.AssemblyName = contractType.Assembly.GetName().Name;
            }

            messageInfo.ServerName = Dns.GetHostName();
            messageInfo.ServerProcessName = AppDomain.CurrentDomain.FriendlyName;

            string message = String.Format("Request => {0} {1}", messageInfo.ServerTimeStamp.ToString(CultureInfo.InvariantCulture), messageInfo.Request);

            if (IsDisplayConsole)
            {
                Console.WriteLine();
                Console.WriteLine(message);
            }

            if (IsLogFile)
            {
                _logger.Log(LogLevel.Info, message);
            }

            if (OnMessage != null)
            {
                OnMessage(this, new MessageInspectorArgs
                {
                    MessageInspectionType = MessageInspectionType.Request,
                    Message = messageInfo
                });
            }

            return messageInfo;
        }
コード例 #2
0
        private OperationDescription GetOperationDescription(ref MessageInfo messageInfo, OperationContext operationContext)
        {
            OperationDescription od = null;
            string bindingName = operationContext.EndpointDispatcher.ChannelDispatcher.BindingName;
            string methodName;
            if (bindingName.Contains("WebHttpBinding"))
            {
                //REST request
                methodName = (string)operationContext.IncomingMessageProperties["HttpOperationName"];
                messageInfo.Request = operationContext.IncomingMessageProperties["Via"].ToString();
            }
            else
            {
                //SOAP request
                string action = operationContext.IncomingMessageHeaders.Action;
                methodName = operationContext.EndpointDispatcher.DispatchRuntime.Operations.FirstOrDefault(o => o.Action == action).Name;
            }

            EndpointAddress epa = operationContext.EndpointDispatcher.EndpointAddress;
            ServiceDescription hostDesc = operationContext.Host.Description;
            ServiceEndpoint ep = hostDesc.Endpoints.Find(epa.Uri);

            if (ep != null)
            {
                od = ep.Contract.Operations.Find(methodName);
            }

            return od;
        }