object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            object messageProperty;

            if (OperationContext.Current.OutgoingMessageProperties.TryGetValue(
                    DiscoveryMessageProperty.Name, out messageProperty))
            {
                DiscoveryMessageProperty discoveryMessageProperty = messageProperty as DiscoveryMessageProperty;
                if (discoveryMessageProperty != null)
                {
                    UdpAddressingState state = discoveryMessageProperty.CorrelationState as UdpAddressingState;
                    if (state != null)
                    {
                        if (state.RemoteEndpointAddress != null)
                        {
                            AnnonymousAddress.ApplyTo(request);
                            request.Properties.Via = state.RemoteEndpointAddress;
                        }

                        if (state.NetworkInterfaceMessageProperty != null)
                        {
                            state.NetworkInterfaceMessageProperty.AddTo(request);
                        }
                    }
                }
            }

            return(null);
        }
        public DiscoveryOperationContext(OperationContext operationContext)
        {
            Fx.Assert(operationContext != null, "The operationContext must be non null.");

            if (Fx.Trace.IsEtwProviderEnabled)
            {
                this.eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(operationContext.IncomingMessage);
            }

            this.operationContext          = operationContext;
            this.operationContextExtension = DiscoveryOperationContext.GetDiscoveryOperationContextExtension(this.operationContext);
            this.messageProperty           = DiscoveryOperationContext.GetDiscoveryMessageProperty(this.operationContext);

            this.thisLock = new object();
        }
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            // obtain the remote transport address information and include it in the ReplyTo
            object messageProperty = null;

            UdpAddressingState addressingState = null;

            if (OperationContext.Current.IncomingMessageProperties.TryGetValue(RemoteEndpointMessageProperty.Name, out messageProperty))
            {
                RemoteEndpointMessageProperty remoteEndpointProperty = messageProperty as RemoteEndpointMessageProperty;
                if (remoteEndpointProperty != null)
                {
                    UriBuilder uriBuilder = new UriBuilder();
                    uriBuilder.Scheme = this.scheme;
                    uriBuilder.Host   = remoteEndpointProperty.Address;
                    uriBuilder.Port   = remoteEndpointProperty.Port;

                    addressingState = new UdpAddressingState();
                    addressingState.RemoteEndpointAddress = uriBuilder.Uri;

                    OperationContext.Current.IncomingMessageHeaders.ReplyTo = AnnonymousAddress;
                }
            }

            NetworkInterfaceMessageProperty udpMessageProperty;

            if (NetworkInterfaceMessageProperty.TryGet(OperationContext.Current.IncomingMessageProperties, out udpMessageProperty))
            {
                if (addressingState == null)
                {
                    addressingState = new UdpAddressingState();
                }

                addressingState.NetworkInterfaceMessageProperty = udpMessageProperty;
            }

            if (addressingState != null)
            {
                DiscoveryMessageProperty discoveryMessageProperty = new DiscoveryMessageProperty(addressingState);
                OperationContext.Current.IncomingMessageProperties[DiscoveryMessageProperty.Name] = discoveryMessageProperty;
            }

            return(null);
        }
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            // obtain the remote transport address information and include it in the ReplyTo
            object messageProperty = null;

            UdpAddressingState addressingState = null;

            if (OperationContext.Current.IncomingMessageProperties.TryGetValue(RemoteEndpointMessageProperty.Name, out messageProperty))
            {
                RemoteEndpointMessageProperty remoteEndpointProperty = messageProperty as RemoteEndpointMessageProperty;
                if (remoteEndpointProperty != null)
                {
                    UriBuilder uriBuilder = new UriBuilder();
                    uriBuilder.Scheme = this.scheme;
                    uriBuilder.Host = remoteEndpointProperty.Address;
                    uriBuilder.Port = remoteEndpointProperty.Port;

                    addressingState = new UdpAddressingState();
                    addressingState.RemoteEndpointAddress = uriBuilder.Uri;

                    OperationContext.Current.IncomingMessageHeaders.ReplyTo = AnnonymousAddress;                    
                }
            }

            NetworkInterfaceMessageProperty udpMessageProperty;
            if (NetworkInterfaceMessageProperty.TryGet(OperationContext.Current.IncomingMessageProperties, out udpMessageProperty))
            {               
                if (addressingState == null)
                {
                    addressingState = new UdpAddressingState();
                }

                addressingState.NetworkInterfaceMessageProperty = udpMessageProperty;
            }

            if (addressingState != null)
            {
                DiscoveryMessageProperty discoveryMessageProperty = new DiscoveryMessageProperty(addressingState);
                OperationContext.Current.IncomingMessageProperties[DiscoveryMessageProperty.Name] = discoveryMessageProperty;
            }

            return null;
        }