4 // Atsushi Enomoto <atsushi@ximian.com>
6 // Copyright (C) 2011 Novell, Inc. http://www.novell.com
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
28 using System.Collections.Generic;
30 using System.Runtime.Serialization;
31 using System.ServiceModel;
32 using System.ServiceModel.Description;
33 using System.Threading;
34 using NUnit.Framework;
36 using WebServiceMoonlightTest.ServiceReference1;
38 namespace MonoTests.System.ServiceModel.Dispatcher
41 public class Bug652331Test
44 public void Bug652331_2 () // test in one of the comment
47 ServiceHost serviceHost = new ServiceHost (typeof (Service1), new Uri ("http://localhost:37564/Service1"));
48 serviceHost.AddServiceEndpoint (typeof (IService1), new BasicHttpBinding (), string.Empty);
50 // Enable metadata exchange (WSDL publishing)
51 var mexBehavior = new ServiceMetadataBehavior ();
52 mexBehavior.HttpGetEnabled = true;
53 serviceHost.Description.Behaviors.Add (mexBehavior);
54 serviceHost.AddServiceEndpoint (typeof (IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding (), "mex");
60 var binding = new BasicHttpBinding ();
61 var remoteAddress = new EndpointAddress ("http://localhost:37564/Service1");
62 var client = new Service1Client (binding, remoteAddress);
64 var wait = new ManualResetEvent (false);
65 client.GetDataCompleted += delegate (object o, GetDataCompletedEventArgs e) {
68 Assert.AreEqual ("A", ((DataType1) e.Result).Id, "#1");
72 client.GetDataAsync ();
73 if (!wait.WaitOne (TimeSpan.FromSeconds (20)))
74 Assert.Fail ("timeout");
80 public class Service1 : IService1
82 public object GetData ()
84 return new DataType1 { Id = "A" };
88 public IAsyncResult BeginGetData (AsyncCallback callback, object state)
91 d = new Func<object> (GetData);
92 return d.BeginInvoke (callback, state);
95 public object EndGetData (IAsyncResult result)
97 return d.EndInvoke (result);
103 // below are part of autogenerated code in comment #1 on bug #652331.
105 namespace WebServiceMoonlightTest.ServiceReference1 {
107 [System.Diagnostics.DebuggerStepThroughAttribute()]
108 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
109 [System.Runtime.Serialization.DataContractAttribute(Name="DataType1", Namespace="http://mynamespace")]
110 public partial class DataType1 : object, System.ComponentModel.INotifyPropertyChanged {
112 private string IdField;
114 [System.Runtime.Serialization.DataMemberAttribute()]
120 if ((object.ReferenceEquals(this.IdField, value) != true)) {
121 this.IdField = value;
122 this.RaisePropertyChanged("Id");
127 public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
129 protected void RaisePropertyChanged(string propertyName) {
130 System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
131 if ((propertyChanged != null)) {
132 propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
137 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
138 [System.ServiceModel.ServiceContractAttribute(Namespace="http://mynamespace", ConfigurationName="ServiceReference1.IService1")]
139 [ServiceKnownType (typeof (DataType1))]
140 public interface IService1 {
142 [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://mynamespace/IService1/GetData", ReplyAction="http://mynamespace/IService1/GetDataResponse")]
143 [System.ServiceModel.ServiceKnownTypeAttribute(typeof(WebServiceMoonlightTest.ServiceReference1.DataType1))]
144 System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState);
146 object EndGetData(System.IAsyncResult result);
149 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
150 public interface IService1Channel : WebServiceMoonlightTest.ServiceReference1.IService1, System.ServiceModel.IClientChannel {
153 [System.Diagnostics.DebuggerStepThroughAttribute()]
154 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
155 public partial class GetDataCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
157 private object[] results;
159 public GetDataCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
160 base(exception, cancelled, userState) {
161 this.results = results;
164 public object Result {
166 base.RaiseExceptionIfNecessary();
167 return ((object)(this.results[0]));
172 [System.Diagnostics.DebuggerStepThroughAttribute()]
173 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
174 public partial class Service1Client : System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
176 private BeginOperationDelegate onBeginGetDataDelegate;
178 private EndOperationDelegate onEndGetDataDelegate;
180 private System.Threading.SendOrPostCallback onGetDataCompletedDelegate;
182 private BeginOperationDelegate onBeginOpenDelegate;
184 private EndOperationDelegate onEndOpenDelegate;
186 private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
188 private BeginOperationDelegate onBeginCloseDelegate;
190 private EndOperationDelegate onEndCloseDelegate;
192 private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
194 public Service1Client() {
197 public Service1Client(string endpointConfigurationName) :
198 base(endpointConfigurationName) {
201 public Service1Client(string endpointConfigurationName, string remoteAddress) :
202 base(endpointConfigurationName, remoteAddress) {
205 public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
206 base(endpointConfigurationName, remoteAddress) {
209 public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
210 base(binding, remoteAddress) {
214 public System.Net.CookieContainer CookieContainer {
216 System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
217 if ((httpCookieContainerManager != null)) {
218 return httpCookieContainerManager.CookieContainer;
225 System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
226 if ((httpCookieContainerManager != null)) {
227 httpCookieContainerManager.CookieContainer = value;
230 throw new System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
231 "ookieContainerBindingElement.");
237 public event System.EventHandler<GetDataCompletedEventArgs> GetDataCompleted;
239 public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
241 public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
243 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
244 System.IAsyncResult WebServiceMoonlightTest.ServiceReference1.IService1.BeginGetData(System.AsyncCallback callback, object asyncState) {
245 return base.Channel.BeginGetData(callback, asyncState);
248 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
249 object WebServiceMoonlightTest.ServiceReference1.IService1.EndGetData(System.IAsyncResult result) {
250 return base.Channel.EndGetData(result);
253 private System.IAsyncResult OnBeginGetData(object[] inValues, System.AsyncCallback callback, object asyncState) {
254 return ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).BeginGetData(callback, asyncState);
257 private object[] OnEndGetData(System.IAsyncResult result) {
258 object retVal = ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).EndGetData(result);
259 return new object[] {
263 private void OnGetDataCompleted(object state) {
264 if ((this.GetDataCompleted != null)) {
265 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
266 this.GetDataCompleted(this, new GetDataCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
270 public void GetDataAsync() {
271 this.GetDataAsync(null);
274 public void GetDataAsync(object userState) {
275 if ((this.onBeginGetDataDelegate == null)) {
276 this.onBeginGetDataDelegate = new BeginOperationDelegate(this.OnBeginGetData);
278 if ((this.onEndGetDataDelegate == null)) {
279 this.onEndGetDataDelegate = new EndOperationDelegate(this.OnEndGetData);
281 if ((this.onGetDataCompletedDelegate == null)) {
282 this.onGetDataCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetDataCompleted);
284 base.InvokeAsync(this.onBeginGetDataDelegate, null, this.onEndGetDataDelegate, this.onGetDataCompletedDelegate, userState);
287 private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) {
288 return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
291 private object[] OnEndOpen(System.IAsyncResult result) {
292 ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);
296 private void OnOpenCompleted(object state) {
297 if ((this.OpenCompleted != null)) {
298 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
299 this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
303 public void OpenAsync() {
304 this.OpenAsync(null);
307 public void OpenAsync(object userState) {
308 if ((this.onBeginOpenDelegate == null)) {
309 this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
311 if ((this.onEndOpenDelegate == null)) {
312 this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
314 if ((this.onOpenCompletedDelegate == null)) {
315 this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
317 base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
320 private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) {
321 return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
324 private object[] OnEndClose(System.IAsyncResult result) {
325 ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);
329 private void OnCloseCompleted(object state) {
330 if ((this.CloseCompleted != null)) {
331 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
332 this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
336 public void CloseAsync() {
337 this.CloseAsync(null);
340 public void CloseAsync(object userState) {
341 if ((this.onBeginCloseDelegate == null)) {
342 this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
344 if ((this.onEndCloseDelegate == null)) {
345 this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
347 if ((this.onCloseCompletedDelegate == null)) {
348 this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
350 base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
354 protected override WebServiceMoonlightTest.ServiceReference1.IService1 CreateChannel() {
355 return new Service1ClientChannel(this);
358 private class Service1ClientChannel : ChannelBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
360 public Service1ClientChannel(System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1> client) :
364 public System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState) {
365 object[] _args = new object[0];
366 System.IAsyncResult _result = base.BeginInvoke("GetData", _args, callback, asyncState);
370 public object EndGetData(System.IAsyncResult result) {
371 object[] _args = new object[0];
372 object _result = ((object)(base.EndInvoke("GetData", _args, result)));