Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1298

  • removed caching of ChannelFactory as this leeds to session timeouts (factory needs to be disposed properly)
File size: 5.3 KB
RevLine 
[4387]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4387]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;
[5527]24using System.ServiceModel.Description;
[4387]25using HeuristicLab.Clients.Common.Properties;
[4913]26using HeuristicLab.Common;
[4387]27
28namespace HeuristicLab.Clients.Common {
29  public static class ClientFactory {
[4406]30    public static T CreateClient<T, I>()
[4387]31      where T : ClientBase<I>, I
32      where I : class {
33      return CreateClient<T, I>(null, null);
34    }
[4406]35    public static T CreateClient<T, I>(string endpointConfigurationName)
[4387]36      where T : ClientBase<I>, I
37      where I : class {
38      return CreateClient<T, I>(endpointConfigurationName, null);
39    }
[4406]40    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
[4387]41      where T : ClientBase<I>, I
42      where I : class {
[4913]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 {
[4387]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)) {
[4913]56        SetEndpointAddress(client.Endpoint, remoteAddress);
[4387]57      }
58
[4913]59      client.ClientCredentials.UserName.UserName = userName;
60      client.ClientCredentials.UserName.Password = password;
[4387]61      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
[4406]62      return client;
[4387]63    }
[4913]64
[5527]65    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName) where I : class {
66      return CreateChannelFactory<I>(endpointConfigurationName, null);
[4913]67    }
[5527]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);
[4913]70    }
[5527]71    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password) where I : class {
[4913]72      ChannelFactory<I> factory = GetChannelFactory<I>(endpointConfigurationName, userName, password);
73
74      if (!string.IsNullOrEmpty(remoteAddress)) {
75        SetEndpointAddress(factory.Endpoint, remoteAddress);
76      }
[5527]77      var disposable = new Disposable<ChannelFactory<I>>(factory);
[4913]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 {
[5527]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;
[4913]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    }
[4387]116  }
117}
Note: See TracBrowser for help on using the repository browser.