Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpClient未按预期操作_C#_Task_Console Application - Fatal编程技术网

C# HttpClient未按预期操作

C# HttpClient未按预期操作,c#,task,console-application,C#,Task,Console Application,我的C#应用程序将文件上传到某个API,我使用了多部分请求,即我上传了一个json字符串和文件的二进制内容,它对大多数文件都很好,但对极少数文件它什么也不做,我的意思是让我们尝试一下名为file.pdf: 我的代码大致如下所示: public async Task<Dictionary<string , string>> Upload(string filePath) { FileInfo fi = new FileInfo(FilePath);

我的C#应用程序将文件上传到某个API,我使用了多部分请求,即我上传了一个json字符串和文件的二进制内容,它对大多数文件都很好,但对极少数文件它什么也不做,我的意思是让我们尝试一下名为
file.pdf

我的代码大致如下所示:

public async Task<Dictionary<string , string>> Upload(string filePath)
{   
      FileInfo fi = new FileInfo(FilePath);

      string jsonString="some json string";

      byte[] fileContents=File.ReadAllBytes(fi.FullName);

      Uri webService = new Uri(url);

      HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post , webService);

      requestMessage.Method = HttpMethod.Post;

      requestMessage.Headers.Add("Authorization" , "MyKey1234");

      const string boundry = "------------------My-Boundary";

      MultipartFormDataContent multiPartContent = new MultipartFormDataContent(boundry);

      ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);

      multiPartContent.Add(byteArrayContent);

      requestMessage.Content = multiPartContent;

      HttpClient httpClient = new HttpClient();

      Console.WriteLine("before");

HttpResponseMessage httpResponse = await httpClient.SendAsync(requestMessage , HttpCompletionOption.ResponseContentRead , CancellationToken.None);

Console.WriteLine("after");
    }
class Program
{
    static void Main(string[] args)
    {
        new MyClass().Start();
    }
}
输出:

before
Press any key to continue . . .
我的意思是没有例外,什么都没有,这是什么?虫子

编辑

控制台应用程序的结构如下所示:

public async Task<Dictionary<string , string>> Upload(string filePath)
{   
      FileInfo fi = new FileInfo(FilePath);

      string jsonString="some json string";

      byte[] fileContents=File.ReadAllBytes(fi.FullName);

      Uri webService = new Uri(url);

      HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post , webService);

      requestMessage.Method = HttpMethod.Post;

      requestMessage.Headers.Add("Authorization" , "MyKey1234");

      const string boundry = "------------------My-Boundary";

      MultipartFormDataContent multiPartContent = new MultipartFormDataContent(boundry);

      ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);

      multiPartContent.Add(byteArrayContent);

      requestMessage.Content = multiPartContent;

      HttpClient httpClient = new HttpClient();

      Console.WriteLine("before");

HttpResponseMessage httpResponse = await httpClient.SendAsync(requestMessage , HttpCompletionOption.ResponseContentRead , CancellationToken.None);

Console.WriteLine("after");
    }
class Program
{
    static void Main(string[] args)
    {
        new MyClass().Start();
    }
}
MyClass:

public async void Start()
{
  myDictionary = await Upload(filePath);
}

如注释部分所述,您的
main
方法不会等待等待调用,也不会等待
HttpClient
实例处理响应

如果console应用程序用于测试目的,则可以对该方法返回的任务实例调用
.Result
属性,如下所示:

new MyClass().Start().Result;
class Program
{
   static async Task Main(string[] args)
   {
        await new MyClass().Start();
   }
}
但是,最好使用
async
关键字,如下所示:

new MyClass().Start().Result;
class Program
{
   static async Task Main(string[] args)
   {
        await new MyClass().Start();
   }
}

最后,作为示例,您应该在异步方法名称中添加后缀“Async”。例如,
Start
将被命名为
StartAsync

,如注释部分所述,您的
main
方法不会等待可等待的调用,也不会等待
HttpClient
实例处理响应

如果console应用程序用于测试目的,则可以对该方法返回的任务实例调用
.Result
属性,如下所示:

new MyClass().Start().Result;
class Program
{
   static async Task Main(string[] args)
   {
        await new MyClass().Start();
   }
}
但是,最好使用
async
关键字,如下所示:

new MyClass().Start().Result;
class Program
{
   static async Task Main(string[] args)
   {
        await new MyClass().Start();
   }
}

最后,作为示例,您应该在异步方法名称中添加后缀“Async”。例如,
Start
将被命名为
StartAsync

调用者在哪里?我们需要完整的类才能为您提供解释。您可以添加
httpResponse.EnsureSuccessStatusCode()
行,然后中断或添加watch以查看API的响应吗?@Boxed我现在尝试了,但它以前仍然只打印
string@exe请编辑您的问题以包含完整的。控制台应用程序的主方法很可能在其声明中没有async关键字,因此不会等待响应。你能发布主要方法代码吗?调用者在哪里?我们需要完整的类才能为您提供解释。您可以添加
httpResponse.EnsureSuccessStatusCode()
行,然后中断或添加watch以查看API的响应吗?@Boxed我现在尝试了,但它以前仍然只打印
string@exe请编辑您的问题以包含完整的。控制台应用程序的主方法很可能在其声明中没有async关键字,因此不会等待响应。你能发布主要的方法代码吗?