2009-09-02 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Wed, 2 Sep 2009 10:35:08 +0000 (10:35 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Wed, 2 Sep 2009 10:35:08 +0000 (10:35 -0000)
* WebHttpBehavior.cs : now message formatters are pairs of request/
  reply formatters.

* WebMessageFormatter.cs : override some serialization methods as
  prohibited.

* WebHttpBehaviorTest.cs : some message formatters tests.

svn path=/trunk/mcs/; revision=141112

mcs/class/System.ServiceModel.Web/System.ServiceModel.Description/ChangeLog
mcs/class/System.ServiceModel.Web/System.ServiceModel.Description/WebHttpBehavior.cs
mcs/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/ChangeLog
mcs/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/WebMessageFormatter.cs
mcs/class/System.ServiceModel.Web/Test/System.ServiceModel.Description/ChangeLog
mcs/class/System.ServiceModel.Web/Test/System.ServiceModel.Description/WebHttpBehaviorTest.cs

index 56fc46ddeb0da9e698600ebf792b5b8537e18bf7..d2a8cfe5a7776f7827db51b96d45270052f1838d 100644 (file)
@@ -1,3 +1,8 @@
+2009-09-02  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * WebHttpBehavior.cs : now message formatters are pairs of request/
+         reply formatters.
+
 2009-09-02  Atsushi Enomoto  <atsushi@ximian.com>
 
        * WebHttpBehavior.cs : BodyStyle is operation specific.
index a14b94bdc1164fe7962da9f6fabbac588242b0fd..9fcd2629e2086b29e61422313318fd74ae2527e0 100644 (file)
@@ -83,18 +83,17 @@ namespace System.ServiceModel.Description
                        // endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add (something);
                }
 
-               [MonoTODO ("where should I set reply client formatter?")]
                public virtual void ApplyClientBehavior (ServiceEndpoint endpoint, ClientRuntime clientRuntime)
                {
                        AddClientErrorInspector (endpoint, clientRuntime);
 
                        foreach (ClientOperation oper in clientRuntime.Operations) {
-                               oper.Formatter = GetRequestClientFormatter (endpoint.Contract.Operations.Find (oper.Name), endpoint);
-                               //oper.Formatter = GetReplyClientFormatter (endpoint.Contract.Operations.Find (oper.Name), endpoint); // FIXME: see MonoTODO.
+                               var req = GetRequestClientFormatter (endpoint.Contract.Operations.Find (oper.Name), endpoint);
+                               var res = GetReplyClientFormatter (endpoint.Contract.Operations.Find (oper.Name), endpoint);
+                               oper.Formatter = new ClientPairFormatter (req, res);
                        }
                }
 
-               [MonoTODO ("where should I set reply dispatch formatter?")]
                public virtual void ApplyDispatchBehavior (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
                {
                        endpointDispatcher.DispatchRuntime.OperationSelector = GetOperationSelector (endpoint);
@@ -104,8 +103,52 @@ namespace System.ServiceModel.Description
                        AddServerErrorHandlers (endpoint, endpointDispatcher);
 
                        foreach (DispatchOperation oper in endpointDispatcher.DispatchRuntime.Operations) {
-                               oper.Formatter = GetRequestDispatchFormatter (endpoint.Contract.Operations.Find (oper.Name), endpoint);
-                               //oper.Formatter = GetReplyDispatchFormatter (endpoint.Contract.Operations.Find (oper.Name), endpoint); // FIXME: see MonoTODO.
+                               var req = GetRequestDispatchFormatter (endpoint.Contract.Operations.Find (oper.Name), endpoint);
+                               var res = GetReplyDispatchFormatter (endpoint.Contract.Operations.Find (oper.Name), endpoint);
+                               oper.Formatter = new DispatchPairFormatter (req, res);
+                       }
+               }
+
+               internal class ClientPairFormatter : IClientMessageFormatter
+               {
+                       public ClientPairFormatter (IClientMessageFormatter request, IClientMessageFormatter reply)
+                       {
+                               this.request = request;
+                               this.reply = reply;
+                       }
+
+                       IClientMessageFormatter request, reply;
+
+                       public Message SerializeRequest (MessageVersion messageVersion, object [] parameters)
+                       {
+                               return request.SerializeRequest (messageVersion, parameters);
+                       }
+
+                       public object DeserializeReply (Message message, object [] parameters)
+                       {
+                               return reply.DeserializeReply (message, parameters);
+                       }
+               }
+
+               internal class DispatchPairFormatter : IDispatchMessageFormatter
+               {
+                       public DispatchPairFormatter (IDispatchMessageFormatter request, IDispatchMessageFormatter reply)
+                       {
+                               this.request = request;
+                               this.reply = reply;
+                       }
+
+                       IDispatchMessageFormatter request;
+                       IDispatchMessageFormatter reply;
+
+                       public void DeserializeRequest (Message message, object [] parameters)
+                       {
+                               request.DeserializeRequest (message, parameters);
+                       }
+
+                       public Message SerializeReply (MessageVersion messageVersion, object [] parameters, object result)
+                       {
+                               return reply.SerializeReply (messageVersion, parameters, result);
                        }
                }
 
index 5dab157b8bd63df32dc79940ac3ebc81dcada2d2..0fc0315e9d0cfd6a60d54b47b31a0bee903359a2 100644 (file)
@@ -1,3 +1,8 @@
+2009-09-02  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * WebMessageFormatter.cs : override some serialization methods as
+         prohibited.
+
 2009-09-02  Atsushi Enomoto  <atsushi@ximian.com>
 
        * WebHttpDispatchOperationSelector.cs : use common extension method.
index 67d3c56d8eba114b91a73d36c1910af899668c3c..401ee8fdc43484b2ec003d376de95d66e2b42de9 100644 (file)
@@ -171,6 +171,11 @@ namespace System.ServiceModel.Description
                                : base (operation, endpoint, converter, behavior)
                        {
                        }
+
+                       public override object DeserializeReply (Message message, object [] parameters)
+                       {
+                               throw new NotSupportedException ();
+                       }
                }
 
                internal class ReplyClientFormatter : WebClientMessageFormatter
@@ -179,6 +184,11 @@ namespace System.ServiceModel.Description
                                : base (operation, endpoint, converter, behavior)
                        {
                        }
+
+                       public override Message SerializeRequest (MessageVersion messageVersion, object [] parameters)
+                       {
+                               throw new NotSupportedException ();
+                       }
                }
 
                internal class RequestDispatchFormatter : WebDispatchMessageFormatter
@@ -204,7 +214,7 @@ namespace System.ServiceModel.Description
                        {
                        }
 
-                       public Message SerializeRequest (MessageVersion messageVersion, object [] parameters)
+                       public virtual Message SerializeRequest (MessageVersion messageVersion, object [] parameters)
                        {
                                if (parameters == null)
                                        throw new ArgumentNullException ("parameters");
@@ -248,7 +258,7 @@ namespace System.ServiceModel.Description
                                return ret;
                        }
 
-                       public object DeserializeReply (Message message, object [] parameters)
+                       public virtual object DeserializeReply (Message message, object [] parameters)
                        {
                                if (parameters == null)
                                        throw new ArgumentNullException ("parameters");
index db1df5e6b62910a017861d8eadf80a78f1793a12..c2cd5a599d31d411f28b5b9f1998c67766a0eb0b 100644 (file)
@@ -1,3 +1,7 @@
+2009-09-02  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * WebHttpBehaviorTest.cs : some message formatters tests.
+
 2008-02-15  Atsushi Enomoto  <atsushi@ximian.com>
 
        * WebHttpBehaviorTest.cs : test that ApplyDispatchBehavior() sets
index a4f9d04a84f41f2332776ba59d626bcf431427be..6d87c87c8c441b446523dcea3cece889b624bd9e 100644 (file)
@@ -1,4 +1,5 @@
 using System;
+using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.ServiceModel.Channels;
 using System.ServiceModel.Description;
@@ -9,6 +10,38 @@ using NUnit.Framework;
 
 namespace MonoTests.System.ServiceModel.Description
 {
+       public class WebHttpBehaviorExt : WebHttpBehavior
+       {
+               public IClientMessageFormatter DoGetReplyClientFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
+               {
+                       return GetReplyClientFormatter (operationDescription, endpoint);
+               }
+
+               public IClientMessageFormatter DoGetRequestClientFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
+               {
+                       return GetRequestClientFormatter (operationDescription, endpoint);
+               }
+
+               public IDispatchMessageFormatter DoGetReplyDispatchFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
+               {
+                       return GetReplyDispatchFormatter (operationDescription, endpoint);
+               }
+
+               public IDispatchMessageFormatter DoGetRequestDispatchFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
+               {
+                       return GetRequestDispatchFormatter (operationDescription, endpoint);
+               }
+
+               public event Action<ServiceEndpoint, ClientRuntime> ApplyClientBehaviorInvoked;
+
+               public override void ApplyClientBehavior (ServiceEndpoint endpoint, ClientRuntime client)
+               {
+                       base.ApplyClientBehavior (endpoint, client);
+                       if (ApplyClientBehaviorInvoked != null)
+                               ApplyClientBehaviorInvoked (endpoint, client);
+               }
+       }
+
        [TestFixture]
        public class WebHttpBehaviorTest
        {
@@ -19,7 +52,6 @@ namespace MonoTests.System.ServiceModel.Description
                }
 
                [Test]
-               [Category("NotWorking")]
                public void AddBiningParameters ()
                {
                        var se = CreateEndpoint ();
@@ -75,6 +107,60 @@ namespace MonoTests.System.ServiceModel.Description
                        Assert.AreEqual (0, ed.DispatchRuntime.Operations.Count, "#4-0"); // hmm... really?
                }
 
+               [Test]
+               public void GetMessageFormatters ()
+               {
+                       var se = CreateEndpoint ();
+                       var od = se.Contract.Operations [0];
+                       var b = new WebHttpBehaviorExt ();
+                       Assert.IsNotNull (b.DoGetRequestClientFormatter (od, se), "#1");
+                       Assert.IsNotNull (b.DoGetReplyClientFormatter (od, se), "#2");
+                       Assert.IsNotNull (b.DoGetRequestDispatchFormatter (od, se), "#3");
+                       Assert.IsNotNull (b.DoGetReplyDispatchFormatter (od, se), "#4");
+               }
+
+               [Test]
+               public void RequestClientFormatter ()
+               {
+                       var se = CreateEndpoint ();
+                       var od = se.Contract.Operations [0];
+                       var b = new WebHttpBehaviorExt ();
+                       var rcf = b.DoGetRequestClientFormatter (od, se);
+                       var msg = rcf.SerializeRequest (MessageVersion.None, new object [] {"foo"});
+                       var hp = msg.Properties [HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
+                       Assert.IsNotNull (hp, "#1");
+                       Assert.IsTrue (msg.IsEmpty, "#2");
+                       Assert.AreEqual (String.Empty, hp.QueryString, "#3");
+                       var mb = msg.CreateBufferedCopy (1000);
+                       try {
+                               rcf.DeserializeReply (mb.CreateMessage (), new object [0]);
+                               Assert.Fail ("It should not support reply deserialization");
+                       } catch (NotSupportedException) {
+                       }
+               }
+
+               [Test]
+               public void RequestClientFormatter2 ()
+               {
+                       var se = CreateEndpoint ();
+                       var od = se.Contract.Operations [0];
+                       var b = new WebHttpBehaviorExt ();
+                       IClientMessageFormatter rcf = null;
+                       b.ApplyClientBehaviorInvoked += delegate (ServiceEndpoint e, ClientRuntime cr) {
+                               rcf = cr.Operations [0].Formatter;
+                       };
+                       se.Behaviors.Add (b);
+                       var ch = new WebChannelFactory<IMyServiceClient> (se).CreateChannel ();
+                       var msg = rcf.SerializeRequest (MessageVersion.None, new object [] {"foo"});
+                       var hp = msg.Properties [HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
+                       Assert.IsNotNull (hp, "#1");
+                       Assert.IsTrue (msg.IsEmpty, "#2");
+                       Assert.AreEqual (String.Empty, hp.QueryString, "#3");
+                       //var mb = msg.CreateBufferedCopy (1000);
+
+                       // TODO: test DeserializeReply too (it is supported unlike above).
+               }
+
                [ServiceContract]
                public interface IMyService
                {
@@ -83,6 +169,10 @@ namespace MonoTests.System.ServiceModel.Description
                        string Echo (string input);
                }
 
+               public interface IMyServiceClient : IMyService, IClientChannel
+               {
+               }
+
                public class MyService: IMyService
                {
                        [OperationBehavior]