小编TCh*_*ick的帖子

C#Async ApiController过早关闭OutputStream

今天的问题是当使用WebApi 2和基于Async ApiController的Get方法时,它返回文件的内容.当我将Get方法更改为同步时,它可以正常工作,但只要我将其转换回异步,它就会过早地关闭流.(Fiddler报告连接中止)工作的同步代码是:

 public void Get(int id)
    {
        try
        {
            FileInfo fileInfo = logic.GetFileInfoSync(id);
            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            response.ClearContent();
            response.Buffer = true;
            response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileInfo.Node.Name + fileInfo.Ext + "\"");
            response.AddHeader("Content-Length", fileInfo.SizeInBytes.ToString());
            response.ContentType = "application/octet-stream";
            logic.GetDownloadStreamSync(id, response.OutputStream);
            response.StatusCode = (int)HttpStatusCode.OK;
            //HttpContext.Current.ApplicationInstance.CompleteRequest();
             response.End();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
Run Code Online (Sandbox Code Playgroud)

GetDownloadStreamSync如下:

public async Task GetDownloadStream(string fileIdentifier, Stream streamToCopyTo)
{
    string filePath = Path.Combine(fileIdentifierFolder, fileIdentifier);
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, BufferSize, false))
    {
        fs.CopyTo(streamToCopyTo);
    }
 } …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc async-await asp.net-web-api asp.net-web-api2

8
推荐指数
1
解决办法
2021
查看次数

使用C#在MySQL中创建存储过程

我正在尝试使用以下代码以编程方式创建新的存储过程:

using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();

                using (MySqlTransaction trans = conn.BeginTransaction())
                {
                    using (MySqlCommand command = conn.CreateCommand())
                    {
                        command.CommandText = query;

                        command.ExecuteNonQuery();
                    }

                    trans.Commit();
                }
            }
Run Code Online (Sandbox Code Playgroud)

以下文本作为从Mysql工作台复制的create语句:

static string query = @"
delimiter $$

CREATE PROCEDURE `GetParentIds`(IN `tempTableName` VARCHAR(255), IN `id` int)
BEGIN
DECLARE parId INT;
DECLARE curId INT;

  DROP TEMPORARY TABLE IF EXISTS tempTableName;

  CREATE TEMPORARY TABLE tempTableName (
node_id INT NOT NULL PRIMARY KEY
   );

 set curId := id;
 get_parents_loop: LOOP
     set parId := null; …
Run Code Online (Sandbox Code Playgroud)

c# mysql stored-procedures

1
推荐指数
1
解决办法
4247
查看次数