// In the normal case, we'll use the OperationContext
        // found in the local thread. However, there are cases this won't work:
        // - When async calls have been done and .NET < 4.6
        // - Within a *client* side OperationContextScope
        // To work around this, we store a copy of our context on the
        // thread's LogicalCallContext, so that it gets moved from thread to thread
        // Because this field is not serializable, we store an
        // ObjectHandle instead.
        public static WcfOperationContext FindContext(OperationContext owner)
        {
            // don't retrieve a context for a client-side OperationContext
            if (owner != null && owner.IsClientSideContext())
            {
                return(null);
            }

            WcfOperationContext context = null;

            if (context == null && owner != null)
            {
                context = owner.Extensions.Find <WcfOperationContext>();
            }

            if (context == null)
            {
                var handle = CallContext.LogicalGetData(CallContextProperty) as ObjectHandle;
                if (handle != null)
                {
                    context = handle.Unwrap() as WcfOperationContext;
                }
            }

            return(context);
        }
        private static IOperationContext GetContext()
        {
            var owner = OperationContext.Current;
            WcfOperationContext context = FindContext(owner);

            if (context == null && owner != null)
            {
                context = new WcfOperationContext(owner);
                owner.Extensions.Add(context);
            }
            return(context);
        }
Exemplo n.º 3
0
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            IOperationContext context = WcfOperationContext.Current;

            if (context != null)
            {
                WcfOperationContext.StoreThreadContext(context);

                if (!this.LogTelemetryFor(context))
                {
                    WcfEventSource.Log.OperationIgnored(context.ContractName, context.ContractNamespace, context.OperationName);
                    return(null);
                }

                foreach (var mod in this.GetModules())
                {
                    Executor.ExceptionSafe(
                        mod.GetType().Name,
                        "OnBeginRequest",
                        mod.OnBeginRequest,
                        context);
                }

                foreach (var mod in this.GetModules())
                {
                    var tracer = mod as IWcfMessageTrace;
                    if (tracer != null)
                    {
                        Executor.ExceptionSafe(
                            mod.GetType().Name,
                            "OnTraceRequest",
                            tracer.OnTraceRequest,
                            context,
                            ref request);
                    }
                }
            }
            else
            {
                WcfEventSource.Log.NoOperationContextFound();
            }

            return(null);
        }
Exemplo n.º 4
0
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            var context = WcfOperationContext.Current;

            if (context != null)
            {
                WcfOperationContext.ClearThreadContext();

                // Do not run OnEndRequest for stuff we're not interested in!
                if (!this.LogTelemetryFor(context))
                {
                    return;
                }

                foreach (var mod in this.GetModules())
                {
                    var tracer = mod as IWcfMessageTrace;
                    if (tracer != null)
                    {
                        Executor.ExceptionSafe(
                            mod.GetType().Name,
                            "OnTraceResponse",
                            tracer.OnTraceResponse,
                            context,
                            ref reply);
                    }
                }

                foreach (var mod in this.GetModules())
                {
                    Executor.ExceptionSafe(
                        mod.GetType().Name,
                        "OnEndRequest",
                        mod.OnEndRequest,
                        context,
                        reply);
                }
            }
            else
            {
                WcfEventSource.Log.NoOperationContextFound();
            }
        }
        private static IOperationContext GetContext()
        {
            var owner = OperationContext.Current;

            if (owner != null && owner.IsClientSideContext())
            {
                owner = null;
            }
            WcfOperationContext context = FindContext(owner);

            if (context == null)
            {
                if (owner != null)
                {
                    context = new WcfOperationContext(owner, PlatformContext.RequestFromHttpContext());
                    owner.Extensions.Add(context);
                    // backup in case we can't get to the server-side OperationContext later
                    CallContext.LogicalSetData(CallContextProperty, new ObjectHandle(context));
                }
                // no server-side OperationContext to attach to
            }
            return(context);
        }