Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Common/3.3/ClientFactory.cs @ 16386

Last change on this file since 16386 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.3 KB
RevLine 
[4387]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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;
[15386]23using System.Security.Cryptography.X509Certificates;
[4387]24using System.ServiceModel;
[5527]25using System.ServiceModel.Description;
[15386]26using System.ServiceModel.Security;
[4387]27using HeuristicLab.Clients.Common.Properties;
28
29namespace HeuristicLab.Clients.Common {
30  public static class ClientFactory {
[5701]31    #region CreateClient Methods
[4406]32    public static T CreateClient<T, I>()
[4387]33      where T : ClientBase<I>, I
34      where I : class {
35      return CreateClient<T, I>(null, null);
36    }
[4406]37    public static T CreateClient<T, I>(string endpointConfigurationName)
[4387]38      where T : ClientBase<I>, I
39      where I : class {
40      return CreateClient<T, I>(endpointConfigurationName, null);
41    }
[4406]42    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
[4387]43      where T : ClientBase<I>, I
44      where I : class {
[7690]45      return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, CryptoService.DecryptString(Settings.Default.Password));
[4913]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 {
[4387]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)) {
[4913]58        SetEndpointAddress(client.Endpoint, remoteAddress);
[4387]59      }
60
[4913]61      client.ClientCredentials.UserName.UserName = userName;
62      client.ClientCredentials.UserName.Password = password;
[15386]63      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.ChainTrust;
64
65      // we (jkarder + abeham) have disabled the revocation check for now
66      // the certificate requires OCSP instead of CRL for revocation checks, but the OCSP check fails
67      // we currently don't know why this is the case, because we observed a valid OCSP request/response using wireshark
68      client.ClientCredentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
[4406]69      return client;
[4387]70    }
[5701]71    #endregion
[4913]72
[5701]73    #region CreateChannelFactory Methods
[5706]74    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName)
[5701]75      where I : class {
[5527]76      return CreateChannelFactory<I>(endpointConfigurationName, null);
[4913]77    }
[5706]78    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress)
[5701]79      where I : class {
[7690]80      return CreateChannelFactory<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, CryptoService.DecryptString(Settings.Default.Password));
[4913]81    }
[5706]82    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
[5701]83      where I : class {
84      ChannelFactory<I> channelFactory = new ChannelFactory<I>(endpointConfigurationName);
[5710]85
[4913]86      if (!string.IsNullOrEmpty(remoteAddress)) {
[5701]87        SetEndpointAddress(channelFactory.Endpoint, remoteAddress);
[4913]88      }
[5701]89
[5706]90      channelFactory.Credentials.UserName.UserName = userName;
91      channelFactory.Credentials.UserName.Password = password;
[15386]92      channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.ChainTrust;
93
94      // we (jkarder + abeham) have disabled the revocation check for now
95      // the certificate requires OCSP instead of CRL for revocation checks, but the OCSP check fails
96      // we currently don't know why this is the case, because we observed a valid OCSP request/response using wireshark
97      channelFactory.Credentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
[5706]98      return channelFactory;
[4913]99    }
[5701]100    #endregion
[4913]101
[5701]102    #region Helpers
[4913]103    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
[5701]104      // change the endpoint address and preserve the identity certificate defined in the config file
105      EndpointAddressBuilder endpointAddressBuilder = new EndpointAddressBuilder(endpoint.Address);
106      UriBuilder uriBuilder = new UriBuilder(endpointAddressBuilder.Uri);
[4913]107      uriBuilder.Host = remoteAddress;
[5701]108      endpointAddressBuilder.Uri = uriBuilder.Uri;
109      endpoint.Address = endpointAddressBuilder.ToEndpointAddress();
[4913]110    }
[5701]111    #endregion
[4387]112  }
113}
Note: See TracBrowser for help on using the repository browser.