private static SINnersClient GetSINnersClient() { SINnersClient client = null; try { var assembly = System.Reflection.Assembly.GetAssembly(typeof(frmChummerMain)); if (assembly.GetName().Version.Build == 0) { Properties.Settings.Default.SINnerUrl = "https://sinners.azurewebsites.net"; } else { Properties.Settings.Default.SINnerUrl = "https://sinners-beta.azurewebsites.net"; } if (System.Diagnostics.Debugger.IsAttached) { try { string local = "http://localhost:5000/"; var request = WebRequest.Create("http://localhost:5000/"); WebResponse response = request.GetResponse(); Properties.Settings.Default.SINnerUrl = local; System.Diagnostics.Trace.TraceInformation("Connected to " + local + "."); } catch (Exception e) { Properties.Settings.Default.SINnerUrl = "https://sinners-beta.azurewebsites.net"; System.Diagnostics.Trace.TraceInformation("Connected to " + Properties.Settings.Default.SINnerUrl + "."); } } ServicePointManager.ServerCertificateValidationCallback += delegate { return(true); }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; Uri baseUri = new Uri(Properties.Settings.Default.SINnerUrl); Microsoft.Rest.ServiceClientCredentials credentials = new MyCredentials(); DelegatingHandler delegatingHandler = new MyMessageHandler(); HttpClientHandler httpClientHandler = new HttpClientHandler(); httpClientHandler.CookieContainer = AuthorizationCookieContainer; client = new SINnersClient(baseUri, credentials, httpClientHandler, delegatingHandler); //double waitTime = 30; //var resptask = client.GetVersionWithHttpMessagesAsync().CancelAfter((int)(1000 * waitTime)); //resptask.ContinueWith((respresult) => // { // try // { // if (respresult.IsCanceled) // { // System.Diagnostics.Trace.TraceInformation("Could not connected to SINners in " + waitTime + " seconds."); // if (_clientNOTworking.HasValue == false) // _clientNOTworking = true; // return; // } // var verresp = respresult.Result; // if (verresp.Response.StatusCode == HttpStatusCode.OK) // System.Diagnostics.Trace.TraceInformation("Connected to SINners in version " + verresp.Body.AssemblyVersion + "."); // else if (verresp.Response.StatusCode == HttpStatusCode.Forbidden) // { // if (_clientNOTworking.HasValue == false) // _clientNOTworking = true; // throw new System.Web.HttpException(403, "WebService disabled by Admin!"); // } // _clientNOTworking = false; // } // catch (Exception e) // { // System.Diagnostics.Trace.TraceError(e.ToString()); // Console.WriteLine(e); // throw; // } // }); } catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); if (!clientErrorShown) { clientErrorShown = true; Exception inner = ex; while (inner.InnerException != null) { inner = inner.InnerException; } string msg = "Error connecting to SINners: " + Environment.NewLine; msg += "(the complete error description is copied to clipboard)" + Environment.NewLine + Environment.NewLine + inner.ToString(); PluginHandler.MainForm.DoThreadSafe(() => { System.Windows.Forms.Clipboard.SetText(ex.ToString()); }); msg += Environment.NewLine + Environment.NewLine + "Please check the Plugin-Options dialog."; MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } return(client); }
private static async Task <SINnersClient> GetSINnersClient() { SINnersClient client = null; try { var assembly = System.Reflection.Assembly.GetAssembly(typeof(frmChummerMain)); if (assembly.GetName().Version.Build == 0) { Properties.Settings.Default.SINnerUrl = "https://sinners.azurewebsites.net"; } else { Properties.Settings.Default.SINnerUrl = "https://sinners-beta.azurewebsites.net"; } if (System.Diagnostics.Debugger.IsAttached) { try { string local = "http://localhost:5000/"; var request = WebRequest.Create("http://localhost:5000/"); WebResponse response = request.GetResponse(); Properties.Settings.Default.SINnerUrl = local; System.Diagnostics.Trace.TraceInformation("Connected to " + local + "."); } catch (Exception e) { System.Diagnostics.Trace.TraceInformation("Connected to " + Properties.Settings.Default.SINnerUrl + "."); } } ServicePointManager.ServerCertificateValidationCallback += delegate { return(true); }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; Uri baseUri = new Uri(Properties.Settings.Default.SINnerUrl); Microsoft.Rest.ServiceClientCredentials credentials = new MyCredentials(); DelegatingHandler delegatingHandler = new MyMessageHandler(); HttpClientHandler httpClientHandler = new HttpClientHandler(); httpClientHandler.CookieContainer = AuthorizationCookieContainer; client = new SINnersClient(baseUri, credentials, httpClientHandler, delegatingHandler); var resptask = client.GetVersionWithHttpMessagesAsync(); resptask.ContinueWith((respresult) => { var verresp = respresult.Result; if (verresp.Response.StatusCode == HttpStatusCode.OK) { System.Diagnostics.Trace.TraceInformation("Connected to SINners in version " + verresp.Body.AssemblyVersion + "."); } else if (verresp.Response.StatusCode == HttpStatusCode.Forbidden) { _clientNOTworking = true; throw new System.Web.HttpException(403, "WebService disabled by Admin!"); } }); } catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); if (!clientErrorShown) { clientErrorShown = true; Exception inner = ex; while (inner.InnerException != null) { inner = inner.InnerException; } string msg = "Error connecting to SINners: " + Environment.NewLine + Environment.NewLine + inner.Message; msg += Environment.NewLine + Environment.NewLine + "Please check the Plugin-Options dialog."; MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } return(client); }