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 using MonoTests.Helpers;
40 namespace MonoTests.System.ServiceModel.Dispatcher
43 public class Bug652331Test
46 public void Bug652331_2 () // test in one of the comment
49 int port = NetworkHelpers.FindFreePort ();
50 ServiceHost serviceHost = new ServiceHost (typeof (Service1), new Uri ("http://localhost:" + port + "/Service1"));
51 serviceHost.AddServiceEndpoint (typeof (IService1), new BasicHttpBinding (), string.Empty);
53 // Enable metadata exchange (WSDL publishing)
54 var mexBehavior = new ServiceMetadataBehavior ();
55 mexBehavior.HttpGetEnabled = true;
56 serviceHost.Description.Behaviors.Add (mexBehavior);
57 serviceHost.AddServiceEndpoint (typeof (IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding (), "mex");
63 var binding = new BasicHttpBinding ();
64 var remoteAddress = new EndpointAddress ("http://localhost:" + port + "/Service1");
65 var client = new Service1Client (binding, remoteAddress);
67 var wait = new ManualResetEvent (false);
68 client.GetDataCompleted += delegate (object o, GetDataCompletedEventArgs e) {
71 Assert.AreEqual ("A", ((DataType1) e.Result).Id, "#1");
75 client.GetDataAsync ();
76 if (!wait.WaitOne (TimeSpan.FromSeconds (20)))
77 Assert.Fail ("timeout");
83 public class Service1 : IService1
85 public object GetData ()
87 return new DataType1 { Id = "A" };
91 public IAsyncResult BeginGetData (AsyncCallback callback, object state)
94 d = new Func<object> (GetData);
95 return d.BeginInvoke (callback, state);
98 public object EndGetData (IAsyncResult result)
100 return d.EndInvoke (result);
106 // below are part of autogenerated code in comment #1 on bug #652331.
108 namespace WebServiceMoonlightTest.ServiceReference1 {
110 [System.Diagnostics.DebuggerStepThroughAttribute()]
111 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
112 [System.Runtime.Serialization.DataContractAttribute(Name="DataType1", Namespace="http://mynamespace")]
113 public partial class DataType1 : object, System.ComponentModel.INotifyPropertyChanged {
115 private string IdField;
117 [System.Runtime.Serialization.DataMemberAttribute()]
123 if ((object.ReferenceEquals(this.IdField, value) != true)) {
124 this.IdField = value;
125 this.RaisePropertyChanged("Id");
130 public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
132 protected void RaisePropertyChanged(string propertyName) {
133 System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
134 if ((propertyChanged != null)) {
135 propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
140 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
141 [System.ServiceModel.ServiceContractAttribute(Namespace="http://mynamespace", ConfigurationName="ServiceReference1.IService1")]
142 [ServiceKnownType (typeof (DataType1))]
143 public interface IService1 {
145 [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://mynamespace/IService1/GetData", ReplyAction="http://mynamespace/IService1/GetDataResponse")]
146 [System.ServiceModel.ServiceKnownTypeAttribute(typeof(WebServiceMoonlightTest.ServiceReference1.DataType1))]
147 System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState);
149 object EndGetData(System.IAsyncResult result);
152 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
153 public interface IService1Channel : WebServiceMoonlightTest.ServiceReference1.IService1, System.ServiceModel.IClientChannel {
156 [System.Diagnostics.DebuggerStepThroughAttribute()]
157 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
158 public partial class GetDataCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
160 private object[] results;
162 public GetDataCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
163 base(exception, cancelled, userState) {
164 this.results = results;
167 public object Result {
169 base.RaiseExceptionIfNecessary();
170 return ((object)(this.results[0]));
175 [System.Diagnostics.DebuggerStepThroughAttribute()]
176 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
177 public partial class Service1Client : System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
179 private BeginOperationDelegate onBeginGetDataDelegate;
181 private EndOperationDelegate onEndGetDataDelegate;
183 private System.Threading.SendOrPostCallback onGetDataCompletedDelegate;
185 private BeginOperationDelegate onBeginOpenDelegate;
187 private EndOperationDelegate onEndOpenDelegate;
189 private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
191 private BeginOperationDelegate onBeginCloseDelegate;
193 private EndOperationDelegate onEndCloseDelegate;
195 private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
197 public Service1Client() {
200 public Service1Client(string endpointConfigurationName) :
201 base(endpointConfigurationName) {
204 public Service1Client(string endpointConfigurationName, string remoteAddress) :
205 base(endpointConfigurationName, remoteAddress) {
208 public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
209 base(endpointConfigurationName, remoteAddress) {
212 public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
213 base(binding, remoteAddress) {
217 public System.Net.CookieContainer CookieContainer {
219 System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
220 if ((httpCookieContainerManager != null)) {
221 return httpCookieContainerManager.CookieContainer;
228 System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
229 if ((httpCookieContainerManager != null)) {
230 httpCookieContainerManager.CookieContainer = value;
233 throw new System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
234 "ookieContainerBindingElement.");
240 public event System.EventHandler<GetDataCompletedEventArgs> GetDataCompleted;
242 public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
244 public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
246 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
247 System.IAsyncResult WebServiceMoonlightTest.ServiceReference1.IService1.BeginGetData(System.AsyncCallback callback, object asyncState) {
248 return base.Channel.BeginGetData(callback, asyncState);
251 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
252 object WebServiceMoonlightTest.ServiceReference1.IService1.EndGetData(System.IAsyncResult result) {
253 return base.Channel.EndGetData(result);
256 private System.IAsyncResult OnBeginGetData(object[] inValues, System.AsyncCallback callback, object asyncState) {
257 return ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).BeginGetData(callback, asyncState);
260 private object[] OnEndGetData(System.IAsyncResult result) {
261 object retVal = ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).EndGetData(result);
262 return new object[] {
266 private void OnGetDataCompleted(object state) {
267 if ((this.GetDataCompleted != null)) {
268 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
269 this.GetDataCompleted(this, new GetDataCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
273 public void GetDataAsync() {
274 this.GetDataAsync(null);
277 public void GetDataAsync(object userState) {
278 if ((this.onBeginGetDataDelegate == null)) {
279 this.onBeginGetDataDelegate = new BeginOperationDelegate(this.OnBeginGetData);
281 if ((this.onEndGetDataDelegate == null)) {
282 this.onEndGetDataDelegate = new EndOperationDelegate(this.OnEndGetData);
284 if ((this.onGetDataCompletedDelegate == null)) {
285 this.onGetDataCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetDataCompleted);
287 base.InvokeAsync(this.onBeginGetDataDelegate, null, this.onEndGetDataDelegate, this.onGetDataCompletedDelegate, userState);
290 private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) {
291 return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
294 private object[] OnEndOpen(System.IAsyncResult result) {
295 ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);
299 private void OnOpenCompleted(object state) {
300 if ((this.OpenCompleted != null)) {
301 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
302 this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
306 public void OpenAsync() {
307 this.OpenAsync(null);
310 public void OpenAsync(object userState) {
311 if ((this.onBeginOpenDelegate == null)) {
312 this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
314 if ((this.onEndOpenDelegate == null)) {
315 this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
317 if ((this.onOpenCompletedDelegate == null)) {
318 this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
320 base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
323 private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) {
324 return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
327 private object[] OnEndClose(System.IAsyncResult result) {
328 ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);
332 private void OnCloseCompleted(object state) {
333 if ((this.CloseCompleted != null)) {
334 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
335 this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
339 public void CloseAsync() {
340 this.CloseAsync(null);
343 public void CloseAsync(object userState) {
344 if ((this.onBeginCloseDelegate == null)) {
345 this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
347 if ((this.onEndCloseDelegate == null)) {
348 this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
350 if ((this.onCloseCompletedDelegate == null)) {
351 this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
353 base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
357 protected override WebServiceMoonlightTest.ServiceReference1.IService1 CreateChannel() {
358 return new Service1ClientChannel(this);
361 private class Service1ClientChannel : ChannelBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
363 public Service1ClientChannel(System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1> client) :
367 public System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState) {
368 object[] _args = new object[0];
369 System.IAsyncResult _result = base.BeginInvoke("GetData", _args, callback, asyncState);
373 public object EndGetData(System.IAsyncResult result) {
374 object[] _args = new object[0];
375 object _result = ((object)(base.EndInvoke("GetData", _args, result)));