Bug652331Test.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. Thread.Sleep (2000); // let WCF spin up
  56. try {
  57. // client
  58. var binding = new BasicHttpBinding ();
  59. var remoteAddress = new EndpointAddress ("http://localhost:" + port + "/Service1");
  60. var client = new Service1Client (binding, remoteAddress);
  61. var wait = new ManualResetEvent (false);
  62. Exception error = null;
  63. object result = null;
  64. client.GetDataCompleted += delegate (object o, GetDataCompletedEventArgs e) {
  65. try {
  66. error = e.Error;
  67. result = e.Error == null ? e.Result : null;
  68. } finally {
  69. wait.Set ();
  70. }
  71. };
  72. client.GetDataAsync ();
  73. Assert.IsTrue (wait.WaitOne (TimeSpan.FromSeconds (20)), "timeout");
  74. Assert.IsNull (error, "#1, inner exception: {0}", error);
  75. Assert.AreEqual ("A", ((DataType1) result).Id, "#2");
  76. } finally {
  77. serviceHost.Close ();
  78. }
  79. }
  80. public class Service1 : IService1
  81. {
  82. public object GetData ()
  83. {
  84. return new DataType1 { Id = "A" };
  85. }
  86. Func<object> d;
  87. public IAsyncResult BeginGetData (AsyncCallback callback, object state)
  88. {
  89. if (d == null)
  90. d = new Func<object> (GetData);
  91. return d.BeginInvoke (callback, state);
  92. }
  93. public object EndGetData (IAsyncResult result)
  94. {
  95. return d.EndInvoke (result);
  96. }
  97. }
  98. }
  99. }
  100. // below are part of autogenerated code in comment #1 on bug #652331.
  101. namespace WebServiceMoonlightTest.ServiceReference1 {
  102. [System.Diagnostics.DebuggerStepThroughAttribute()]
  103. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
  104. [System.Runtime.Serialization.DataContractAttribute(Name="DataType1", Namespace="http://mynamespace")]
  105. public partial class DataType1 : object, System.ComponentModel.INotifyPropertyChanged {
  106. private string IdField;
  107. [System.Runtime.Serialization.DataMemberAttribute()]
  108. public string Id {
  109. get {
  110. return this.IdField;
  111. }
  112. set {
  113. if ((object.ReferenceEquals(this.IdField, value) != true)) {
  114. this.IdField = value;
  115. this.RaisePropertyChanged("Id");
  116. }
  117. }
  118. }
  119. public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
  120. protected void RaisePropertyChanged(string propertyName) {
  121. System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
  122. if ((propertyChanged != null)) {
  123. propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
  124. }
  125. }
  126. }
  127. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  128. [System.ServiceModel.ServiceContractAttribute(Namespace="http://mynamespace", ConfigurationName="ServiceReference1.IService1")]
  129. [ServiceKnownType (typeof (DataType1))]
  130. public interface IService1 {
  131. [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://mynamespace/IService1/GetData", ReplyAction="http://mynamespace/IService1/GetDataResponse")]
  132. [System.ServiceModel.ServiceKnownTypeAttribute(typeof(WebServiceMoonlightTest.ServiceReference1.DataType1))]
  133. System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState);
  134. object EndGetData(System.IAsyncResult result);
  135. }
  136. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  137. public interface IService1Channel : WebServiceMoonlightTest.ServiceReference1.IService1, System.ServiceModel.IClientChannel {
  138. }
  139. [System.Diagnostics.DebuggerStepThroughAttribute()]
  140. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  141. public partial class GetDataCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
  142. private object[] results;
  143. public GetDataCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
  144. base(exception, cancelled, userState) {
  145. this.results = results;
  146. }
  147. public object Result {
  148. get {
  149. base.RaiseExceptionIfNecessary();
  150. return ((object)(this.results[0]));
  151. }
  152. }
  153. }
  154. [System.Diagnostics.DebuggerStepThroughAttribute()]
  155. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  156. public partial class Service1Client : System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
  157. private BeginOperationDelegate onBeginGetDataDelegate;
  158. private EndOperationDelegate onEndGetDataDelegate;
  159. private System.Threading.SendOrPostCallback onGetDataCompletedDelegate;
  160. private BeginOperationDelegate onBeginOpenDelegate;
  161. private EndOperationDelegate onEndOpenDelegate;
  162. private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
  163. private BeginOperationDelegate onBeginCloseDelegate;
  164. private EndOperationDelegate onEndCloseDelegate;
  165. private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
  166. public Service1Client() {
  167. }
  168. public Service1Client(string endpointConfigurationName) :
  169. base(endpointConfigurationName) {
  170. }
  171. public Service1Client(string endpointConfigurationName, string remoteAddress) :
  172. base(endpointConfigurationName, remoteAddress) {
  173. }
  174. public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
  175. base(endpointConfigurationName, remoteAddress) {
  176. }
  177. public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
  178. base(binding, remoteAddress) {
  179. }
  180. /*
  181. public System.Net.CookieContainer CookieContainer {
  182. get {
  183. System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
  184. if ((httpCookieContainerManager != null)) {
  185. return httpCookieContainerManager.CookieContainer;
  186. }
  187. else {
  188. return null;
  189. }
  190. }
  191. set {
  192. System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
  193. if ((httpCookieContainerManager != null)) {
  194. httpCookieContainerManager.CookieContainer = value;
  195. }
  196. else {
  197. throw new System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
  198. "ookieContainerBindingElement.");
  199. }
  200. }
  201. }
  202. */
  203. public event System.EventHandler<GetDataCompletedEventArgs> GetDataCompleted;
  204. public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
  205. public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
  206. [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
  207. System.IAsyncResult WebServiceMoonlightTest.ServiceReference1.IService1.BeginGetData(System.AsyncCallback callback, object asyncState) {
  208. return base.Channel.BeginGetData(callback, asyncState);
  209. }
  210. [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
  211. object WebServiceMoonlightTest.ServiceReference1.IService1.EndGetData(System.IAsyncResult result) {
  212. return base.Channel.EndGetData(result);
  213. }
  214. private System.IAsyncResult OnBeginGetData(object[] inValues, System.AsyncCallback callback, object asyncState) {
  215. return ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).BeginGetData(callback, asyncState);
  216. }
  217. private object[] OnEndGetData(System.IAsyncResult result) {
  218. object retVal = ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).EndGetData(result);
  219. return new object[] {
  220. retVal};
  221. }
  222. private void OnGetDataCompleted(object state) {
  223. if ((this.GetDataCompleted != null)) {
  224. InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
  225. this.GetDataCompleted(this, new GetDataCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
  226. }
  227. }
  228. public void GetDataAsync() {
  229. this.GetDataAsync(null);
  230. }
  231. public void GetDataAsync(object userState) {
  232. if ((this.onBeginGetDataDelegate == null)) {
  233. this.onBeginGetDataDelegate = new BeginOperationDelegate(this.OnBeginGetData);
  234. }
  235. if ((this.onEndGetDataDelegate == null)) {
  236. this.onEndGetDataDelegate = new EndOperationDelegate(this.OnEndGetData);
  237. }
  238. if ((this.onGetDataCompletedDelegate == null)) {
  239. this.onGetDataCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetDataCompleted);
  240. }
  241. base.InvokeAsync(this.onBeginGetDataDelegate, null, this.onEndGetDataDelegate, this.onGetDataCompletedDelegate, userState);
  242. }
  243. private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) {
  244. return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
  245. }
  246. private object[] OnEndOpen(System.IAsyncResult result) {
  247. ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);
  248. return null;
  249. }
  250. private void OnOpenCompleted(object state) {
  251. if ((this.OpenCompleted != null)) {
  252. InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
  253. this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
  254. }
  255. }
  256. public void OpenAsync() {
  257. this.OpenAsync(null);
  258. }
  259. public void OpenAsync(object userState) {
  260. if ((this.onBeginOpenDelegate == null)) {
  261. this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
  262. }
  263. if ((this.onEndOpenDelegate == null)) {
  264. this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
  265. }
  266. if ((this.onOpenCompletedDelegate == null)) {
  267. this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
  268. }
  269. base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
  270. }
  271. private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) {
  272. return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
  273. }
  274. private object[] OnEndClose(System.IAsyncResult result) {
  275. ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);
  276. return null;
  277. }
  278. private void OnCloseCompleted(object state) {
  279. if ((this.CloseCompleted != null)) {
  280. InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
  281. this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
  282. }
  283. }
  284. public void CloseAsync() {
  285. this.CloseAsync(null);
  286. }
  287. public void CloseAsync(object userState) {
  288. if ((this.onBeginCloseDelegate == null)) {
  289. this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
  290. }
  291. if ((this.onEndCloseDelegate == null)) {
  292. this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
  293. }
  294. if ((this.onCloseCompletedDelegate == null)) {
  295. this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
  296. }
  297. base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
  298. }
  299. /*
  300. protected override WebServiceMoonlightTest.ServiceReference1.IService1 CreateChannel() {
  301. return new Service1ClientChannel(this);
  302. }
  303. private class Service1ClientChannel : ChannelBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
  304. public Service1ClientChannel(System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1> client) :
  305. base(client) {
  306. }
  307. public System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState) {
  308. object[] _args = new object[0];
  309. System.IAsyncResult _result = base.BeginInvoke("GetData", _args, callback, asyncState);
  310. return _result;
  311. }
  312. public object EndGetData(System.IAsyncResult result) {
  313. object[] _args = new object[0];
  314. object _result = ((object)(base.EndInvoke("GetData", _args, result)));
  315. return _result;
  316. }
  317. }
  318. */
  319. }
  320. }