Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Clients.Common/3.3/ClientFactory.cs @ 5502

Last change on this file since 5502 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 6.2 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 HeuristicLab.Clients.Common.Properties;
25using System.ServiceModel.Description;
26using System.Collections;
27using System.Collections.Generic;
28using HeuristicLab.Common;
29
30namespace HeuristicLab.Clients.Common {
31  public static class ClientFactory {
32    public static T CreateClient<T, I>()
33      where T : ClientBase<I>, I
34      where I : class {
35      return CreateClient<T, I>(null, null);
36    }
37    public static T CreateClient<T, I>(string endpointConfigurationName)
38      where T : ClientBase<I>, I
39      where I : class {
40      return CreateClient<T, I>(endpointConfigurationName, null);
41    }
42    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
43      where T : ClientBase<I>, I
44      where I : class {
45      return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
46    }
47    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
48      where T : ClientBase<I>, I
49      where I : class {
50      T client;
51      if (string.IsNullOrEmpty(endpointConfigurationName)) {
52        client = Activator.CreateInstance<T>();
53      } else {
54        client = (T)Activator.CreateInstance(typeof(T), endpointConfigurationName);
55      }
56
57      if (!string.IsNullOrEmpty(remoteAddress)) {
58        SetEndpointAddress(client.Endpoint, remoteAddress);
59      }
60
61      client.ClientCredentials.UserName.UserName = userName;
62      client.ClientCredentials.UserName.Password = password;
63      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
64      return client;
65    }
66
67    public static Disposable<I> CreateClient<I>(string endpointConfigurationName) where I : class {
68      return CreateClient<I>(endpointConfigurationName, null);
69    }
70    public static Disposable<I> CreateClient<I>(string endpointConfigurationName, string remoteAddress) where I : class {
71      return CreateClient<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
72    }
73    public static Disposable<I> CreateClient<I>(string endpointConfigurationName, string remoteAddress, string userName, string password) where I : class {
74      ChannelFactory<I> factory = GetChannelFactory<I>(endpointConfigurationName, userName, password);
75
76      if (!string.IsNullOrEmpty(remoteAddress)) {
77        SetEndpointAddress(factory.Endpoint, remoteAddress);
78      }
79      Disposable<I> disposable = new Disposable<I>(factory.CreateChannel());
80      disposable.OnDisposing += new EventHandler<EventArgs<object>>(disposable_OnDisposing);
81
82      return disposable;
83    }
84
85    private static void disposable_OnDisposing(object sender, EventArgs<object> e) {
86      DisposeCommunicationObject((ICommunicationObject)e.Value);
87      ((Disposable)sender).OnDisposing -= new EventHandler<EventArgs<object>>(disposable_OnDisposing);
88    }
89
90    private static IDictionary<ChannelProperties, ChannelFactory> channelFactoryCache = new Dictionary<ChannelProperties, ChannelFactory>();
91    private static ChannelFactory<I> GetChannelFactory<I>(string endpointConfigurationName, string userName, string password) where I : class {
92      ChannelProperties key = new ChannelProperties(typeof(I), endpointConfigurationName, userName, password);
93      if (!channelFactoryCache.ContainsKey(key)) {
94        channelFactoryCache.Add(key, new ChannelFactory<I>(endpointConfigurationName));
95        channelFactoryCache[key].Credentials.UserName.UserName = userName;
96        channelFactoryCache[key].Credentials.UserName.Password = password;
97        channelFactoryCache[key].Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
98      }
99      return (ChannelFactory<I>)channelFactoryCache[key];
100    }
101
102    public static void DisposeCommunicationObject(ICommunicationObject obj) {
103      if (obj != null) {
104        if (obj.State != CommunicationState.Faulted && obj.State != CommunicationState.Closed) {
105          try { obj.Close(); }
106          catch { obj.Abort(); }
107        } else {
108          obj.Abort();
109        }
110      }
111    }
112
113    /// <summary>
114    /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file
115    /// </summary>
116    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
117      EndpointAddressBuilder endpointAddressbuilder = new EndpointAddressBuilder(endpoint.Address);
118      UriBuilder uriBuilder = new UriBuilder(endpointAddressbuilder.Uri);
119      uriBuilder.Host = remoteAddress;
120      endpointAddressbuilder.Uri = uriBuilder.Uri;
121      endpoint.Address = endpointAddressbuilder.ToEndpointAddress();
122    }
123  }
124
125  internal struct ChannelProperties {
126    public Type type;
127    public string endpointConfigurationName;
128    public string userName;
129    public string password;
130
131    public ChannelProperties(Type type, string endpointConfigurationName, string userName, string password) {
132      this.type = type;
133      this.endpointConfigurationName = endpointConfigurationName;
134      this.userName = userName;
135      this.password = password;
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.