简体   繁体   English

使用 RestSharp 方法时 Visual Studio 中断

[英]When using RestSharp Method Visual Studio breaks

I am having a pretty weird issue, earlier today I had a large amount of code working and running in Visual Studio, my form worked flawlessly.我遇到了一个非常奇怪的问题,今天早些时候我有大量代码在 Visual Studio 中工作和运行,我的表单完美无缺。 I went for lunch came back and tried to open it and nothing.我去吃午饭回来了,试图打开它,什么也没有。 It runs, there are 0 errors, it uses memory but it won't display the form.它运行,有 0 个错误,它使用内存但不会显示表单。 I have not changed anything as far as I am aware.据我所知,我没有改变任何东西。

I have created a new Windows Form Application and re-written the code line by line, I have found what breaks it however I cannot for the life of me figure out why it is breaking it.我创建了一个新的 Windows 窗体应用程序并逐行重新编写代码,我找到了破坏它的原因,但是我一生都无法弄清楚为什么它破坏了它。

Form1.cs broken Form1.cs 坏了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using Newtonsoft;
using Newtonsoft.Json;

namespace RestApiViewerWUG
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var client = new RestClient("http://notrelevent.whocares.com/api/v1/token");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", "Basic cmVzdDo5RGJSIypkQDQ=");
            request.AddHeader("Content-Type", "text/plain");
            request.AddParameter("text/plain", "userName=rest&password=xxxxxx&grant_type=password", ParameterType.RequestBody); 
            IRestResponse response = client.Execute(request); //THIS LINE BREAKS IT

            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {

            }
    }
}

The issue问题

Simply removing the line "IRestResponse response = client.Execute(request);"只需删除“IRestResponse response = client.Execute(request);”这一行fixes it and allows the form to appear.修复它并允许表单出现。

What have I tried?我尝试了什么?

  • I have ensured RestSharp is properly installed and referenced, I have even tried several versions of it.我已经确保正确安装和引用了 RestSharp,我什至尝试了它的几个版本。
  • I have uninstalled and reinstalled Visual Studio 2015我已经卸载并重新安装了 Visual Studio 2015
  • As already stated I have created an entire new project going line by line如前所述,我已经逐行创建了一个全新的项目
  • Breakpoints/writing to console/log does nothing, 0 anything will happen when that line is left in the code断点/写入控制台/日志没有任何作用,0 当该行留在代码中时,任何事情都会发生

Other info其他信息

Program.cs程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RestApiViewerWUG
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

The form:表格:

Literally just textBox with the exact name of textBox1字面意思只是 textBox 与 textBox1 的确切名称

The short story is, you shouldn't be doing this type of thing in the constructor , do it after the form loads.简而言之,你不应该在构造函数中做这种类型的事情,在表单加载后做。

Then, if you there is a timeout , you aren't waiting for the form to load and can give feedback of the fact it's in progress, or if there is an exception you aren't tearing the form down.然后,如果您有一个timeout ,您不会等待表单加载并且可以提供正在进行的事实的反馈,或者如果出现exception您不会拆除表单

You would also catch any exceptions , and give some notification of the failure modes that might happen.您还将捕获任何异常,并给出可能发生的故障模式的一些通知。

Background背景

A forms constructor runs before a form is loaded (rendered), so it's best not to do anything expensive in them, in fact this is true for any constructor.表单构造函数在表单加载(渲染)之前运行,因此最好不要在其中执行任何昂贵的操作,实际上对于任何构造函数都是如此。 A constructor is for initializing the minimum state the class/struct needs to function.构造函数用于初始化类/结构需要运行的最小状态。

In regards to a forms constructor , if there is a long running process that happens during the constructor, the form load is directly effected and won't show leaving the user (and you) to scratch your head wondering why the form isn't showing.关于表单构造函数,如果在构造函数期间发生了长时间运行的过程,则表单加载会直接受到影响并且不会显示,让用户(和您)挠头想知道为什么表单没有显示. Even worse, if there is an unhandled exception your form never loads更糟糕的是,如果有未处理的异常,您的表单永远不会加载

I greatly apologize for this and appreciate the new knowledge I've learned.我对此深表歉意,并感谢我学到的新知识。

The answer to my question was way too simple... A month ago I added a certificate to our api, however it never seemed to apply, others had complained about a similar issue with the product we are using on the server.我的问题的答案太简单了......一个月前我在我们的 api 中添加了一个证书,但它似乎从未适用,其他人抱怨我们在服务器上使用的产品存在类似问题。 I didn't think anything of it.我什么都没想。

Yesterday they took the server offline for a few minutes to replace a PSU, once restarted... my certificate loaded in so now it's using https.昨天他们将服务器离线几分钟以更换 PSU,一旦重新启动......我的证书已加载,现在它使用 https。

The reason it wouldn't "load" was actually because the timeout time to the server takes ~ 60 seconds so after waiting long enough populated.它不会“加载”的原因实际上是因为服务器的超时时间需要约 60 秒,因此在等待足够长的填充时间之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM