Esempio n. 1
0
        /// <summary cref="IClientMessageInspector.BeforeSendRequest" />
        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            // extract request body from message object.
            IServiceMessage requestBody = (IServiceMessage)request.Properties[MessageProperties.RequestBody];

            // extract the request from the message body.
            IEncodeable encodeable = requestBody.GetRequest();

            byte[] encodedBody = null;

            if (!m_transportSupportsEncoding)
            {
                encodedBody = BinaryEncoder.EncodeMessage(encodeable, m_messageContext);
            }

            // create encoded message.
            Message encodedRequest = Message.CreateMessage(
                request.Version,
                Namespaces.OpcUaWsdl + "/InvokeService",
                new InvokeServiceBodyWriter(encodedBody, true));

            encodedRequest.Headers.To        = request.Headers.To;
            encodedRequest.Headers.MessageId = request.Headers.MessageId;
            encodedRequest.Headers.From      = request.Headers.From;

            if (encodedBody != null)
            {
                encodedRequest.Properties.Add(MessageProperties.EncodedBody, encodedBody);
            }

            encodedRequest.Properties.Add(MessageProperties.UnencodedBody, encodeable);

            // save information need to process reply.
            object correlationState = new object[] { request.Version, request.Headers.Action, requestBody };

            // replace the outgoing message.
            request = encodedRequest;

            return(correlationState);
        }
Esempio n. 2
0
        /// <summary cref="IClientMessageInspector.AfterReceiveReply" />
        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            // check for fault.
            if (reply.IsFault)
            {
                return;
            }

            // parse request parameters.
            object[] parameters = correlationState as object[];

            if (parameters == null || parameters.Length != 3)
            {
                throw new InvalidOperationException("Cannot decode request because the IClientMessageInspector not configured properly.");
            }

            // extract request parameters.
            MessageVersion  messageVersion = parameters[0] as MessageVersion;
            string          action         = parameters[1] as string;
            IServiceMessage request        = parameters[2] as IServiceMessage;

            object encodeable = null;

            if (!reply.Properties.TryGetValue(MessageProperties.UnencodedBody, out encodeable))
            {
                // extract binary encoded response from body.
                XmlDictionaryReader reader = reply.GetReaderAtBodyContents();
                reader.MoveToStartElement("InvokeServiceResponse", Namespaces.OpcUaXsd);
                byte[] response = reader.ReadElementContentAsBase64();

                // decode body.
                try
                {
                    encodeable = BinaryDecoder.DecodeMessage(response, null, m_messageContext);
                }
                catch (Exception e)
                {
                    ServiceResult error = ServiceResult.Create(
                        e,
                        StatusCodes.BadDecodingError,
                        "Could not decoding incoming response message.");

                    ServiceFault fault = new ServiceFault();

                    fault.ResponseHeader.RequestHandle = request.GetRequest().RequestHeader.RequestHandle;
                    fault.ResponseHeader.Timestamp     = DateTime.UtcNow;
                    fault.ResponseHeader.ServiceResult = error.Code;

                    StringTable stringTable = new StringTable();

                    fault.ResponseHeader.ServiceDiagnostics = new DiagnosticInfo(
                        error,
                        DiagnosticsMasks.NoInnerStatus,
                        true,
                        stringTable);

                    fault.ResponseHeader.StringTable = stringTable.ToArray();

                    encodeable = fault;
                }
            }

            object unencodedBody = request.CreateResponse((IServiceResponse)encodeable);

            // create the unencoded reply message.
            Message unencodedReply = Message.CreateMessage(
                messageVersion,
                action + "Response",
                unencodedBody);

            unencodedReply.Headers.MessageId = reply.Headers.MessageId;
            unencodedReply.Headers.RelatesTo = reply.Headers.RelatesTo;

            unencodedReply.Properties.Add(MessageProperties.UnencodedBody, unencodedBody);

            // replace the incoming message.
            reply = unencodedReply;
        }