Bug652331Test.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // Authors:
  3. // David Straw
  4. // Atsushi Enomoto <[email protected]>
  5. //
  6. // Copyright (C) 2011 Novell, Inc. http://www.novell.com
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using System.Runtime.Serialization;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Description;
  33. using System.Threading;
  34. using NUnit.Framework;
  35. using WebServiceMoonlightTest.ServiceReference1;
  36. using MonoTests.Helpers;
  37. namespace MonoTests.System.ServiceModel.Dispatcher
  38. {
  39. [TestFixture]
  40. public class Bug652331Test
  41. {
  42. [Test]
  43. public void Bug652331_2 () // test in one of the comment
  44. {
  45. // Init service
  46. int port = NetworkHelpers.FindFreePort ();
  47. ServiceHost serviceHost = new ServiceHost (typeof (Service1), new Uri ("http://localhost:" + port + "/Service1"));
  48. serviceHost.AddServiceEndpoint (typeof (IService1), new BasicHttpBinding (), string.Empty);
  49. // Enable metadata exchange (WSDL publishing)
  50. var mexBehavior = new ServiceMetadataBehavior ();
  51. mexBehavior.HttpGetEnabled = true;
  52. serviceHost.Description.Behaviors.Add (mexBehavior);
  53. serviceHost.AddServiceEndpoint (typeof (IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding (), "mex");
  54. serviceHost.Open ();
  55. try {
  56. // client
  57. var binding = new BasicHttpBinding ();
  58. var remoteAddress = new EndpointAddress ("http://localhost:" + port + "/Service1");
  59. var client = new Service1Client (binding, remoteAddress);
  60. var wait = new ManualResetEvent (false);
  61. client.GetDataCompleted += delegate (object o, GetDataCompletedEventArgs e) {
  62. if (e.Error != null)
  63. throw e.Error;
  64. Assert.AreEqual ("A", ((DataType1) e.Result).Id, "#1");
  65. wait.Set ();
  66. };
  67. client.GetDataAsync ();
  68. if (!wait.WaitOne (TimeSpan.FromSeconds (20)))
  69. Assert.Fail ("timeout");
  70. } finally {
  71. serviceHost.Close ();
  72. }
  73. }
  74. public class Service1 : IService1
  75. {
  76. public object GetData ()
  77. {
  78. return new DataType1 { Id = "A" };
  79. }
  80. Func<object> d;
  81. public IAsyncResult BeginGetData (AsyncCallback callback, object state)
  82. {
  83. if (d == null)
  84. d = new Func<object> (GetData);
  85. return d.BeginInvoke (callback, state);
  86. }
  87. public object EndGetData (IAsyncResult result)
  88. {
  89. return d.EndInvoke (result);
  90. }
  91. }
  92. }
  93. }
  94. // below are part of autogenerated code in comment #1 on bug #652331.
  95. namespace WebServiceMoonlightTest.ServiceReference1 {
  96. [System.Diagnostics.DebuggerStepThroughAttribute()]
  97. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
  98. [System.Runtime.Serialization.DataContractAttribute(Name="DataType1", Namespace="http://mynamespace")]
  99. public partial class DataType1 : object, System.ComponentModel.INotifyPropertyChanged {
  100. private string IdField;
  101. [System.Runtime.Serialization.DataMemberAttribute()]
  102. public string Id {
  103. get {
  104. return this.IdField;
  105. }
  106. set {
  107. if ((object.ReferenceEquals(this.IdField, value) != true)) {
  108. this.IdField = value;
  109. this.RaisePropertyChanged("Id");
  110. }
  111. }
  112. }
  113. public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
  114. protected void RaisePropertyChanged(string propertyName) {
  115. System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
  116. if ((propertyChanged != null)) {
  117. propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
  118. }
  119. }
  120. }
  121. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  122. [System.ServiceModel.ServiceContractAttribute(Namespace="http://mynamespace", ConfigurationName="ServiceReference1.IService1")]
  123. [ServiceKnownType (typeof (DataType1))]
  124. public interface IService1 {
  125. [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://mynamespace/IService1/GetData", ReplyAction="http://mynamespace/IService1/GetDataResponse")]
  126. [System.ServiceModel.ServiceKnownTypeAttribute(typeof(WebServiceMoonlightTest.ServiceReference1.DataType1))]
  127. System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState);
  128. object EndGetData(System.IAsyncResult result);
  129. }
  130. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  131. public interface IService1Channel : WebServiceMoonlightTest.ServiceReference1.IService1, System.ServiceModel.IClientChannel {
  132. }
  133. [System.Diagnostics.DebuggerStepThroughAttribute()]
  134. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  135. public partial class GetDataCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
  136. private object[] results;
  137. public GetDataCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
  138. base(exception, cancelled, userState) {
  139. this.results = results;
  140. }
  141. public object Result {
  142. get {
  143. base.RaiseExceptionIfNecessary();
  144. return ((object)(this.results[0]));
  145. }
  146. }
  147. }
  148. [System.Diagnostics.DebuggerStepThroughAttribute()]
  149. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  150. public partial class Service1Client : System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
  151. private BeginOperationDelegate onBeginGetDataDelegate;
  152. private EndOperationDelegate onEndGetDataDelegate;
  153. private System.Threading.SendOrPostCallback onGetDataCompletedDelegate;
  154. private BeginOperationDelegate onBeginOpenDelegate;
  155. private EndOperationDelegate onEndOpenDelegate;
  156. private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
  157. private BeginOperationDelegate onBeginCloseDelegate;
  158. private EndOperationDelegate onEndCloseDelegate;
  159. private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
  160. public Service1Client() {
  161. }
  162. public Service1Client(string endpointConfigurationName) :
  163. base(endpointConfigurationName) {
  164. }
  165. public Service1Client(string endpointConfigurationName, string remoteAddress) :
  166. base(endpointConfigurationName, remoteAddress) {
  167. }
  168. public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
  169. base(endpointConfigurationName, remoteAddress) {
  170. }
  171. public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
  172. base(binding, remoteAddress) {
  173. }
  174. /*
  175. public System.Net.CookieContainer CookieContainer {
  176. get {
  177. System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
  178. if ((httpCookieContainerManager != null)) {
  179. return httpCookieContainerManager.CookieContainer;
  180. }
  181. else {
  182. return null;
  183. }
  184. }
  185. set {
  186. System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
  187. if ((httpCookieContainerManager != null)) {
  188. httpCookieContainerManager.CookieContainer = value;
  189. }
  190. else {
  191. throw new System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
  192. "ookieContainerBindingElement.");
  193. }
  194. }
  195. }
  196. */
  197. public event System.EventHandler<GetDataCompletedEventArgs> GetDataCompleted;
  198. public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
  199. public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
  200. [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
  201. System.IAsyncResult WebServiceMoonlightTest.ServiceReference1.IService1.BeginGetData(System.AsyncCallback callback, object asyncState) {
  202. return base.Channel.BeginGetData(callback, asyncState);
  203. }
  204. [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
  205. object WebServiceMoonlightTest.ServiceReference1.IService1.EndGetData(System.IAsyncResult result) {
  206. return base.Channel.EndGetData(result);
  207. }
  208. private System.IAsyncResult OnBeginGetData(object[] inValues, System.AsyncCallback callback, object asyncState) {
  209. return ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).BeginGetData(callback, asyncState);
  210. }
  211. private object[] OnEndGetData(System.IAsyncResult result) {
  212. object retVal = ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).EndGetData(result);
  213. return new object[] {
  214. retVal};
  215. }
  216. private void OnGetDataCompleted(object state) {
  217. if ((this.GetDataCompleted != null)) {
  218. InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
  219. this.GetDataCompleted(this, new GetDataCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
  220. }
  221. }
  222. public void GetDataAsync() {
  223. this.GetDataAsync(null);
  224. }
  225. public void GetDataAsync(object userState) {
  226. if ((this.onBeginGetDataDelegate == null)) {
  227. this.onBeginGetDataDelegate = new BeginOperationDelegate(this.OnBeginGetData);
  228. }
  229. if ((this.onEndGetDataDelegate == null)) {
  230. this.onEndGetDataDelegate = new EndOperationDelegate(this.OnEndGetData);
  231. }
  232. if ((this.onGetDataCompletedDelegate == null)) {
  233. this.onGetDataCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetDataCompleted);
  234. }
  235. base.InvokeAsync(this.onBeginGetDataDelegate, null, this.onEndGetDataDelegate, this.onGetDataCompletedDelegate, userState);
  236. }
  237. private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) {
  238. return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
  239. }
  240. private object[] OnEndOpen(System.IAsyncResult result) {
  241. ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);
  242. return null;
  243. }
  244. private void OnOpenCompleted(object state) {
  245. if ((this.OpenCompleted != null)) {
  246. InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
  247. this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
  248. }
  249. }
  250. public void OpenAsync() {
  251. this.OpenAsync(null);
  252. }
  253. public void OpenAsync(object userState) {
  254. if ((this.onBeginOpenDelegate == null)) {
  255. this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
  256. }
  257. if ((this.onEndOpenDelegate == null)) {
  258. this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
  259. }
  260. if ((this.onOpenCompletedDelegate == null)) {
  261. this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
  262. }
  263. base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
  264. }
  265. private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) {
  266. return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
  267. }
  268. private object[] OnEndClose(System.IAsyncResult result) {
  269. ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);
  270. return null;
  271. }
  272. private void OnCloseCompleted(object state) {
  273. if ((this.CloseCompleted != null)) {
  274. InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
  275. this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
  276. }
  277. }
  278. public void CloseAsync() {
  279. this.CloseAsync(null);
  280. }
  281. public void CloseAsync(object userState) {
  282. if ((this.onBeginCloseDelegate == null)) {
  283. this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
  284. }
  285. if ((this.onEndCloseDelegate == null)) {
  286. this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
  287. }
  288. if ((this.onCloseCompletedDelegate == null)) {
  289. this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
  290. }
  291. base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
  292. }
  293. /*
  294. protected override WebServiceMoonlightTest.ServiceReference1.IService1 CreateChannel() {
  295. return new Service1ClientChannel(this);
  296. }
  297. private class Service1ClientChannel : ChannelBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
  298. public Service1ClientChannel(System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1> client) :
  299. base(client) {
  300. }
  301. public System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState) {
  302. object[] _args = new object[0];
  303. System.IAsyncResult _result = base.BeginInvoke("GetData", _args, callback, asyncState);
  304. return _result;
  305. }
  306. public object EndGetData(System.IAsyncResult result) {
  307. object[] _args = new object[0];
  308. object _result = ((object)(base.EndInvoke("GetData", _args, result)));
  309. return _result;
  310. }
  311. }
  312. */
  313. }
  314. }