2 // DispatchOperationTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Dispatcher;
36 using System.ServiceModel.Security;
38 using NUnit.Framework;
40 using MonoTests.Helpers;
42 namespace MonoTests.System.ServiceModel.Dispatcher
45 public class DispatchOperationTest
48 [ExpectedException (typeof (ArgumentNullException))]
49 public void TestConstructorNullName ()
51 new DispatchOperation (CreateRuntime (), null, null);
55 public void TestConstructorNullAction ()
57 DispatchRuntime r = CreateRuntime ();
58 // null Action is allowed.
59 new DispatchOperation (r, String.Empty, null);
60 // null ReplyAction as well.
61 new DispatchOperation (r, String.Empty, null, null);
65 public void TestConstructor ()
67 DispatchOperation o = new DispatchOperation (
68 CreateRuntime (), String.Empty, null, null);
69 Assert.IsTrue (o.DeserializeRequest, "#1");
70 Assert.IsTrue (o.SerializeReply, "#2");
71 Assert.IsNull (o.Formatter, "#3");
72 Assert.AreEqual (0, o.FaultContractInfos.Count, "#4");
73 Assert.IsNull (o.Invoker, "#5");
74 Assert.IsFalse (o.IsOneWay, "#6");
75 Assert.IsFalse (o.IsTerminating, "#7");
76 Assert.IsFalse (o.ReleaseInstanceBeforeCall, "#8");
77 Assert.IsFalse (o.ReleaseInstanceAfterCall, "#9");
78 Assert.IsFalse (o.TransactionAutoComplete, "#10");
79 Assert.IsFalse (o.TransactionRequired, "#11");
80 Assert.AreEqual (0, o.ParameterInspectors.Count, "#12");
83 DispatchRuntime CreateRuntime ()
85 return new EndpointDispatcher (
86 new EndpointAddress ("http://localhost:8080"), "IFoo", "urn:foo").DispatchRuntime;
90 public void FaultContractInfos ()
92 var host = new ServiceHost (typeof (TestFaultContract));
93 int port = NetworkHelpers.FindFreePort ();
94 host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = false;
95 host.AddServiceEndpoint (typeof (ITestFaultContract), new BasicHttpBinding (), new Uri ("http://localhost:" + port));
98 var cf = new ChannelFactory<ITestFaultContract> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:" + port));
99 var cli = cf.CreateChannel ();
103 } catch (FaultException<PrivateAffairError> ex) {
105 Assert.AreEqual (5, p.ErrorCode, "#2");
106 Assert.AreEqual ("foobarerror", p.Text, "#3");
110 cli.Run ("deriveddata");
112 } catch (Exception ex) {
113 // The type must be explicitly listed in the [FaultContract],
114 // it is not allowed to use a subclass of the exception data type.
115 Assert.AreEqual (typeof (FaultException), ex.GetType (), "#5");
119 cli.Run ("derivedexception");
121 } catch (Exception ex) {
122 // However, it is allowed to derive from FaultException<T>, provided
123 // that T is explicitly listed in [FaultContract]. Bug #7177.
124 Assert.AreEqual (typeof (FaultException<PrivateAffairError>), ex.GetType (), "#7");
132 public interface ITestFaultContract
135 [FaultContract (typeof (PrivateAffairError), Action = "urn:myfault")]
136 string Run (string input);
139 class TestFaultContract : ITestFaultContract
141 public string Run (string input)
143 Assert.AreEqual (1, ContractDescription.GetContract (typeof (TestFaultContract)).Operations [0].Faults.Count, "s#0");
145 var dr = OperationContext.Current.EndpointDispatcher.DispatchRuntime;
146 Assert.AreEqual (1, dr.Operations.Count);
147 var dop = dr.Operations [0];
148 Assert.AreEqual ("Run", dop.Name, "s#1");
149 Assert.AreEqual (1, dop.FaultContractInfos.Count, "s#2");
150 var fci = dop.FaultContractInfos [0];
151 Assert.AreEqual (typeof (PrivateAffairError), fci.Detail, "s#3");
152 if (input.Equals ("default"))
153 throw new FaultException<PrivateAffairError> (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
154 else if (input.Equals ("deriveddata"))
155 throw new FaultException<DerivedPrivateAffairError> (new DerivedPrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
156 else if (input.Equals ("derivedexception"))
157 throw new DerivedFaultException (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
159 throw new FaultException ("Invalid operation");
164 class PrivateAffairError
167 public int ErrorCode { get; set; }
169 public string Text { get; set; }
173 class DerivedPrivateAffairError : PrivateAffairError
177 class DerivedFaultException : FaultException<PrivateAffairError>
179 public DerivedFaultException (PrivateAffairError error)