Exemple #1
0
        } // end AfterReceiveReply

        /// <summary>
        /// agent for client traffic outbound request
        /// </summary>
        /// <param name="request"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
        {
            WCFContainer w = new WCFContainer();

            w.thistid = "";
            w.thisid  = Guid.NewGuid().ToString();
            w.start   = DateTime.Now;

            //request.Headers.Add(new FGSMSAgentHeaderWCF());
            //  start = DateTime.Now;
            if (request != null)
            {
                w.req = MessageHelper.MessagetoString(ref request);
            }
            //  w.req = request.ToString();

            w.url    = "urn:undeterminable";
            w.action = "urn:undeterminable";
            if (System.ServiceModel.OperationContext.Current != null &&
                System.ServiceModel.OperationContext.Current.RequestContext != null &&
                System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage != null &&
                System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers != null &&
                !String.IsNullOrEmpty(System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.Action))
            {
                w.action = System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.Action;
                w.action.Replace("\"", "");
                w.action.Replace("'", "");
                if (String.IsNullOrEmpty(w.action))
                {
                    w.action = "urn:undeterminable";
                }
            }
            if (w.action.Equals("urn:undeterminable"))
            {
                w.action = request.Headers.Action;
                w.action.Replace("\"", "");
                w.action.Replace("'", "");
                if (String.IsNullOrEmpty(w.action))
                {
                    w.action = "urn:undeterminable";
                }
            }
            if (w.action.Equals("urn:undeterminable") && HttpContext.Current != null)
            {
                w.action = HttpContext.Current.Request.Headers.Get("SOAPAction");
                w.action = w.action.Replace("\"", "");
                w.action = w.action.Replace("'", "");
                if (String.IsNullOrEmpty(w.action))
                {
                    w.action = "urn:undeterminable";
                }
            }
            if (w.action.Equals("urn:undeterminable") && HttpContext.Current != null)
            {
                w.action = HttpContext.Current.Request.HttpMethod;
                w.action = w.action.Replace("\"", "");
                w.action = w.action.Replace("'", "");
                if (String.IsNullOrEmpty(w.action))
                {
                    w.action = "urn:undeterminable";
                }
            }
            if (w.action.Equals("urn:undeterminable"))
            {
                w.action = getActionFromFirstXmlNode(request);
            }
            if (OperationContext.Current != null && request.Headers.To != null)
            {
                w.url = request.Headers.To.ToString();
            }
            if (w.url.Equals("urn:undeterminable"))
            {
                w.url = channel.RemoteAddress.Uri.ToString();
            }
            if (w.url.Equals("urn:undeterminable"))
            {
            }
            //changed
            w.thistid = MessageProcessor.GetTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name);
            if (!String.IsNullOrEmpty(w.thistid))
            {
                w.thistid = w.thistid.Trim();
            }
            if (String.IsNullOrEmpty(w.thistid))
            {
                w.thistid = Guid.NewGuid().ToString();
            }
            // string relatedtransaction = "";
            w.myinboundheaders = GetHttpRequestProp(request);//request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            if (w.myinboundheaders != null && MessageProcessor.GetConfig.DependencyInjectionEnabled)
            {
                //for some reason, the http request property is persisted across objects when the proxy object is reused.
                //its most likely an optimization from microsoft regarding http session reuse.

                //When in visual studuo, it added http headers to outbound connections called vscasultydata, it will also be repeated when
                //persisting connections...
                w.myinboundheaders.Headers.Remove("VsDebuggerCausalityData");
                w.myinboundheaders.Headers.Remove(FGSMSConstants.MessageId);
                w.myinboundheaders.Headers.Remove(FGSMSConstants.transactionthreadKey);

                w.myinboundheaders.Headers.Add(FGSMSConstants.MessageId, w.thisid);
                w.myinboundheaders.Headers.Add(FGSMSConstants.transactionthreadKey, w.thistid);
            }
            if (MessageProcessor.GetConfig.DependencyInjectionEnabled)
            {
                //  MessageHeader mh = new MessageHeader("value").GetUnt
                request.Headers.Add(new FGSMSSoapHeaderTransactionThreadIdWCF(w.thistid));
                request.Headers.Add(new FGSMSSoapHeaderRelatedMessageIdWCF(w.thisid));
            }
            return(w);
        }
Exemple #2
0
        /// <summary>
        /// Agent for client traffic inbound response
        /// </summary>
        /// <param name="reply"></param>
        /// <param name="correlationState"></param>
        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            MessageProcessor    mp              = MessageProcessor.Instance;
            NameValueCollection requestheaders  = new NameValueCollection();
            NameValueCollection responseheaders = new NameValueCollection();
            //  if (MessageProcessor.Enabled)

            /*if (FGSMSConstants.log)
             *  try
             *  {
             *      if (FGSMSConstants.log) EventLog.WriteEntry(this.GetType().FullName, "Client hello from AfterRecieveReply");
             *  }
             *  catch { }*/



            // HttpRequestMessageProperty prop = new HttpRequestMessageProperty();
            // prop.Headers.Add("FGSMS.transactionrecord", Guid.NewGuid().ToString());

            WCFContainer w = null;

            try
            {
                w = (WCFContainer)correlationState;
            }
            catch (Exception ex)
            {
                Logger.debug(ex, "unable to get correlation object");
            }
            if (w == null)
            {
                Logger.debug("unable to get correlation object");
            }
            if (OperationContext.Current != null)
            {
                IEnumerator <string> it2 = OperationContext.Current.OutgoingMessageProperties.Keys.GetEnumerator();
                while (it2.MoveNext())
                {
                    if (it2.Current == HttpRequestMessageProperty.Name)
                    {
                        HttpRequestMessageProperty prop = OperationContext.Current.OutgoingMessageProperties[it2.Current] as HttpRequestMessageProperty;
                        if (prop != null)
                        {
                            requestheaders = prop.Headers;
                        }
                    }
                }
                if (OperationContext.Current.IncomingMessageProperties != null)
                {
                    IEnumerator <string> it = OperationContext.Current.IncomingMessageProperties.Keys.GetEnumerator();
                    while (it.MoveNext())
                    {
                        if (it.Current == HttpResponseMessageProperty.Name)
                        {
                            HttpResponseMessageProperty prop = OperationContext.Current.IncomingMessageProperties[it.Current] as HttpResponseMessageProperty;
                            if (prop != null)
                            {
                                responseheaders = prop.Headers;
                            }
                        }
                    }
                }
            }
            if (responseheaders != null && reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
            {
                HttpResponseMessageProperty rh = reply.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                responseheaders = rh.Headers;
            }


            if (w.myinboundheaders != null)
            {
                IEnumerator it = w.myinboundheaders.Headers.GetEnumerator();

                while (it.MoveNext())
                {
                    try
                    {
                        if (it != null && it.Current != null)
                        {
                            string name = it.Current as string;
                            if (name != null)
                            {
                                requestheaders.Add(name, w.myinboundheaders.Headers.Get(name));
                            }
                        }
                    }
                    catch (Exception ex) {
                        Logger.debug(ex);
                    }
                }
            }
            try
            {
                string myHost = String.Empty;
                string myIP   = String.Empty;
                try
                {
                    myHost = System.Net.Dns.GetHostName();
                    myIP   = System.Net.Dns.GetHostEntry(myHost).AddressList[0].ToString();
                }
                catch { }

                string relatedmsg = string.Empty;
                if (responseheaders != null)
                {
                    relatedmsg = responseheaders[FGSMSConstants.MessageId];
                }
                int idx = reply.Headers.FindHeader(FGSMSSoapHeaderRelatedMessageIdWCF.Name2, FGSMSSoapHeaderRelatedMessageIdWCF.Namespace2);
                if (idx >= 0)
                {
                    FGSMSSoapHeaderRelatedMessageIdWCF h = FGSMSSoapHeaderRelatedMessageIdWCF.ReadHeader(reply.Headers.GetReaderAtHeader(idx));
                    if (h != null)
                    {
                        relatedmsg = h.Id;
                    }
                }
                //since this is a client, the thread id is already known

                //string action = "urn:undeterminable";

                if (OperationContext.Current != null && OperationContext.Current.RequestContext != null)
                {
                    //typically a service as a client
                    string user = "";
                    if (ServiceSecurityContext.Current != null)
                    {
                        user = ServiceSecurityContext.Current.PrimaryIdentity.Name;
                    }
                    MessageProcessor.ProcessMessage(w.url,
                                                    //OperationContext.Current.RequestContext.RequestMessage.Headers.To.ToString(), \
                                                    w.start, DateTime.Now,
                                                    w.action,
                                                    //OperationContext.Current.RequestContext.RequestMessage.Headers.Action,
                                                    w.req,
                                                    //OperationContext.Current.RequestContext.RequestMessage.ToString(),
                                                    reply.ToString(), w.thisid, reply.IsFault, HttpContext.Current, myIP, this.GetType().FullName + ".client",
                                                    user, string.Empty, w.thistid, relatedmsg, requestheaders, responseheaders);
                }
                else
                {
                    //stand alone client
                    MessageProcessor.ProcessMessage(w.url, w.start, DateTime.Now,
                                                    w.action, w.req,
                                                    MessageHelper.MessagetoString(ref reply)
                                                    //.ToString()
                                                    , w.thisid, reply.IsFault, HttpContext.Current, myIP, this.GetType().FullName + ".client",
                                                    System.Environment.UserName, string.Empty, w.thistid, relatedmsg, requestheaders, responseheaders);
                }
            }

            catch (Exception ex)
            {
                Logger.error(ex, this.GetType().FullName + "Error from FGSMS WCF client agent, AfterReceiveReply, this typically means that a transaction could not be added to the queue, ");
            }
        } // end AfterReceiveReply
Exemple #3
0
        } // end AfterReceiveRequest

        /// <summary>
        /// service traffic outbound response
        /// </summary>
        /// <param name="reply"></param>
        /// <param name="correlationState"></param>
        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            // object prop;
            // string requestHeader = null;
            //Console.WriteLine("In {0}", MethodBase.GetCurrentMethod().Name);
            WCFContainer w = null;

            try
            {
                try
                {
                    w = (WCFContainer)correlationState;
                }
                catch (Exception ex)
                {
                    Logger.debug(ex, "can't get reference to the correlation object");
                }
                if (w == null)
                {
                    Logger.debug("can't get reference to the correlation object");
                    return;
                }

                /******************************
                 * BEGIN, do not remove this code, there's some kind of strange bug in wcf that causes services using this handler to hang after 10 invocations
                 * unless this code is execute. I believe it's related to obtaining http headers
                 */
                /*
                 * if (OperationContext.Current.IncomingMessageProperties.TryGetValue(HttpRequestMessageProperty.Name, out prop))
                 * {
                 *   HttpRequestMessageProperty reqProp = (HttpRequestMessageProperty)prop;
                 *   requestHeader = reqProp.Headers["X-MyHeader"];
                 *  // Console.WriteLine("Got the request header: {0}", requestHeader);
                 * }
                 *
                 * if (!OperationContext.Current.OutgoingMessageProperties.TryGetValue(HttpResponseMessageProperty.Name, out prop))
                 * {
                 *   prop = new HttpResponseMessageProperty();
                 *   OperationContext.Current.OutgoingMessageProperties.Add(HttpResponseMessageProperty.Name, prop);
                 * }
                 * HttpResponseMessageProperty respProp = (HttpResponseMessageProperty)prop;
                 * respProp.Headers["X-MyResponseHeader2"] = "Added from BeforeSendReply - " + requestHeader;
                 * */


                /**********************************
                 * END
                 */
            }
            catch (Exception ex)
            {
                Logger.debug(ex, "can't get reference to the correlation object");
            }

            //this is usually a ?wsdl request or a null request such a get

            MessageProcessor ctx = MessageProcessor.Instance;

            try
            {
                string use = string.Empty;
                use = w.thisid;

                String action = "urn:undeterminable";
                if (w.action != null)
                {
                    action = w.action;
                    action = action.Replace("\"", "");
                    action = action.Replace("'", "");
                    if (String.IsNullOrEmpty(action))
                    {
                        action = "urn:undeterminable";
                    }
                }
                if (action.Equals("urn:undeterminable") && w.myinboundheaders != null &&
                    !String.IsNullOrEmpty(w.myinboundheaders.Headers["SOAPAction"]))
                //   if (System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers != null &&
                //  !String.IsNullOrEmpty(System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.Action))
                {
                    action = w.myinboundheaders.Headers["SOAPAction"];
                    //System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.Action;
                    action = action.Replace("\"", "");
                    action = action.Replace("'", "");
                    if (String.IsNullOrEmpty(action))
                    {
                        action = "urn:undeterminable";
                    }
                }
                if (action.Equals("urn:undeterminable") && HttpContext.Current != null)
                {
                    action = HttpContext.Current.Request.Headers.Get("SOAPAction");
                    action = action.Replace("\"", "");
                    action = action.Replace("'", "");
                    if (String.IsNullOrEmpty(action))
                    {
                        action = "urn:undeterminable";
                    }
                }
                if (action.Equals("urn:undeterminable") && HttpContext.Current != null)
                {
                    action = HttpContext.Current.Request.HttpMethod;
                    action = action.Replace("\"", "");
                    action = action.Replace("'", "");
                    if (String.IsNullOrEmpty(action))
                    {
                        action = "urn:undeterminable";
                    }
                }
                string ip = string.Empty;
                NameValueCollection reqheaders = null;
                if (w.myinboundheaders != null && w.myinboundheaders.Headers != null)
                {
                    reqheaders = w.myinboundheaders.Headers;
                }
                NameValueCollection resheaders = new NameValueCollection();
                if (HttpContext.Current != null)
                {
                    ip         = HttpContext.Current.Request.UserHostAddress;
                    resheaders = HttpContext.Current.Response.Headers;
                    reqheaders = HttpContext.Current.Request.Headers;
                }
                else
                {
                    if (OperationContext.Current != null)
                    {
                        try
                        {
                            MessageProperties messageProperties = OperationContext.Current.IncomingMessageProperties;

                            if (w.myinboundheaders == null)
                            {
                                w.myinboundheaders = messageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
                            }
                            RemoteEndpointMessageProperty endpointProperty =
                                messageProperties[RemoteEndpointMessageProperty.Name]
                                as RemoteEndpointMessageProperty;
                            ip = endpointProperty.Address;
                        }
                        catch (Exception ex)
                        {
                            Logger.debug(ex);
                        }
                        if (w.myinboundheaders != null)
                        {
                            if (action.Equals("urn:undeterminable"))
                            {
                                action = w.myinboundheaders.Method + w.myinboundheaders.QueryString;
                            }
                            if (w.myinboundheaders.Headers != null)
                            {
                                reqheaders = w.myinboundheaders.Headers;
                            }
                        }
                    }
                }
                if (ip == "127.0.0.1" || ip == "::1")
                {
                    try
                    {
                        string myHost = System.Net.Dns.GetHostName();
                        System.Net.IPAddress[] list = System.Net.Dns.GetHostEntry(myHost).AddressList;
                        for (int i = 0; i < list.Length; i++)
                        {
                            if (!IPAddress.IsLoopback(System.Net.Dns.GetHostEntry(myHost).AddressList[i]))
                            {
                                ip = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.debug(ex);
                    }
                }

                string user = user = w.user;

                if (resheaders != null && MessageProcessor.GetConfig.DependencyInjectionEnabled)
                {
                    resheaders.Add(FGSMSConstants.MessageId, use);
                    resheaders.Add(FGSMSConstants.transactionthreadKey, w.thistid);
                }
                if (MessageProcessor.GetConfig.DependencyInjectionEnabled)// && !OperationContext.Current.OutgoingMessageProperties.TryGetValue(HttpResponseMessageProperty.Name, out prop))
                {
                    HttpResponseMessageProperty respProp = null;
                    try
                    {
                        respProp = (HttpResponseMessageProperty)OperationContext.Current.OutgoingMessageProperties[HttpResponseMessageProperty.Name];
                    }
                    catch { }
                    if (respProp != null)
                    {
                        respProp.Headers.Add(FGSMSConstants.MessageId, use);
                        respProp.Headers.Add(FGSMSConstants.transactionthreadKey, w.thistid);
                    }
                    if (reply != null && reply.Version != MessageVersion.None)
                    {
                        try
                        {
                            reply.Headers.Add(new FGSMSSoapHeaderRelatedMessageIdWCF(use));
                            reply.Headers.Add(new FGSMSSoapHeaderTransactionThreadIdWCF(w.thistid));
                        }
                        catch { }
                    }
                    //else can't add response http headers!
                }

                MessageProcessor.ProcessMessage(w.url, //System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.To.ToString(),
                                                w.start, DateTime.Now, action,
                                                w.req,
                                                //System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.ToString(),
                                                MessageHelper.MessagetoString(ref reply),
                                                //  reply.ToString(),
                                                use, (reply == null) ? false : reply.IsFault, HttpContext.Current, ip, this.GetType().FullName, user, "Current .net thread: " + Thread.CurrentThread.ManagedThreadId,
                                                //thread id
                                                w.thistid,
                                                //related transaction (id of the inbound message)
                                                w.relatedid, reqheaders, resheaders);
            }
            catch (Exception ex)
            {
                Logger.error(ex, this.GetType().FullName + " Error from FGSMS WCF Service Agent, BeforeSendReply. This typically means that the message traffic to the url " + w.url + " could not be added the queue: ");
            }
            try { MessageProcessor.ClearTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name); }
            catch (Exception ex) {
                Logger.debug(ex);
            }
        } // end BeforeSendReply
Exemple #4
0
        } // end AgentWCFServiceMessageInspector

        #region IDispatchMessageInspector Members
        /// <summary>
        /// service traffic inbound request
        /// </summary>
        /// <param name="request"></param>
        /// <param name="channel"></param>
        /// <param name="instanceContext"></param>
        /// <returns></returns>
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            string u = channel.LocalAddress.Uri.ToString();

            WCFContainer w = new WCFContainer();

            w.thistid   = "";
            w.relatedid = "";
            w.start     = DateTime.Now;
            w.url       = u;
            string query = String.Empty;

            try
            {
                //added
                w.action           = request.Headers.Action;
                w.myinboundheaders = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
                if (request != null)
                {
                    w.req = MessageHelper.MessagetoString(ref request);
                    //request.ToString();
                }
            }
            catch (Exception ex)
            {
                try
                {
                    //          if (FGSMSConstants.log) EventLog.WriteEntry(this.GetType().FullName, "Error from AfterRecieveRequest" + ex.Message);
                }
                catch { }
            }
            if (String.IsNullOrEmpty(w.url))
            {
                try
                {
                    w.url = request.Headers.To.ToString();
                    query = request.Headers.To.Query;
                    if (!String.IsNullOrEmpty(query))
                    {
                        int idx2 = w.url.LastIndexOf(query);
                        w.url = w.url.Substring(0, idx2);
                        //w.url = w.url.Replace(query, String.Empty);
                    }
                    if (w.url.EndsWith("?wsdl", StringComparison.CurrentCultureIgnoreCase))
                    {
                        w.url = w.url.Replace("?wsdl", "");
                    }
                    //w.url = w.url.Substring(0, w.url.Length - 5);
                }
                catch (Exception x)
                { }
            }
            //AgentWCFClientMessageInspector.GetHttpRequestProp(request);
            try//added
            {
                if (ServiceSecurityContext.Current != null && !ServiceSecurityContext.Current.IsAnonymous)
                {
                    w.user = ServiceSecurityContext.Current.PrimaryIdentity.Name;
                }
            }
            catch (Exception ex)
            {
                Logger.debug(ex, this.GetType().FullName + " error from AfterRecieveRequest when obtaining the current user's identity");
            }
            if (Trace.CorrelationManager.ActivityId == Guid.Empty)
            {
                Guid newGuid = Guid.NewGuid();
                Trace.CorrelationManager.ActivityId = newGuid;
            }

            try
            {
                if (request.Headers == null || request.Headers.MessageId == null)
                {
                    w.thisid = Guid.NewGuid().ToString();
                    //      if (FGSMSConstants.log) log.TraceEvent(TraceEventType.Verbose, 0, (name + " AfterReceiveRequest Main Thread id " + System.Threading.Thread.CurrentThread.ManagedThreadId +
                    //        "Error, inbound message does not have a message id. Generating a new guid"));
                }
                else
                {
                    w.thisid = request.Headers.MessageId.ToString();
                }
            }
            catch (Exception ex)
            {
                Logger.warn(ex, " error caught obtaining request headers, this is unexpected");
            }
            w.thistid = "";
            if (OperationContext.Current != null)
            {
                try
                {
                    HttpRequestMessageProperty myinboundheaders = OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
                    if (myinboundheaders != null)
                    {
                        w.relatedid = myinboundheaders.Headers[FGSMSConstants.MessageId];
                        w.thistid   = myinboundheaders.Headers[FGSMSConstants.transactionthreadKey];
                        if (!String.IsNullOrEmpty(w.thistid))   //added
                        {
                            w.thistid = w.thistid.Trim();
                        }
                        w.myinboundheaders = myinboundheaders;
                    }
                }
                catch (Exception ex)
                {
                    Logger.debug(ex);
                }
            }

            int idx = request.Headers.FindHeader(FGSMSSoapHeaderRelatedMessageIdWCF.Name2, FGSMSSoapHeaderRelatedMessageIdWCF.Namespace2);

            if (idx >= 0)
            {
                MessageHeaderInfo info = request.Headers[idx] as MessageHeaderInfo;
                if (info.Name.Equals(FGSMSSoapHeaderRelatedMessageIdWCF.Name2, StringComparison.CurrentCultureIgnoreCase) && info.Namespace.Equals(FGSMSSoapHeaderRelatedMessageIdWCF.Namespace2, StringComparison.CurrentCultureIgnoreCase))
                {
                    XmlDictionaryReader reader           = request.Headers.GetReaderAtHeader(idx);
                    FGSMSSoapHeaderRelatedMessageIdWCF h = FGSMSSoapHeaderRelatedMessageIdWCF.ReadHeader(reader);
                    reader.Close();

                    if (h != null)
                    {
                        w.relatedid = h.Id;
                    }
                }
            }
            idx = request.Headers.FindHeader(FGSMSSoapHeaderTransactionThreadIdWCF.Name2, FGSMSSoapHeaderTransactionThreadIdWCF.Namespace2);
            if (idx >= 0)
            {
                XmlDictionaryReader reader = request.Headers.GetReaderAtHeader(idx);
                FGSMSSoapHeaderTransactionThreadIdWCF h = FGSMSSoapHeaderTransactionThreadIdWCF.ReadHeader(reader);
                reader.Close();

                if (h != null)
                {
                    w.thistid = h.Id;
                }
            }


            if (String.IsNullOrEmpty(w.thistid))
            {
                w.thistid = Guid.NewGuid().ToString();
            }
            MessageProcessor.SetTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name, w.thistid);

            /*
             * DependencyContextExtension x = DependencyContextExtension.Current;
             * if (x == null)
             *   DependencyContextExtension.Add(OperationContext.Current);
             */

            //c.Add(Thread.CurrentContext.ContextID.ToString() + ":" + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + ":" + Thread.CurrentThread.Name, w);


            return(w);
        } // end AfterReceiveRequest
Exemple #5
0
        /// <summary>
        /// service traffic inbound request
        /// </summary>
        /// <param name="request"></param>
        /// <param name="channel"></param>
        /// <param name="instanceContext"></param>
        /// <returns></returns>
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            string u=channel.LocalAddress.Uri.ToString();

            WCFContainer w = new WCFContainer();
            w.thistid = "";
            w.relatedid = "";
            w.start = DateTime.Now;
            w.url = u;
            string query = String.Empty;
            try
            {
                //added
                w.action = request.Headers.Action;
                w.myinboundheaders = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
                if (request != null)
                {
                    w.req = MessageHelper.MessagetoString(ref request);
                        //request.ToString();
                }
            }
            catch (Exception ex)
            {
                try
                {
                    //          if (FGSMSConstants.log) EventLog.WriteEntry(this.GetType().FullName, "Error from AfterRecieveRequest" + ex.Message);
                }
                catch { }
            }
            if (String.IsNullOrEmpty(w.url))
            try
            {
                w.url = request.Headers.To.ToString();
                query = request.Headers.To.Query;
                if (!String.IsNullOrEmpty(query))
                {
                    int idx2 = w.url.LastIndexOf(query);
                    w.url = w.url.Substring(0, idx2);
                    //w.url = w.url.Replace(query, String.Empty);
                }
                if (w.url.EndsWith("?wsdl", StringComparison.CurrentCultureIgnoreCase))
                    w.url = w.url.Replace("?wsdl", "");
                //w.url = w.url.Substring(0, w.url.Length - 5);
            }
            catch (Exception x)
            { }
            //AgentWCFClientMessageInspector.GetHttpRequestProp(request);
            try//added
            {
                if (ServiceSecurityContext.Current != null && !ServiceSecurityContext.Current.IsAnonymous)
                    w.user = ServiceSecurityContext.Current.PrimaryIdentity.Name;
            }
            catch (Exception ex)
            {
                Logger.debug(ex, this.GetType().FullName + " error from AfterRecieveRequest when obtaining the current user's identity");
            }
            if (Trace.CorrelationManager.ActivityId == Guid.Empty)
            {
                Guid newGuid = Guid.NewGuid();
                Trace.CorrelationManager.ActivityId = newGuid;
            }

            try
            {
                if (request.Headers == null || request.Headers.MessageId == null)
                {
                    w.thisid = Guid.NewGuid().ToString();
                    //      if (FGSMSConstants.log) log.TraceEvent(TraceEventType.Verbose, 0, (name + " AfterReceiveRequest Main Thread id " + System.Threading.Thread.CurrentThread.ManagedThreadId +
                    //        "Error, inbound message does not have a message id. Generating a new guid"));
                }
                else w.thisid = request.Headers.MessageId.ToString();
            }
            catch (Exception ex)
            {
                Logger.warn(ex, " error caught obtaining request headers, this is unexpected");
            }
            w.thistid = "";
            if (OperationContext.Current != null)
            {
                try
                {
                    HttpRequestMessageProperty myinboundheaders = OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
                    if (myinboundheaders != null)
                    {
                        w.relatedid = myinboundheaders.Headers[FGSMSConstants.MessageId];
                        w.thistid = myinboundheaders.Headers[FGSMSConstants.transactionthreadKey];
                        if (!String.IsNullOrEmpty(w.thistid))   //added
                            w.thistid = w.thistid.Trim();
                        w.myinboundheaders = myinboundheaders;

                    }
                }
                catch (Exception ex)
                {
                    Logger.debug(ex);
                }
            }

            int idx = request.Headers.FindHeader(FGSMSSoapHeaderRelatedMessageIdWCF.Name2, FGSMSSoapHeaderRelatedMessageIdWCF.Namespace2);
            if (idx >= 0)
            {
                MessageHeaderInfo info = request.Headers[idx] as MessageHeaderInfo;
                if (info.Name.Equals(FGSMSSoapHeaderRelatedMessageIdWCF.Name2, StringComparison.CurrentCultureIgnoreCase) && info.Namespace.Equals(FGSMSSoapHeaderRelatedMessageIdWCF.Namespace2, StringComparison.CurrentCultureIgnoreCase))
                {
                    XmlDictionaryReader reader = request.Headers.GetReaderAtHeader(idx);
                    FGSMSSoapHeaderRelatedMessageIdWCF h = FGSMSSoapHeaderRelatedMessageIdWCF.ReadHeader(reader);
                    reader.Close();

                    if (h != null)
                        w.relatedid = h.Id;
                }
            }
            idx = request.Headers.FindHeader(FGSMSSoapHeaderTransactionThreadIdWCF.Name2, FGSMSSoapHeaderTransactionThreadIdWCF.Namespace2);
            if (idx >= 0)
            {
                XmlDictionaryReader reader = request.Headers.GetReaderAtHeader(idx);
                FGSMSSoapHeaderTransactionThreadIdWCF h = FGSMSSoapHeaderTransactionThreadIdWCF.ReadHeader(reader);
                reader.Close();

                if (h != null)
                    w.thistid = h.Id;
            }

            if (String.IsNullOrEmpty(w.thistid))
                w.thistid = Guid.NewGuid().ToString();
            MessageProcessor.SetTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name, w.thistid);
            /*
             DependencyContextExtension x = DependencyContextExtension.Current;
             if (x == null)
                 DependencyContextExtension.Add(OperationContext.Current);
             */

            //c.Add(Thread.CurrentContext.ContextID.ToString() + ":" + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + ":" + Thread.CurrentThread.Name, w);

            return w;
        }
Exemple #6
0
        /// <summary>
        /// agent for client traffic outbound request
        /// </summary>
        /// <param name="request"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
        {
            WCFContainer w = new WCFContainer();
            w.thistid = "";
            w.thisid = Guid.NewGuid().ToString();
            w.start = DateTime.Now;

            //request.Headers.Add(new FGSMSAgentHeaderWCF());
            //  start = DateTime.Now;
            if (request != null)
              w.req=  MessageHelper.MessagetoString(ref request);
              //  w.req = request.ToString();

            w.url = "urn:undeterminable";
            w.action = "urn:undeterminable";
            if (System.ServiceModel.OperationContext.Current != null &&
                System.ServiceModel.OperationContext.Current.RequestContext != null &&
                System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage != null &&
                System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers != null &&
                !String.IsNullOrEmpty(System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.Action))
            {
                w.action = System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.Action;
                w.action.Replace("\"", "");
                w.action.Replace("'", "");
                if (String.IsNullOrEmpty(w.action))
                    w.action = "urn:undeterminable";
            }
            if (w.action.Equals("urn:undeterminable"))
            {
                w.action = request.Headers.Action;
                w.action.Replace("\"", "");
                w.action.Replace("'", "");
                if (String.IsNullOrEmpty(w.action))
                    w.action = "urn:undeterminable";

            }
            if (w.action.Equals("urn:undeterminable") && HttpContext.Current != null)
            {
                w.action = HttpContext.Current.Request.Headers.Get("SOAPAction");
                w.action = w.action.Replace("\"", "");
                w.action = w.action.Replace("'", "");
                if (String.IsNullOrEmpty(w.action))
                    w.action = "urn:undeterminable";
            }
            if (w.action.Equals("urn:undeterminable") && HttpContext.Current != null)
            {
                w.action = HttpContext.Current.Request.HttpMethod;
                w.action = w.action.Replace("\"", "");
                w.action = w.action.Replace("'", "");
                if (String.IsNullOrEmpty(w.action))
                    w.action = "urn:undeterminable";
            }
            if (w.action.Equals("urn:undeterminable"))
            {
                w.action = getActionFromFirstXmlNode(request);
            }
            if (OperationContext.Current != null && request.Headers.To != null)
            {
                w.url = request.Headers.To.ToString();

            }
            if (w.url.Equals("urn:undeterminable"))
            {
                w.url = channel.RemoteAddress.Uri.ToString();
            }
            if (w.url.Equals("urn:undeterminable"))
            {

            }
            //changed
            w.thistid = MessageProcessor.GetTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name);
            if (!String.IsNullOrEmpty(w.thistid))
                w.thistid = w.thistid.Trim();
            if (String.IsNullOrEmpty(w.thistid))
            {
                w.thistid = Guid.NewGuid().ToString();
            }
            // string relatedtransaction = "";
            w.myinboundheaders = GetHttpRequestProp(request);//request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            if (w.myinboundheaders != null && MessageProcessor.GetConfig.DependencyInjectionEnabled)
            {
                //for some reason, the http request property is persisted across objects when the proxy object is reused.
                //its most likely an optimization from microsoft regarding http session reuse.

                //When in visual studuo, it added http headers to outbound connections called vscasultydata, it will also be repeated when
                //persisting connections...
                w.myinboundheaders.Headers.Remove("VsDebuggerCausalityData");
                w.myinboundheaders.Headers.Remove(FGSMSConstants.MessageId);
                w.myinboundheaders.Headers.Remove(FGSMSConstants.transactionthreadKey);

                w.myinboundheaders.Headers.Add(FGSMSConstants.MessageId, w.thisid);
                w.myinboundheaders.Headers.Add(FGSMSConstants.transactionthreadKey, w.thistid);

            }
            if (MessageProcessor.GetConfig.DependencyInjectionEnabled)
            {
                //  MessageHeader mh = new MessageHeader("value").GetUnt
                request.Headers.Add(new FGSMSSoapHeaderTransactionThreadIdWCF(w.thistid));
                request.Headers.Add(new FGSMSSoapHeaderRelatedMessageIdWCF(w.thisid));
            }
            return w;
        }