【问题标题】:Belgrade SqlClient -> Response won't finish贝尔格莱德 SqlClient -> 响应不会完成
【发布时间】:2019-12-23 14:54:18
【问题描述】:

我想要做的只是从存储过程 (MS SQL) 向 Web API 调用者发送一个(大)json 输出(用于 json 路径)。 我使用 http://jocapc.github.io/Belgrade-SqlClient/aspnet.html 请求 SQL Server。这是因为我看了这篇文章:https://www.codeproject.com/Articles/1106622/Building-REST-services-with-ASP-NET-Core-Web-API-a Firefox 收到结果(只有文本,没有格式化的 json),但请求永远不会结束。 PostMan 没有显示任何结果。

    public HttpResponseMessage Get(int rows)
    {

        ICommand cmd = new Command(connStr);

        HttpResponseMessage response = Request.CreateResponse();
        response.Content = new PushStreamContent(async (stream, content, context) => 
        {
            await cmd
            .Proc("cuh.GetADDRESSES")
            .Param("@rows", rows)
            .Stream(stream, "[]");
        }, "application/json");

        //response.Headers.TransferEncodingChunked = true;

        return response;

    }

我读到,当请求结束时流未关闭时,可能会发生这种情况。但我不知道在这种情况下该怎么做。 任何其他处理来自存储过程的 json 输出的工作方法将不胜感激。另一个方向也一样:通过 nvarchar(max) 参数将 JSON 直接写入带有存储过程的数据库(存储过程代码不是我的问题)。

【问题讨论】:

  • 命令是否启动过?
  • 是的,Firefox 正在接收文本输出。我尝试将一行转换为 json 和很多行。它有效,但似乎 firefox 无休止地等待更多......
  • 会不会是输出太大了?您是否尝试将其写入文件以查看是否结束?

标签: c# json sql-server webapi


【解决方案1】:

比预期的要容易得多:

    public HttpResponseMessage Get(int rows)
    {

        ICommand cmd = new Command(connStr);

        HttpResponseMessage response = Request.CreateResponse();
        response.Content = new PushStreamContent(async (stream, content, context) => 
        {

            await cmd
            .Proc("cuh.GetADDRESSES")
            .Param("@rows", rows)
            .Stream(stream, "[]");

            stream.Dispose();//This is the missing part to get it work

        }, "application/json");

        return response;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多