예제 #1
0
        /// <summary>
        /// Executes X-Road operation on endpoint specified by WebRequest parameter.
        /// </summary>
        /// <param name="webRequest">WebRequest used to transfer X-Road messages.</param>
        /// <param name="body">Soap body part of outgoing serialized X-Road message.</param>
        /// <param name="header">Soap header part of outgoing serialized X-Road message.</param>
        /// <param name="options">Additional options to configure service call execution.</param>
        /// <typeparam name="TResult">Expected result type of the operation.</typeparam>
        /// <returns>Deserialized value of X-Road response message Soap body.</returns>
        public virtual TResult Execute <TResult>(WebRequest webRequest, object body, THeader header, ServiceExecutionOptions options = null)
        {
            using (var requestMessage = new XRoadMessage(this, header))
            {
                IServiceMap operationServiceMap;
                using (var writer = XmlWriter.Create(requestMessage.ContentStream))
                {
                    writer.WriteStartDocument();

                    Style.WriteSoapEnvelope(writer, ProducerNamespace);
                    if (!string.IsNullOrEmpty(options?.RequestNamespace))
                    {
                        writer.WriteAttributeString(PrefixConstants.XMLNS, "req", NamespaceConstants.XMLNS, options.RequestNamespace);
                    }

                    Style.WriteSoapHeader(writer, header, HeaderDefinition);

                    writer.WriteStartElement("Body", NamespaceConstants.SOAP_ENV);

                    var operationName = XName.Get(options?.OperationName ?? header.Service.ServiceCode, ProducerNamespace);
                    operationServiceMap = options?.ServiceMap ?? GetSerializer(options?.Version ?? requestMessage.Version).GetServiceMap(operationName);
                    operationServiceMap.SerializeRequest(writer, body, requestMessage, options?.RequestNamespace);

                    writer.WriteEndElement();

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }

                options?.BeforeRequest?.Invoke(this, new XRoadRequestEventArgs(requestMessage));

                requestMessage.SaveTo(webRequest);

                using (var response = webRequest.GetResponseAsync().Result)
                    using (var responseStream = response.GetResponseStream())
                        using (var seekableStream = new MemoryStream())
                            using (var responseMessage = new XRoadMessage())
                            {
                                responseStream?.CopyTo(seekableStream);
                                options?.BeforeDeserialize?.Invoke(this, new XRoadResponseEventArgs(response, seekableStream));
                                responseMessage.LoadResponse(seekableStream, response.Headers.GetContentTypeHeader(), Path.GetTempPath(), this);
                                return((TResult)responseMessage.DeserializeMessageContent(operationServiceMap));
                            }
            }
        }
예제 #2
0
        /// <inheritdoc />
        object IServiceManager.Execute(WebRequest webRequest, object body, ISoapHeader header, ServiceExecutionOptions options)
        {
            var messageFormatter = options?.MessageFormatter ?? new SoapMessageFormatter();

            using (var requestMessage = new XRoadMessage(this, header))
            {
                IServiceMap operationServiceMap;
                using (var writer = XmlWriter.Create(requestMessage.ContentStream))
                {
                    writer.WriteStartDocument();

                    writer.WriteSoapEnvelope(messageFormatter, ProtocolDefinition);
                    if (!string.IsNullOrEmpty(options?.RequestNamespace) && writer.LookupPrefix(options.RequestNamespace) == null)
                    {
                        writer.WriteAttributeString(PrefixConstants.XMLNS, "req", NamespaceConstants.XMLNS, options.RequestNamespace);
                    }

                    messageFormatter.WriteSoapHeader(writer, Style, header, HeaderDefinition);
                    messageFormatter.WriteStartBody(writer);

                    var serviceCode = (header as IXRoadHeader)?.Service?.ServiceCode ?? string.Empty;

                    var operationName = XName.Get(options?.OperationName ?? serviceCode, ProducerNamespace);
                    operationServiceMap = options?.ServiceMap ?? GetSerializer(options?.Version ?? requestMessage.Version).GetServiceMap(operationName);
                    operationServiceMap.SerializeRequest(writer, body, requestMessage, options?.RequestNamespace);

                    writer.WriteEndElement();

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }

                options?.BeforeRequest?.Invoke(this, new XRoadRequestEventArgs(requestMessage));

                requestMessage.SaveTo(webRequest, messageFormatter);

                using (var response = webRequest.GetResponseAsync().Result)
                    using (var responseStream = response.GetResponseStream())
                        using (var seekableStream = new MemoryStream())
                            using (var responseMessage = new XRoadMessage())
                            {
                                responseStream?.CopyTo(seekableStream);
                                options?.BeforeDeserialize?.Invoke(this, new XRoadResponseEventArgs(response, seekableStream));
                                responseMessage.LoadResponse(seekableStream, messageFormatter, response.Headers.GetContentTypeHeader(), Path.GetTempPath(), this);
                                return(responseMessage.DeserializeMessageContent(operationServiceMap, messageFormatter));
                            }
            }
        }
예제 #3
0
 /// <inheritdoc />
 public virtual TResult Execute <TResult>(WebRequest webRequest, object body, XRoadCommonHeader commonHeader, ServiceExecutionOptions options = null) =>
 Execute <TResult>(webRequest, body, new THeader().InitFrom(commonHeader), options);