Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/Imports/HiveServiceLocatorWeb.cs @ 13805

Last change on this file since 13805 was 13782, checked in by jlodewyc, 9 years ago

#2582 Resource calendar and client group creation, control implemented

File size: 5.9 KB
Line 
1using HeuristicLab.Clients.Common;
2using HeuristicLab.Clients.Common.Properties;
3using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
4using Microsoft.AspNet.Http;
5using Microsoft.AspNet.Mvc;
6using System;
7using System.Collections.Generic;
8using System.Linq;
9using System.ServiceModel;
10using System.ServiceModel.Security;
11using System.Threading.Tasks;
12
13namespace HeuristicLab.Clients.Hive.WebJobManager.Services
14{
15    /// <summary>
16    /// Rewritten for Endpoint problems
17    /// </summary>
18    public class HiveServiceLocatorWeb : IHiveServiceLocator
19    {
20        private HiveServiceClient clientinst;
21        public Guid UserId {get; set; }
22        private string username;
23        public string Username
24        {
25            get { return username; }
26            set { username = value; }
27        }
28
29        private string password;
30        public string Password
31        {
32            get { return password; }
33            set { password = value; }
34        }
35        public int EndpointRetries { get; private set; }
36
37        public string WorkingEndpoint { get; private set; }
38
39
40
41        public void CallHiveService(Action<IHiveService> call)
42        {
43            HiveServiceClient client = NewServiceClient();
44
45            try
46            {
47                call(client);
48            }
49            finally
50            {
51                try
52                {
53                    client.Close();
54                }
55                catch (Exception)
56                {
57                    client.Abort();
58                }
59            }
60        }
61
62        public T CallHiveService<T>(Func<IHiveService, T> call)
63        {
64            HiveServiceClient client = NewServiceClient();
65
66            try
67            {
68                return call(client);
69            }
70            finally
71            {
72                try
73                {
74                    client.Close();
75                }
76                catch (Exception)
77                {
78                    client.Abort();
79                }
80            }
81        }
82
83        /// <summary>
84        /// Gets current loaded HiveServiceClient
85        /// </summary>
86        /// <returns></returns>
87        public HiveServiceClient getHiveServiceClient()
88        {
89            if(clientinst == null || clientinst.State != CommunicationState.Opened)
90            {
91                clientinst = NewServiceClient();
92            }
93            return clientinst;
94        }
95        /// <summary>
96        /// Checks if login still works
97        /// </summary>
98        /// <returns></returns>
99        public bool CheckLogin()
100        {
101            if(clientinst != null)
102            {
103                try
104                {
105                    clientinst.GetJobs();
106                    return true;
107                }
108                catch(Exception e)
109                {
110                    if (e is MessageSecurityException || e is InvalidOperationException)
111                    {
112                        return false;
113                    }
114                    throw;
115                }
116            }
117            return false;
118        }
119
120        public string GetEndpointInformation()
121        {
122            throw new NotImplementedException();
123        }
124        /// <summary>
125        /// Builds hard coded binding and gets a new HiveServiceClient
126        /// </summary>
127        /// <returns></returns>
128        private HiveServiceClient NewServiceClient()
129        {
130            //build config by code
131            WSHttpBinding binding = new WSHttpBinding();
132            // I think most (or all) of these are defaults--I just copied them from app.config:
133            binding.Name = "wsHttpBinding_Hive";
134            binding.CloseTimeout = TimeSpan.FromMinutes(1);
135            binding.OpenTimeout = TimeSpan.FromMinutes(1); 
136            binding.ReceiveTimeout = TimeSpan.FromMinutes(35);
137            binding.SendTimeout = TimeSpan.FromMinutes(35.0);
138            binding.BypassProxyOnLocal = false;
139           
140            binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
141            binding.MaxBufferPoolSize = 2147483647;
142            binding.MaxReceivedMessageSize = 2147483647;
143            binding.MessageEncoding = WSMessageEncoding.Text;
144 
145            binding.TextEncoding = System.Text.Encoding.UTF8;
146            binding.UseDefaultWebProxy = true;
147           // binding.TransferMode = TransferMode.Buffered;
148            binding.AllowCookies = false;
149
150            binding.ReaderQuotas.MaxDepth = 32;
151            binding.ReaderQuotas.MaxStringContentLength = 200000000;
152            binding.ReaderQuotas.MaxArrayLength = 200000000;
153            binding.ReaderQuotas.MaxBytesPerRead = 4096;
154            binding.ReaderQuotas.MaxNameTableCharCount = 16384;
155
156            binding.ReliableSession.Ordered = true;
157            binding.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00");
158            binding.ReliableSession.Enabled = false;
159
160
161            binding.Security.Mode = SecurityMode.Message;
162            binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
163            binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
164           
165
166
167            EndpointAddress end = new EndpointAddress("http://services.heuristiclab.com/Hive-3.3/HiveService.svc");
168
169            HiveServiceClient client = new HiveServiceClient(binding, end);
170            client.ClientCredentials.UserName.UserName = this.username;
171            client.ClientCredentials.UserName.Password = this.password;
172            client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
173
174            return client;
175         
176        }
177
178        public void destroyClient()
179        {
180            clientinst = null;
181        }
182    }
183}
Note: See TracBrowser for help on using the repository browser.