Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Common/3.3/ClientFactory.cs @ 5527

Last change on this file since 5527 was 5527, checked in by cneumuel, 12 years ago

#1298

  • removed caching of ChannelFactory as this leeds to session timeouts (factory needs to be disposed properly)
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.ServiceModel;
24using System.ServiceModel.Description;
25using HeuristicLab.Clients.Common.Properties;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Clients.Common {
29  public static class ClientFactory {
30    public static T CreateClient<T, I>()
31      where T : ClientBase<I>, I
32      where I : class {
33      return CreateClient<T, I>(null, null);
34    }
35    public static T CreateClient<T, I>(string endpointConfigurationName)
36      where T : ClientBase<I>, I
37      where I : class {
38      return CreateClient<T, I>(endpointConfigurationName, null);
39    }
40    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
41      where T : ClientBase<I>, I
42      where I : class {
43      return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
44    }
45    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
46      where T : ClientBase<I>, I
47      where I : class {
48      T client;
49      if (string.IsNullOrEmpty(endpointConfigurationName)) {
50        client = Activator.CreateInstance<T>();
51      } else {
52        client = (T)Activator.CreateInstance(typeof(T), endpointConfigurationName);
53      }
54
55      if (!string.IsNullOrEmpty(remoteAddress)) {
56        SetEndpointAddress(client.Endpoint, remoteAddress);
57      }
58
59      client.ClientCredentials.UserName.UserName = userName;
60      client.ClientCredentials.UserName.Password = password;
61      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
62      return client;
63    }
64
65    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName) where I : class {
66      return CreateChannelFactory<I>(endpointConfigurationName, null);
67    }
68    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress) where I : class {
69      return CreateChannelFactory<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
70    }
71    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password) where I : class {
72      ChannelFactory<I> factory = GetChannelFactory<I>(endpointConfigurationName, userName, password);
73
74      if (!string.IsNullOrEmpty(remoteAddress)) {
75        SetEndpointAddress(factory.Endpoint, remoteAddress);
76      }
77      var disposable = new Disposable<ChannelFactory<I>>(factory);
78      disposable.OnDisposing += new EventHandler<EventArgs<object>>(disposable_OnDisposing);
79      return disposable;
80    }
81
82    private static void disposable_OnDisposing(object sender, EventArgs<object> e) {
83      DisposeCommunicationObject((ICommunicationObject)e.Value);
84      ((Disposable)sender).OnDisposing -= new EventHandler<EventArgs<object>>(disposable_OnDisposing);
85    }
86
87    private static ChannelFactory<I> GetChannelFactory<I>(string endpointConfigurationName, string userName, string password) where I : class {
88      var channelFactory = new ChannelFactory<I>(endpointConfigurationName);
89      channelFactory.Credentials.UserName.UserName = userName;
90      channelFactory.Credentials.UserName.Password = password;
91      channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
92      return channelFactory;
93    }
94
95    public static void DisposeCommunicationObject(ICommunicationObject obj) {
96      if (obj != null) {
97        if (obj.State != CommunicationState.Faulted && obj.State != CommunicationState.Closed) {
98          try { obj.Close(); }
99          catch { obj.Abort(); }
100        } else {
101          obj.Abort();
102        }
103      }
104    }
105
106    /// <summary>
107    /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file
108    /// </summary>
109    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
110      EndpointAddressBuilder endpointAddressbuilder = new EndpointAddressBuilder(endpoint.Address);
111      UriBuilder uriBuilder = new UriBuilder(endpointAddressbuilder.Uri);
112      uriBuilder.Host = remoteAddress;
113      endpointAddressbuilder.Uri = uriBuilder.Uri;
114      endpoint.Address = endpointAddressbuilder.ToEndpointAddress();
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.