/// <summary>
        /// Adds the required properties to the message.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="message"></param>
        void ReadProperties(HttpRequest request, Message message)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            // existing WCF HTTP request property
            var p1 = new HttpRequestMessageProperty();

            p1.Method      = request.Method;
            p1.QueryString = request.QueryString.ToString();
            foreach (var header in request.Headers)
            {
                p1.Headers.Add(header.Key, header.Value);
            }
            message.Properties.Add(HttpRequestMessageProperty.Name, p1);

            // new ASP.Net core property
            var p2 = new AspNetCoreMessageProperty();

            p2.Context = request.HttpContext;
            message.Properties.Add(AspNetCoreMessageProperty.Name, p2);
        }
Ejemplo n.º 2
0
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (reply.IsFault)
            {
                return;
            }

            var http = reply.Properties.GetValue <HttpResponseMessageProperty>(HttpResponseMessageProperty.Name);

            if (http == null)
            {
                return;
            }

            if (http.Headers.Get("Content-Type") is string c)
            {
                var contentType = new ContentType(c);
                if (contentType.MediaType != "text/xml")
                {
                    return;
                }

                var message = reply.CreateBufferedCopy(int.MaxValue);
                var reading = message.CreateMessage().GetReaderAtBodyContents();

                // content is a WSDL
                if (reading.IsStartElement("definitions", "http://schemas.xmlsoap.org/wsdl/"))
                {
                    var service = System.Web.Services.Description.ServiceDescription.Read(reading);
                    if (service != null)
                    {
                        // use rewriter to change service description
                        var property = AspNetCoreMessageProperty.GetValue((MessageProperties)correlationState);
                        var rewriter = new AspNetCoreMetadataRewriter(property);
                        rewriter.RewriteServiceDescription(service);

                        reply = Message.CreateMessage(
                            reply.Version,
                            reply.Headers.Action,
                            new XmlSerializerBodyWriter(System.Web.Services.Description.ServiceDescription.Serializer, service));

                        return;
                    }
                }

                if (reading.IsStartElement("schema", "http://www.w3.org/2001/XMLSchema"))
                {
                    var schema = XmlSchema.Read(reading, (s, a) => { });
                    if (schema != null)
                    {
                        // use rewriter to change service description
                        var property = AspNetCoreMessageProperty.GetValue((MessageProperties)correlationState);
                        var rewriter = new AspNetCoreMetadataRewriter(property);
                        rewriter.RewriteSchema(schema);

                        reply = Message.CreateMessage(
                            reply.Version,
                            reply.Headers.Action,
                            new XmlSchemaBodyWriter(schema));

                        return;
                    }
                }

                // reconstitute original message if we failed to match
                reply = message.CreateMessage();
            }
        }
Ejemplo n.º 3
0
        public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
        {
            var ctx = OperationContext.Current;

            new AspNetCoreMetadataRewriter(ctx != null ? AspNetCoreMessageProperty.GetValue(ctx.IncomingMessageProperties) : null).RewriteEndpoint(exporter);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Gets the ASP.Net Core ServiceModel routing URI for the given request context.
 /// </summary>
 /// <param name="self"></param>
 /// <returns></returns>
 public static Uri GetAspNetCoreRoutingUri(this OperationContext self)
 {
     return(AspNetCoreMessageProperty.GetValue(self.IncomingMessageProperties)?.RoutingUri);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets the ASP.Net Core <see cref="HttpContext"/> executing the given request context.
 /// </summary>
 /// <param name="self"></param>
 /// <returns></returns>
 public static HttpContext GetAspNetCoreContext(this OperationContext self)
 {
     return(AspNetCoreMessageProperty.GetValue(self.IncomingMessageProperties)?.Context);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 public AspNetCoreMetadataRewriter(AspNetCoreMessageProperty properties)
 {
     this.properties = properties;
 }