DispatchRuntimeTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. //
  2. // DispatchRuntimeTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Runtime.Serialization;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Channels;
  33. using System.ServiceModel.Description;
  34. using System.ServiceModel.Dispatcher;
  35. using System.ServiceModel.Security;
  36. using System.Xml;
  37. using NUnit.Framework;
  38. using System.Collections.ObjectModel;
  39. using SMMessage = System.ServiceModel.Channels.Message;
  40. using System.Threading;
  41. namespace MonoTests.System.ServiceModel.Dispatcher
  42. {
  43. [TestFixture]
  44. public class DispatchRuntimeTest
  45. {
  46. [Test]
  47. public void TestConstructor ()
  48. {
  49. // This test is rather to just detect implementation
  50. // differences than digging in-depth meanings, so feel
  51. // free to change if MS implementation does not make
  52. // sense.
  53. DispatchRuntime r = new EndpointDispatcher (
  54. new EndpointAddress ("http://localhost:30158"), "IFoo", "urn:foo").DispatchRuntime;
  55. Assert.AreEqual (AuditLogLocation.Default,
  56. r.SecurityAuditLogLocation, "#1");
  57. Assert.IsTrue (r.AutomaticInputSessionShutdown, "#2");
  58. Assert.IsNotNull (r.CallbackClientRuntime, "#3");
  59. Assert.IsNull (r.ExternalAuthorizationPolicies, "#4");
  60. Assert.IsFalse (r.IgnoreTransactionMessageProperty, "#5");
  61. Assert.IsFalse (r.ImpersonateCallerForAllOperations, "#6");
  62. Assert.AreEqual (0, r.InputSessionShutdownHandlers.Count, "#7");
  63. Assert.AreEqual (0, r.InstanceContextInitializers.Count, "#8");
  64. //Assert.AreEqual (0, r.InstanceContextLifetimes.Count, "#9");
  65. Assert.IsNull (r.InstanceProvider, "#10");
  66. Assert.IsNull (r.InstanceContextProvider, "#10-2");
  67. Assert.AreEqual (AuditLevel.None,
  68. r.MessageAuthenticationAuditLevel, "#11");
  69. Assert.AreEqual (0, r.MessageInspectors.Count, "#12");
  70. Assert.AreEqual (0, r.Operations.Count, "#13");
  71. Assert.IsNull (r.OperationSelector, "#14");
  72. // This is silly, but anyways there will be similar
  73. // functionality that just represents unix "Groups".
  74. Assert.AreEqual (PrincipalPermissionMode.UseWindowsGroups,
  75. r.PrincipalPermissionMode, "#15");
  76. Assert.IsFalse (r.ReleaseServiceInstanceOnTransactionComplete, "#16");
  77. Assert.IsNull (r.RoleProvider, "#17");
  78. Assert.AreEqual (AuditLevel.None, r.ServiceAuthorizationAuditLevel, "#18");
  79. Assert.IsNull (r.ServiceAuthorizationManager, "#19");
  80. Assert.IsTrue (r.SuppressAuditFailure, "#20");
  81. Assert.IsNull (r.SynchronizationContext, "#21");
  82. Assert.IsFalse (r.TransactionAutoCompleteOnSessionClose, "#22");
  83. Assert.IsNull (r.Type, "#23");
  84. Assert.IsNotNull (r.UnhandledDispatchOperation, "#24");
  85. DispatchOperation udo = r.UnhandledDispatchOperation;
  86. Assert.AreEqual ("*", udo.Name, "#24-2");
  87. Assert.AreEqual ("*", udo.Action, "#24-3");
  88. Assert.AreEqual ("*", udo.ReplyAction, "#24-4");
  89. Assert.IsFalse (udo.IsOneWay, "#24-5");
  90. }
  91. [Test]
  92. public void TestInstanceBehavior1 ()
  93. {
  94. Result res = new Result ();
  95. MessageInspectBehavior b = new MessageInspectBehavior ();
  96. b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
  97. b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
  98. //b.instanceProvider = new MyInstanceProvider (null, res);
  99. b.msgInspect = new MyMessageInspector (res);
  100. string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , IsIdle , OperationContext , InstanceContext = Opened , NotifyIdle , OperationContext , InstanceContext = Opened , ";
  101. TestInstanceBehavior (b, expected, res, 1);
  102. Assert.IsTrue (res.Done, "done");
  103. }
  104. [Test]
  105. public void TestInstanceBehavior2 ()
  106. {
  107. Result res = new Result ();
  108. MessageInspectBehavior b = new MessageInspectBehavior ();
  109. b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
  110. b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
  111. b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
  112. b.msgInspect = new MyMessageInspector (res);
  113. string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , GetInstance1 , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ReleaseInstance , OperationContext , InstanceContext = Opened , IsIdle , OperationContext , InstanceContext = Opened , NotifyIdle , OperationContext , InstanceContext = Opened , ";
  114. TestInstanceBehavior (b, expected, res, 1);
  115. Assert.IsTrue (res.Done, "done");
  116. }
  117. [Test]
  118. public void TestInstanceBehavior3 ()
  119. {
  120. Result res = new Result ();
  121. MessageInspectBehavior b = new MessageInspectBehavior ();
  122. b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
  123. InstanceContext c = new InstanceContext (new AllActions (res));
  124. b.instanceCtxProvider = new MyInstanceContextProvider (c, res);
  125. b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
  126. b.msgInspect = new MyMessageInspector (res);
  127. string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ";
  128. TestInstanceBehavior (b, expected, res, 1); Assert.IsTrue (res.Done, "done");
  129. }
  130. [Test]
  131. public void TestInstanceBehavior4 ()
  132. {
  133. Result res = new Result ();
  134. MessageInspectBehavior b = new MessageInspectBehavior ();
  135. b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
  136. b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
  137. b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
  138. b.msgInspect = new MyMessageInspector (res);
  139. string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , GetInstance1 , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ReleaseInstance , OperationContext , InstanceContext = Opened , IsIdle , OperationContext , InstanceContext = Opened , NotifyIdle , OperationContext , InstanceContext = Opened , GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , GetInstance1 , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ReleaseInstance , OperationContext , InstanceContext = Opened , IsIdle , OperationContext , InstanceContext = Opened , NotifyIdle , OperationContext , InstanceContext = Opened , ";
  140. TestInstanceBehavior (b, expected, res, 2); Assert.IsTrue (res.Done, "done");
  141. }
  142. void TestInstanceBehavior (MessageInspectBehavior b, string expected, Result actual, int invocations)
  143. {
  144. ServiceHost h = new ServiceHost (typeof (AllActions), new Uri ("http://localhost:30158"));
  145. try {
  146. h.AddServiceEndpoint (typeof (IAllActions).FullName, new BasicHttpBinding (), "AllActions");
  147. h.Description.Behaviors.Add (b);
  148. ServiceDebugBehavior db = h.Description.Behaviors.Find<ServiceDebugBehavior> ();
  149. db.IncludeExceptionDetailInFaults = true;
  150. h.Open ();
  151. foreach (ChannelDispatcher cd in h.ChannelDispatchers) {
  152. foreach (var ed in cd.Endpoints) {
  153. if (ed.ContractName == "IHttpGetHelpPageAndMetadataContract")
  154. continue;
  155. Assert.AreEqual (typeof (AllActions), ed.DispatchRuntime.Type, "Type property: " + ed.ContractName);
  156. }
  157. }
  158. AllActionsProxy p = new AllActionsProxy (new BasicHttpBinding () { SendTimeout = TimeSpan.FromSeconds (5), ReceiveTimeout = TimeSpan.FromSeconds (5) }, new EndpointAddress ("http://localhost:30158/AllActions"));
  159. for (int i = 0; i < invocations; ++i)
  160. p.Get (10);
  161. p.Close ();
  162. //let ther server finish his work
  163. Thread.Sleep (100);
  164. Assert.AreEqual (expected, actual.string_res);
  165. actual.Done = true;
  166. }
  167. finally {
  168. h.Close ();
  169. }
  170. }
  171. }
  172. #region helpers
  173. #region message inspectors
  174. public class MessageInspectBehavior : IServiceBehavior
  175. {
  176. public MyMessageInspector msgInspect;
  177. public MyInstanceContextProvider instanceCtxProvider;
  178. public MyInstanceProvider instanceProvider;
  179. public MyInstanceContextInitializer instanceCtxInitializer;
  180. public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
  181. }
  182. public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  183. ChannelDispatcher d = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
  184. d.Endpoints [0].DispatchRuntime.MessageInspectors.Add (msgInspect);
  185. d.Endpoints [0].DispatchRuntime.InstanceContextProvider = instanceCtxProvider;
  186. d.Endpoints [0].DispatchRuntime.InstanceProvider = instanceProvider;
  187. d.Endpoints [0].DispatchRuntime.InstanceContextInitializers.Add (instanceCtxInitializer);
  188. }
  189. public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  190. }
  191. }
  192. public class MyMessageInspector : IDispatchMessageInspector
  193. {
  194. Result res;
  195. public MyMessageInspector (Result result) {
  196. res = result;
  197. }
  198. #region IDispatchMessageInspector Members
  199. public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
  200. res.string_res += "AfterReceiveRequest , ";
  201. res.AddCurrentOperationContextInfo ();
  202. return null;
  203. }
  204. public void BeforeSendReply (ref Message reply, object correlationState) {
  205. res.string_res += "BeforeSendReply , ";
  206. res.AddCurrentOperationContextInfo ();
  207. }
  208. #endregion
  209. }
  210. #endregion
  211. #region InstanceProvider
  212. public class MyInstanceProvider : IInstanceProvider
  213. {
  214. object instance;
  215. Result res;
  216. public MyInstanceProvider (object obj, Result result) {
  217. instance = obj;
  218. res = result;
  219. }
  220. #region IInstanceProvider Members
  221. public object GetInstance (InstanceContext instanceContext, Message message) {
  222. res.string_res += "GetInstance1 , ";
  223. res.AddCurrentOperationContextInfo ();
  224. return instance;
  225. }
  226. public object GetInstance (InstanceContext instanceContext) {
  227. res.string_res += "GetInstance2 , ";
  228. res.AddCurrentOperationContextInfo ();
  229. return instance;
  230. }
  231. public void ReleaseInstance (InstanceContext instanceContext, object instance) {
  232. res.string_res += "ReleaseInstance , ";
  233. res.AddCurrentOperationContextInfo ();
  234. }
  235. #endregion
  236. }
  237. #endregion
  238. #region InstanceContextProvider
  239. public class MyInstanceContextProvider : IInstanceContextProvider
  240. {
  241. InstanceContext existing;
  242. Result res;
  243. public MyInstanceContextProvider (InstanceContext exist, Result result) {
  244. existing = exist;
  245. res = result;
  246. }
  247. #region IInstanceContextProvider Members
  248. public InstanceContext GetExistingInstanceContext (Message message, IContextChannel channel) {
  249. res.string_res += "GetExistingInstanceContext , ";
  250. res.AddCurrentOperationContextInfo ();
  251. return existing;
  252. }
  253. public void InitializeInstanceContext (InstanceContext instanceContext, Message message, IContextChannel channel) {
  254. res.string_res += "InitializeInstanceContext , ";
  255. res.AddCurrentOperationContextInfo ();
  256. }
  257. public bool IsIdle (InstanceContext instanceContext) {
  258. res.string_res += "IsIdle , ";
  259. res.AddCurrentOperationContextInfo ();
  260. return false;
  261. }
  262. public void NotifyIdle (InstanceContextIdleCallback callback, InstanceContext instanceContext) {
  263. res.string_res += "NotifyIdle , ";
  264. res.AddCurrentOperationContextInfo ();
  265. }
  266. #endregion
  267. }
  268. #endregion
  269. #region InstanceContextInitializer
  270. public class MyInstanceContextInitializer : IInstanceContextInitializer
  271. {
  272. Result res;
  273. public MyInstanceContextInitializer (Result result) {
  274. res = result;
  275. }
  276. public void Initialize (InstanceContext instanceContext, Message message) {
  277. res.string_res += "Initialize , ";
  278. res.AddCurrentOperationContextInfo ();
  279. }
  280. }
  281. #endregion
  282. #region Helpers
  283. public class Result
  284. {
  285. public bool Done;
  286. public string string_res = "";
  287. public void AddCurrentOperationContextInfo()
  288. {
  289. if (OperationContext.Current != null) {
  290. string_res += "OperationContext , ";
  291. if (OperationContext.Current.InstanceContext != null) {
  292. string_res += ("InstanceContext = " + OperationContext.Current.InstanceContext.State + " , ");
  293. //if (OperationContext.Current.InstanceContext != null)
  294. // string_res += ("Instance = " + OperationContext.Current.InstanceContext.GetServiceInstance () + " , ");
  295. }
  296. }
  297. }
  298. }
  299. class AllActions : IAllActions, IDisposable
  300. {
  301. Result res;
  302. public AllActions () { }
  303. public AllActions (Result result) {
  304. res = result;
  305. }
  306. [OperationBehavior (ReleaseInstanceMode = ReleaseInstanceMode.BeforeAndAfterCall)]
  307. public int Get(int i)
  308. {
  309. return i;
  310. }
  311. public void Dispose () {
  312. if (res != null)
  313. res.string_res += "Disposed , ";
  314. }
  315. }
  316. [ServiceContract (Namespace = "http://MonoTests.System.ServiceModel.Dispatcher")]
  317. public interface IAllActions
  318. {
  319. [OperationContract]
  320. int Get(int i);
  321. }
  322. #endregion
  323. #region ClientProxy
  324. public class AllActionsProxy : ClientBase<IAllActions>, IAllActions
  325. {
  326. public AllActionsProxy (Binding binding, EndpointAddress remoteAddress) :
  327. base (binding, remoteAddress)
  328. {
  329. }
  330. public int Get (int i) {
  331. return base.Channel.Get (i);
  332. }
  333. }
  334. #endregion
  335. #endregion
  336. }