#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.ServiceModel; using System.ServiceModel.Description; using HeuristicLab.Clients.Common.Properties; using HeuristicLab.Common; namespace HeuristicLab.Clients.Common { public static class ClientFactory { public static T CreateClient() where T : ClientBase, I where I : class { return CreateClient(null, null); } public static T CreateClient(string endpointConfigurationName) where T : ClientBase, I where I : class { return CreateClient(endpointConfigurationName, null); } public static T CreateClient(string endpointConfigurationName, string remoteAddress) where T : ClientBase, I where I : class { return CreateClient(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password); } public static T CreateClient(string endpointConfigurationName, string remoteAddress, string userName, string password) where T : ClientBase, I where I : class { T client; if (string.IsNullOrEmpty(endpointConfigurationName)) { client = Activator.CreateInstance(); } else { client = (T)Activator.CreateInstance(typeof(T), endpointConfigurationName); } if (!string.IsNullOrEmpty(remoteAddress)) { SetEndpointAddress(client.Endpoint, remoteAddress); } client.ClientCredentials.UserName.UserName = userName; client.ClientCredentials.UserName.Password = password; client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; return client; } public static Disposable> CreateChannelFactory(string endpointConfigurationName) where I : class { return CreateChannelFactory(endpointConfigurationName, null); } public static Disposable> CreateChannelFactory(string endpointConfigurationName, string remoteAddress) where I : class { return CreateChannelFactory(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password); } public static Disposable> CreateChannelFactory(string endpointConfigurationName, string remoteAddress, string userName, string password) where I : class { ChannelFactory factory = GetChannelFactory(endpointConfigurationName, userName, password); if (!string.IsNullOrEmpty(remoteAddress)) { SetEndpointAddress(factory.Endpoint, remoteAddress); } var disposable = new Disposable>(factory); disposable.OnDisposing += new EventHandler>(disposable_OnDisposing); return disposable; } private static void disposable_OnDisposing(object sender, EventArgs e) { DisposeCommunicationObject((ICommunicationObject)e.Value); ((Disposable)sender).OnDisposing -= new EventHandler>(disposable_OnDisposing); } private static ChannelFactory GetChannelFactory(string endpointConfigurationName, string userName, string password) where I : class { var channelFactory = new ChannelFactory(endpointConfigurationName); channelFactory.Credentials.UserName.UserName = userName; channelFactory.Credentials.UserName.Password = password; channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; return channelFactory; } public static void DisposeCommunicationObject(ICommunicationObject obj) { if (obj != null) { if (obj.State != CommunicationState.Faulted && obj.State != CommunicationState.Closed) { try { obj.Close(); } catch { obj.Abort(); } } else { obj.Abort(); } } } /// /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file /// private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) { EndpointAddressBuilder endpointAddressbuilder = new EndpointAddressBuilder(endpoint.Address); UriBuilder uriBuilder = new UriBuilder(endpointAddressbuilder.Uri); uriBuilder.Host = remoteAddress; endpointAddressbuilder.Uri = uriBuilder.Uri; endpoint.Address = endpointAddressbuilder.ToEndpointAddress(); } } }