Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Common/3.3/ClientFactory.cs @ 14204

Last change on this file since 14204 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
26
27namespace HeuristicLab.Clients.Common {
28  public static class ClientFactory {
29    #region CreateClient Methods
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, CryptoService.DecryptString(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    #endregion
65
66    #region CreateChannelFactory Methods
67    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName)
68      where I : class {
69      return CreateChannelFactory<I>(endpointConfigurationName, null);
70    }
71    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress)
72      where I : class {
73      return CreateChannelFactory<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, CryptoService.DecryptString(Settings.Default.Password));
74    }
75    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
76      where I : class {
77      ChannelFactory<I> channelFactory = new ChannelFactory<I>(endpointConfigurationName);
78
79      if (!string.IsNullOrEmpty(remoteAddress)) {
80        SetEndpointAddress(channelFactory.Endpoint, remoteAddress);
81      }
82
83      channelFactory.Credentials.UserName.UserName = userName;
84      channelFactory.Credentials.UserName.Password = password;
85      channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
86      return channelFactory;
87    }
88    #endregion
89
90    #region Helpers
91    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
92      // change the endpoint address and preserve the identity certificate defined in the config file
93      EndpointAddressBuilder endpointAddressBuilder = new EndpointAddressBuilder(endpoint.Address);
94      UriBuilder uriBuilder = new UriBuilder(endpointAddressBuilder.Uri);
95      uriBuilder.Host = remoteAddress;
96      endpointAddressBuilder.Uri = uriBuilder.Uri;
97      endpoint.Address = endpointAddressBuilder.ToEndpointAddress();
98    }
99    #endregion
100  }
101}
Note: See TracBrowser for help on using the repository browser.