Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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# WebAPI返回200,但SendAsync调用显示500_C#_Asp.net Mvc_Asp.net Mvc 4_Asp.net Web Api_Sendasync - Fatal编程技术网

C# WebAPI返回200,但SendAsync调用显示500

C# WebAPI返回200,但SendAsync调用显示500,c#,asp.net-mvc,asp.net-mvc-4,asp.net-web-api,sendasync,C#,Asp.net Mvc,Asp.net Mvc 4,Asp.net Web Api,Sendasync,我有一个在POST和GET上调用WebAPI异步的MVC应用程序。在本地运行WebAPI和MVC应用程序时,WebAPI响应显示成功,但SendAsync请求返回500。而且,Fiddler不显示API调用。我怀疑这与异步请求的处理方式有关,但我不确定我遗漏了什么 MVC控制器对API的调用: public Model UploadFile(Model formCollection) { var documentModel = formCollection.ToString();

我有一个在POST和GET上调用WebAPI异步的MVC应用程序。在本地运行WebAPI和MVC应用程序时,WebAPI响应显示成功,但SendAsync请求返回500。而且,Fiddler不显示API调用。我怀疑这与异步请求的处理方式有关,但我不确定我遗漏了什么

MVC控制器对API的调用:

public Model UploadFile(Model formCollection)
{
    var documentModel = formCollection.ToString();

    var client = new HttpClient();
    var uri = new Uri(BaseUri + "document/");

    var content = new StringContent(documentModel);

    var request = new HttpRequestMessage(HttpMethod.Post, uri) {Content = content};

    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    var response = client.SendAsync(request);
    response.Wait();

    try
    {
        var returned = response.Result;
        if (returned.StatusCode != HttpStatusCode.OK)
        {
            throw new Exception(returned.RequestMessage.ToString());
        }

        var model = JsonConvert.DeserializeObject<Model>    (returned.Content.ReadAsStringAsync().Result);
        model.FileContents = "";

        return model;
    }
    catch(Exception e)
    {
        var error = new Exception("Service failure - ", e);
        throw error;
    }
}
关于客户为什么在您的Global.asax.cs中显示500?

有什么想法吗

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // Try adding the following lines:
        var configuration = GlobalConfiguration.Configuration;
        var formatters = configuration.Formatters;
        formatters.Clear();
        formatters.Add(new JsonMediaTypeFormatter());
    }

    // A good idea to have this:
    protected void Application_Error()
    {
        Exception unhandledException = Server.GetLastError();
        // Set a breakpoint here or do something to log the exception
    }
添加到
Application\u Start
的代码将确保序列化只针对JSON。虽然这可能不是您在最后代码中想要的,但它可能会有助于作为一种临时措施来隔离问题的原因

添加
Application\u Error
应有助于捕获WebAPI层中发生的问题,例如当它序列化从控制器方法返回的对象时


我怀疑
SubmitNew
返回的是无法序列化的内容,例如无限递归引用(例如:具有相互引用的父/子结构)。

我发现了问题,WebAPI的日志记录服务在初始POST和GET调用结果后发生超时错误。。问题已解决。

我对一些代码也有类似的问题,我“改进了”-我看到HttpResponseMessage实现了IDisposable接口,因此决定包装返回请求。CreateResponse方法是一个using语句,导致500错误


删除using语句为我解决了这个问题。

然后包装var response=client.sendsync(请求);使用try-catch块并查看异常错误500表示WebAPI配置不正确。您是否有更简单的WebAPI get方法来测试,以帮助您确认API是否正常工作?使用POSTMAN/Advanced rest客户端进行此调用,您将能够在响应正文中看到错误详细信息如上所述使用POSTMAN,或者不使用RequestMessage(不包含实际响应)引发异常,请尝试读取response.Content.ReadAsStringAsync()并进行检查。我发现了问题,WebAPI的日志记录服务在初始POST和GET调用结果之后出现超时错误。。问题解决了。
return Request.CreateResponse(HttpStatusCode.OK, result);
public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // Try adding the following lines:
        var configuration = GlobalConfiguration.Configuration;
        var formatters = configuration.Formatters;
        formatters.Clear();
        formatters.Add(new JsonMediaTypeFormatter());
    }

    // A good idea to have this:
    protected void Application_Error()
    {
        Exception unhandledException = Server.GetLastError();
        // Set a breakpoint here or do something to log the exception
    }