using HeuristicLab.Clients.Common; using HeuristicLab.Clients.Common.Properties; using HeuristicLab.Clients.Hive.WebJobManager.ViewModels; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Security; using System.Threading.Tasks; namespace HeuristicLab.Clients.Hive.WebJobManager.Services { /// /// Rewritten for Endpoint problems /// public class HiveServiceLocatorWeb : IHiveServiceLocator { private HiveServiceClient clientinst; public Guid UserId {get; set; } private string username; public string Username { get { return username; } set { username = value; } } private string password; public string Password { get { return password; } set { password = value; } } public int EndpointRetries { get; private set; } public string WorkingEndpoint { get; private set; } public void CallHiveService(Action call) { HiveServiceClient client = NewServiceClient(); try { call(client); } finally { try { client.Close(); } catch (Exception) { client.Abort(); } } } public T CallHiveService(Func call) { HiveServiceClient client = NewServiceClient(); try { return call(client); } finally { try { client.Close(); } catch (Exception) { client.Abort(); } } } /// /// Gets current loaded HiveServiceClient /// /// public HiveServiceClient getHiveServiceClient() { if(clientinst == null || clientinst.State != CommunicationState.Opened) { clientinst = NewServiceClient(); } return clientinst; } /// /// Checks if login still works /// /// public bool CheckLogin() { if(clientinst != null) { try { clientinst.GetJobs(); return true; } catch(Exception e) { if (e is MessageSecurityException || e is InvalidOperationException) { return false; } throw; } } return false; } public string GetEndpointInformation() { throw new NotImplementedException(); } /// /// Builds hard coded binding and gets a new HiveServiceClient /// /// private HiveServiceClient NewServiceClient() { //build config by code WSHttpBinding binding = new WSHttpBinding(); // I think most (or all) of these are defaults--I just copied them from app.config: binding.Name = "wsHttpBinding_Hive"; binding.CloseTimeout = TimeSpan.FromMinutes(1); binding.OpenTimeout = TimeSpan.FromMinutes(1); binding.ReceiveTimeout = TimeSpan.FromMinutes(35); binding.SendTimeout = TimeSpan.FromMinutes(35.0); binding.BypassProxyOnLocal = false; binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; binding.MaxBufferPoolSize = 2147483647; binding.MaxReceivedMessageSize = 2147483647; binding.MessageEncoding = WSMessageEncoding.Text; binding.TextEncoding = System.Text.Encoding.UTF8; binding.UseDefaultWebProxy = true; // binding.TransferMode = TransferMode.Buffered; binding.AllowCookies = false; binding.ReaderQuotas.MaxDepth = 32; binding.ReaderQuotas.MaxStringContentLength = 200000000; binding.ReaderQuotas.MaxArrayLength = 200000000; binding.ReaderQuotas.MaxBytesPerRead = 4096; binding.ReaderQuotas.MaxNameTableCharCount = 16384; binding.ReliableSession.Ordered = true; binding.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00"); binding.ReliableSession.Enabled = false; binding.Security.Mode = SecurityMode.Message; binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default; EndpointAddress end = new EndpointAddress("http://services.heuristiclab.com/Hive-3.3/HiveService.svc"); HiveServiceClient client = new HiveServiceClient(binding, end); client.ClientCredentials.UserName.UserName = this.username; client.ClientCredentials.UserName.Password = this.password; client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; return client; } public void destroyClient() { clientinst = null; } } }