2
0

DispatchRuntimeTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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:8080"), "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.AreEqual (AuditLevel.None,
  67. r.MessageAuthenticationAuditLevel, "#11");
  68. Assert.AreEqual (0, r.MessageInspectors.Count, "#12");
  69. Assert.AreEqual (0, r.Operations.Count, "#13");
  70. Assert.IsNull (r.OperationSelector, "#14");
  71. // This is silly, but anyways there will be similar
  72. // functionality that just represents unix "Groups".
  73. Assert.AreEqual (PrincipalPermissionMode.UseWindowsGroups,
  74. r.PrincipalPermissionMode, "#15");
  75. Assert.IsFalse (r.ReleaseServiceInstanceOnTransactionComplete, "#16");
  76. Assert.IsNull (r.RoleProvider, "#17");
  77. Assert.AreEqual (AuditLevel.None, r.ServiceAuthorizationAuditLevel, "#18");
  78. Assert.IsNull (r.ServiceAuthorizationManager, "#19");
  79. Assert.IsTrue (r.SuppressAuditFailure, "#20");
  80. Assert.IsNull (r.SynchronizationContext, "#21");
  81. Assert.IsFalse (r.TransactionAutoCompleteOnSessionClose, "#22");
  82. Assert.IsNull (r.Type, "#23");
  83. Assert.IsNotNull (r.UnhandledDispatchOperation, "#24");
  84. DispatchOperation udo = r.UnhandledDispatchOperation;
  85. Assert.AreEqual ("*", udo.Name, "#24-2");
  86. Assert.AreEqual ("*", udo.Action, "#24-3");
  87. Assert.AreEqual ("*", udo.ReplyAction, "#24-4");
  88. Assert.IsFalse (udo.IsOneWay, "#24-5");
  89. }
  90. [Test]
  91. public void TestInstanceBehavior1()
  92. {
  93. Result res = new Result ();
  94. MessageInspectBehavior b = new MessageInspectBehavior ();
  95. b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
  96. b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
  97. //b.instanceProvider = new MyInstanceProvider (null, res);
  98. b.msgInspect = new MyMessageInspector (res);
  99. 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 , ";
  100. TestInstanceBehavior (b, expected, res, 1);
  101. }
  102. [Test]
  103. public void TestInstanceBehavior2 () {
  104. Result res = new Result ();
  105. MessageInspectBehavior b = new MessageInspectBehavior ();
  106. b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
  107. b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
  108. b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
  109. b.msgInspect = new MyMessageInspector (res);
  110. 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 , ";
  111. TestInstanceBehavior (b, expected, res, 1);
  112. }
  113. [Test]
  114. public void TestInstanceBehavior3 () {
  115. Result res = new Result ();
  116. MessageInspectBehavior b = new MessageInspectBehavior ();
  117. b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
  118. InstanceContext c = new InstanceContext (new AllActions (res));
  119. b.instanceCtxProvider = new MyInstanceContextProvider (c, res);
  120. b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
  121. b.msgInspect = new MyMessageInspector (res);
  122. string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ";
  123. TestInstanceBehavior (b, expected, res, 1);
  124. }
  125. [Test]
  126. public void TestInstanceBehavior4 () {
  127. Result res = new Result ();
  128. MessageInspectBehavior b = new MessageInspectBehavior ();
  129. b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
  130. b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
  131. b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
  132. b.msgInspect = new MyMessageInspector (res);
  133. 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 , ";
  134. TestInstanceBehavior (b, expected, res, 2);
  135. }
  136. void TestInstanceBehavior (MessageInspectBehavior b, string expected, Result actual, int invocations) {
  137. ServiceHost h = new ServiceHost (typeof (AllActions), new Uri ("http://localhost:8080"));
  138. try {
  139. h.AddServiceEndpoint (typeof (IAllActions).FullName, new BasicHttpBinding (), "AllActions");
  140. h.Description.Behaviors.Add (b);
  141. ServiceDebugBehavior db = h.Description.Behaviors.Find<ServiceDebugBehavior> ();
  142. db.IncludeExceptionDetailInFaults = true;
  143. h.Open ();
  144. AllActionsProxy p = new AllActionsProxy (new BasicHttpBinding (), new EndpointAddress ("http://localhost:8080/AllActions"));
  145. for (int i = 0; i < invocations; ++i)
  146. p.Get (10);
  147. //let ther server finish his work
  148. Thread.Sleep (100);
  149. Assert.AreEqual (expected, actual.string_res);
  150. }
  151. finally {
  152. h.Close ();
  153. }
  154. }
  155. }
  156. #region helpers
  157. #region message inspectors
  158. public class MessageInspectBehavior : IServiceBehavior
  159. {
  160. public MyMessageInspector msgInspect;
  161. public MyInstanceContextProvider instanceCtxProvider;
  162. public MyInstanceProvider instanceProvider;
  163. public MyInstanceContextInitializer instanceCtxInitializer;
  164. public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
  165. }
  166. public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  167. ChannelDispatcher d = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
  168. d.Endpoints [0].DispatchRuntime.MessageInspectors.Add (msgInspect);
  169. d.Endpoints [0].DispatchRuntime.InstanceContextProvider = instanceCtxProvider;
  170. d.Endpoints [0].DispatchRuntime.InstanceProvider = instanceProvider;
  171. d.Endpoints [0].DispatchRuntime.InstanceContextInitializers.Add (instanceCtxInitializer);
  172. }
  173. public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  174. }
  175. }
  176. public class MyMessageInspector : IDispatchMessageInspector
  177. {
  178. Result res;
  179. public MyMessageInspector (Result result) {
  180. res = result;
  181. }
  182. #region IDispatchMessageInspector Members
  183. public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
  184. res.string_res += "AfterReceiveRequest , ";
  185. res.AddCurrentOperationContextInfo ();
  186. return null;
  187. }
  188. public void BeforeSendReply (ref Message reply, object correlationState) {
  189. res.string_res += "BeforeSendReply , ";
  190. res.AddCurrentOperationContextInfo ();
  191. }
  192. #endregion
  193. }
  194. #endregion
  195. #region InstanceProvider
  196. public class MyInstanceProvider : IInstanceProvider
  197. {
  198. object instance;
  199. Result res;
  200. public MyInstanceProvider (object obj, Result result) {
  201. instance = obj;
  202. res = result;
  203. }
  204. #region IInstanceProvider Members
  205. public object GetInstance (InstanceContext instanceContext, Message message) {
  206. res.string_res += "GetInstance1 , ";
  207. res.AddCurrentOperationContextInfo ();
  208. return instance;
  209. }
  210. public object GetInstance (InstanceContext instanceContext) {
  211. res.string_res += "GetInstance2 , ";
  212. res.AddCurrentOperationContextInfo ();
  213. return instance;
  214. }
  215. public void ReleaseInstance (InstanceContext instanceContext, object instance) {
  216. res.string_res += "ReleaseInstance , ";
  217. res.AddCurrentOperationContextInfo ();
  218. }
  219. #endregion
  220. }
  221. #endregion
  222. #region InstanceContextProvider
  223. public class MyInstanceContextProvider : IInstanceContextProvider
  224. {
  225. InstanceContext existing;
  226. Result res;
  227. public MyInstanceContextProvider (InstanceContext exist, Result result) {
  228. existing = exist;
  229. res = result;
  230. }
  231. #region IInstanceContextProvider Members
  232. public InstanceContext GetExistingInstanceContext (Message message, IContextChannel channel) {
  233. res.string_res += "GetExistingInstanceContext , ";
  234. res.AddCurrentOperationContextInfo ();
  235. return existing;
  236. }
  237. public void InitializeInstanceContext (InstanceContext instanceContext, Message message, IContextChannel channel) {
  238. res.string_res += "InitializeInstanceContext , ";
  239. res.AddCurrentOperationContextInfo ();
  240. }
  241. public bool IsIdle (InstanceContext instanceContext) {
  242. res.string_res += "IsIdle , ";
  243. res.AddCurrentOperationContextInfo ();
  244. return false;
  245. }
  246. public void NotifyIdle (InstanceContextIdleCallback callback, InstanceContext instanceContext) {
  247. res.string_res += "NotifyIdle , ";
  248. res.AddCurrentOperationContextInfo ();
  249. }
  250. #endregion
  251. }
  252. #endregion
  253. #region InstanceContextInitializer
  254. public class MyInstanceContextInitializer : IInstanceContextInitializer
  255. {
  256. Result res;
  257. public MyInstanceContextInitializer (Result result) {
  258. res = result;
  259. }
  260. public void Initialize (InstanceContext instanceContext, Message message) {
  261. res.string_res += "Initialize , ";
  262. res.AddCurrentOperationContextInfo ();
  263. }
  264. }
  265. #endregion
  266. #region Helpers
  267. public class Result
  268. {
  269. public string string_res = "";
  270. public void AddCurrentOperationContextInfo()
  271. {
  272. if (OperationContext.Current != null) {
  273. string_res += "OperationContext , ";
  274. if (OperationContext.Current.InstanceContext != null) {
  275. string_res += ("InstanceContext = " + OperationContext.Current.InstanceContext.State + " , ");
  276. //if (OperationContext.Current.InstanceContext != null)
  277. // string_res += ("Instance = " + OperationContext.Current.InstanceContext.GetServiceInstance () + " , ");
  278. }
  279. }
  280. }
  281. }
  282. class AllActions : IAllActions, IDisposable
  283. {
  284. Result res;
  285. public AllActions () { }
  286. public AllActions (Result result) {
  287. res = result;
  288. }
  289. [OperationBehavior (ReleaseInstanceMode = ReleaseInstanceMode.BeforeAndAfterCall)]
  290. public int Get(int i)
  291. {
  292. return i;
  293. }
  294. public void Dispose () {
  295. if (res != null)
  296. res.string_res += "Disposed , ";
  297. }
  298. }
  299. [ServiceContract (Namespace = "http://MonoTests.System.ServiceModel.Dispatcher")]
  300. public interface IAllActions
  301. {
  302. [OperationContract]
  303. int Get(int i);
  304. }
  305. #endregion
  306. #region ClientProxy
  307. public class AllActionsProxy : ClientBase<IAllActions>, IAllActions
  308. {
  309. public AllActionsProxy (Binding binding, EndpointAddress remoteAddress) :
  310. base (binding, remoteAddress)
  311. {
  312. }
  313. public int Get (int i) {
  314. return base.Channel.Get (i);
  315. }
  316. }
  317. #endregion
  318. #endregion
  319. }