Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the dispatcher, but once you consume the message, you'll need to recreate it before returning from the method. The code below shows an example of it.</p> <pre><code>public class StackOverflow_7506072 { [DataContract(Name = "MyClass1", Namespace = "")] public class MyClass1 { [DataMember] public string MyProperty { get; set; } } [ServiceContract] public class Service { [WebInvoke(UriTemplate = "", Method = "POST")] public MyClass1 GetItem(MyClass1 postedItem) { return postedItem; } } public class MyInspector : IEndpointBehavior, IDispatchMessageInspector { public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this); } public void Validate(ServiceEndpoint endpoint) { } public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { MemoryStream ms = new MemoryStream(); XmlWriter w = XmlWriter.Create(ms); request.WriteMessage(w); w.Flush(); ms.Position = 0; XElement element = XElement.Load(ms); if (element.Name.NamespaceName == "http://schemas.datacontract.org/2004/07/MyNamespace") { element.Name = XName.Get(element.Name.LocalName, ""); foreach (XElement child in element.Descendants()) { if (child.Name.NamespaceName == "http://schemas.datacontract.org/2004/07/MyNamespace") { child.Name = XName.Get(child.Name.LocalName, ""); } } element.Attribute("xmlns").Remove(); } XmlReader r = element.CreateReader(); Message newRequest = Message.CreateMessage(r, int.MaxValue, request.Version); newRequest.Properties.CopyProperties(request.Properties); request = newRequest; return null; } public void BeforeSendReply(ref Message reply, object correlationState) { } } public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), ""); endpoint.Behaviors.Add(new WebHttpBehavior()); endpoint.Behaviors.Add(new MyInspector()); host.Open(); Console.WriteLine("Host opened"); WebClient c = new WebClient(); c.Headers[HttpRequestHeader.ContentType] = "text/xml"; string xml = "&lt;MyClass1&gt;&lt;MyProperty&gt;123&lt;/MyProperty&gt;&lt;/MyClass1&gt;"; Console.WriteLine(c.UploadString(baseAddress + "/", xml)); c = new WebClient(); c.Headers[HttpRequestHeader.ContentType] = "text/xml"; xml = "&lt;MyClass1 xmlns=\"http://schemas.datacontract.org/2004/07/MyNamespace\"&gt;&lt;MyProperty&gt;123&lt;/MyProperty&gt;&lt;/MyClass1&gt;"; Console.WriteLine(c.UploadString(baseAddress + "/", xml)); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload