1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.ServiceModel;
5 using System.ServiceModel.Channels;
6 using System.ServiceModel.Description;
7 using System.ServiceModel.Dispatcher;
8 
9 namespace System.ServiceModel.Routing
10 {
11 	public class SoapProcessingBehavior : IEndpointBehavior
12 	{
13 		class MessageInspector : IClientMessageInspector
14 		{
MessageInspector(SoapProcessingBehavior owner, ServiceEndpoint endpoint)15 			public MessageInspector (SoapProcessingBehavior owner, ServiceEndpoint endpoint)
16 			{
17 				this.endpoint = endpoint;
18 			}
19 
20 			ServiceEndpoint endpoint;
21 
BeforeSendRequest(ref Message request, IClientChannel channel)22 			public object BeforeSendRequest (ref Message request, IClientChannel channel)
23 			{
24 				// See "Request processing" at http://msdn.microsoft.com/en-us/library/ee517422%28VS.100%29.aspx
25 
26 				var original = request;
27 				var msg = Message.CreateMessage (endpoint.Binding.MessageVersion, request.Headers.Action, request.GetReaderAtBodyContents ());
28 				// The above page does not explain, but it should do copy headers (other than addressing ones, to be removed later).
29 				msg.Headers.Action = null;
30 				msg.Headers.CopyHeadersFrom (request);
31 				msg.Headers.To = null;
32 				msg.Headers.From = null;
33 				msg.Headers.FaultTo = null;
34 				msg.Headers.RelatesTo = null;
35 				msg.Properties.CopyProperties (request.Properties);
36 
37 				request = msg;
38 				return original;
39 			}
40 
AfterReceiveReply(ref Message reply, object correlationState)41 			public void AfterReceiveReply (ref Message reply, object correlationState)
42 			{
43 				// See "Response processing" at http://msdn.microsoft.com/en-us/library/ee517422%28VS.100%29.aspx
44 
45 				Message original = (Message) correlationState;
46 
47 				var msg = Message.CreateMessage (original.Version, reply.Headers.Action, reply.GetReaderAtBodyContents ());
48 				// The above page does not explain, but it should do copy headers (other than addressing ones, to be removed later).
49 				msg.Headers.Action = null;
50 				msg.Headers.CopyHeadersFrom (reply);
51 				/*
52 				msg.Headers.To = null;
53 				msg.Headers.From = null;
54 				msg.Headers.FaultTo = null;
55 				msg.Headers.RelatesTo = null;
56 				*/
57 				msg.Properties.CopyProperties (reply.Properties);
58 				reply = msg;
59 			}
60 		}
61 
SoapProcessingBehavior()62 		public SoapProcessingBehavior ()
63 		{
64 			ProcessMessages = true;
65 		}
66 
67 		public bool ProcessMessages { get; set; }
68 
AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)69 		public void AddBindingParameters (ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
70 		{
71 			// nothing to do here.
72 		}
73 
ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)74 		public void ApplyClientBehavior (ServiceEndpoint endpoint, ClientRuntime clientRuntime)
75 		{
76 			if (!ProcessMessages)
77 				return;
78 			clientRuntime.MessageInspectors.Add (new MessageInspector (this, endpoint));
79 		}
80 
ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)81 		public void ApplyDispatchBehavior (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
82 		{
83 			throw new NotSupportedException (); // ... right? I assume it is not for services.
84 		}
85 
Validate(ServiceEndpoint endpoint)86 		public void Validate (ServiceEndpoint endpoint)
87 		{
88 		}
89 	}
90 }
91