Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5701 was 5701, checked in by swagner, 13 years ago

Restructured and formatted code of ClientFactory (#1298)

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    #region CreateClient Methods
31    public static T CreateClient<T, I>()
32      where T : ClientBase<I>, I
33      where I : class {
34      return CreateClient<T, I>(null, null);
35    }
36    public static T CreateClient<T, I>(string endpointConfigurationName)
37      where T : ClientBase<I>, I
38      where I : class {
39      return CreateClient<T, I>(endpointConfigurationName, null);
40    }
41    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
42      where T : ClientBase<I>, I
43      where I : class {
44      return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
45    }
46    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
47      where T : ClientBase<I>, I
48      where I : class {
49      T client;
50      if (string.IsNullOrEmpty(endpointConfigurationName)) {
51        client = Activator.CreateInstance<T>();
52      } else {
53        client = (T)Activator.CreateInstance(typeof(T), endpointConfigurationName);
54      }
55
56      if (!string.IsNullOrEmpty(remoteAddress)) {
57        SetEndpointAddress(client.Endpoint, remoteAddress);
58      }
59
60      client.ClientCredentials.UserName.UserName = userName;
61      client.ClientCredentials.UserName.Password = password;
62      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
63      return client;
64    }
65    #endregion
66
67    #region CreateChannelFactory Methods
68    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName)
69      where I : class {
70      return CreateChannelFactory<I>(endpointConfigurationName, null);
71    }
72    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress)
73      where I : class {
74      return CreateChannelFactory<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
75    }
76    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
77      where I : class {
78      ChannelFactory<I> channelFactory = new ChannelFactory<I>(endpointConfigurationName);
79      channelFactory.Credentials.UserName.UserName = userName;
80      channelFactory.Credentials.UserName.Password = password;
81      channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
82
83      if (!string.IsNullOrEmpty(remoteAddress)) {
84        SetEndpointAddress(channelFactory.Endpoint, remoteAddress);
85      }
86
87      Disposable<ChannelFactory<I>> disposableChannelFactory = new Disposable<ChannelFactory<I>>(channelFactory);
88      disposableChannelFactory.OnDisposing += new EventHandler<EventArgs<object>>(DisposableChannelFactory_OnDisposing);
89      return disposableChannelFactory;
90    }
91
92    private static void DisposableChannelFactory_OnDisposing(object sender, EventArgs<object> e) {
93      DisposeCommunicationObject((ICommunicationObject)e.Value);
94      ((Disposable)sender).OnDisposing -= new EventHandler<EventArgs<object>>(DisposableChannelFactory_OnDisposing);
95    }
96    #endregion
97
98    public static void DisposeCommunicationObject(ICommunicationObject obj) {
99      if (obj != null) {
100        if (obj.State != CommunicationState.Faulted && obj.State != CommunicationState.Closed) {
101          try { obj.Close(); }
102          catch { obj.Abort(); }
103        } else {
104          obj.Abort();
105        }
106      }
107    }
108
109    #region Helpers
110    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
111      // change the endpoint address and preserve the identity certificate defined in the config file
112      EndpointAddressBuilder endpointAddressBuilder = new EndpointAddressBuilder(endpoint.Address);
113      UriBuilder uriBuilder = new UriBuilder(endpointAddressBuilder.Uri);
114      uriBuilder.Host = remoteAddress;
115      endpointAddressBuilder.Uri = uriBuilder.Uri;
116      endpoint.Address = endpointAddressBuilder.ToEndpointAddress();
117    }
118    #endregion
119  }
120}
Note: See TracBrowser for help on using the repository browser.