I'm trying to upload a log file from a desktop app client to the sql db server using webapi2 and im getting this error "A Task was canceled", this error does not appear when i host the webapi project on local machine though!
public static bool UploadLogData(string exceptionLog)
{
bool result = true;
string dataFilePath = AppDomain.CurrentDomain.BaseDirectory + "ExceptionLog.zip";
CommonParamModel responseModel = new CommonParamModel();
try
{
HttpClient httpClient = new HttpClient() { BaseAddress = new Uri("http://mywebsite.com") };
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>( "P1", ComputerInfoHelper.GetCustomMachineKey())
, new KeyValuePair<string, string>( "P2", exceptionLog)
};
var content = new MultipartFormDataContent();
FileStream filestream = new FileStream(dataFilePath, FileMode.Open);
string fileName = System.IO.Path.GetFileName(dataFilePath);
content.Add(new StreamContent(filestream), "file", fileName);
content.Add(new FormUrlEncodedContent(pairs));
httpClient.Timeout = TimeSpan.FromMinutes(30);
var response = httpClient.PostAsync("api/Users/AddExceptionDataLog/", content).Result;
if (response.IsSuccessStatusCode)
{
string strResult = response.Content.ReadAsStringAsync().Result;
JObject json = JObject.Parse(strResult);
responseModel = json.ToObject(typeof(FreeUserApp)) as CommonParamModel;
}
else
{
result = false;
}
}
catch (Exception ex)
{
Utilities.Error.LogException(Utilities.Error.ErrorSources.Exception, ex.Message, Utilities.Debugger.GetCurrentMethod(), ex);
result = false;
}
return true;
}
答案 0 :(得分:0)
感谢 Michael Randall 和 DBro 确实有效,在客户端和api方法上使用了wait之后,问题得以解决。