Bug652331Test.cs 16 KB

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