Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.4 KB
RevLine 
[4387]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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;
26
27namespace HeuristicLab.Clients.Common {
28  public static class ClientFactory {
[5701]29    #region CreateClient Methods
[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 {
[7690]43      return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, CryptoService.DecryptString(Settings.Default.Password));
[4913]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    }
[5701]64    #endregion
[4913]65
[5701]66    #region CreateChannelFactory Methods
[5706]67    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName)
[5701]68      where I : class {
[5527]69      return CreateChannelFactory<I>(endpointConfigurationName, null);
[4913]70    }
[5706]71    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress)
[5701]72      where I : class {
[7690]73      return CreateChannelFactory<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, CryptoService.DecryptString(Settings.Default.Password));
[4913]74    }
[5706]75    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
[5701]76      where I : class {
77      ChannelFactory<I> channelFactory = new ChannelFactory<I>(endpointConfigurationName);
[5710]78
[4913]79      if (!string.IsNullOrEmpty(remoteAddress)) {
[5701]80        SetEndpointAddress(channelFactory.Endpoint, remoteAddress);
[4913]81      }
[5701]82
[5706]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;
[4913]87    }
[5701]88    #endregion
[4913]89
[5701]90    #region Helpers
[4913]91    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
[5701]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);
[4913]95      uriBuilder.Host = remoteAddress;
[5701]96      endpointAddressBuilder.Uri = uriBuilder.Uri;
97      endpoint.Address = endpointAddressBuilder.ToEndpointAddress();
[4913]98    }
[5701]99    #endregion
[4387]100  }
101}
Note: See TracBrowser for help on using the repository browser.