C# Console应用程序无法将大型数据集发布到asp.net .net core 3.0 web api上。

问题描述 投票:0回答:1

我对我的控制台应用程序的最后一步感到困惑。我有一个发布功能,可以发布一个汽车品牌,包括车型、世代、改装和汽车数据。你可以想象,这是一个很大的数据集。小的品牌是发送没有问题,但当我来到更大的品牌,如奥迪,宝马和梅赛德斯,然后我的应用程序不张贴。

以下是发送数据的控制台应用程序代码。

private static async Task PostDataSet(List<Brand> DataSet)
    {

        string key = Api_Key;
        if (key == null || key == "")
        {
            Console.WriteLine("No key set");
            Console.ReadKey();
            return;
        }
        Console.WriteLine("Posting a batch with " + DataSet.Count + " items :" + DataSet[0].Name);
        try
        {
            using (var wb = new WebClient())
            {
                var data = new NameValueCollection();
                data["Key"] = Api_Key;
                data["Dataset"] = JsonConvert.SerializeObject(DataSet).ToString();
                data["HostIp"] = Hostip;
                string ResponseString = "";
                if (UseLive)
                {
                    Uri x = new Uri(LiveUrl + "/Api/FullApiUpdate");
                    var response = await wb.UploadValuesTaskAsync(x, "POST", data);
                    ResponseString = Encoding.UTF8.GetString(response);
                    Console.WriteLine(ResponseString);
                }
                if (!UseLive)
                {
                    Uri x = new Uri(localurl + "/Api/FullApiUpdate");
                    var response = await wb.UploadValuesTaskAsync(x, "POST", data);
                    ResponseString = Encoding.UTF8.GetString(response);
                    Console.WriteLine(ResponseString);
                }
                if (ResponseString == "No api key specified" || ResponseString == "Invalid Api Key" || ResponseString == "Received null")
                {
                    Console.WriteLine("Api posted a cancellation token : " + ResponseString);
                    Console.ReadKey();
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error posting to api");
            Console.WriteLine(e.InnerException);
        }

    }

对于小品牌来说,所有的东西都能用,但是就像我之前跟你说的大品牌坏了。服务器端api的动作是这样的

public async Task<string> FullApiUpdate(string Dataset , string HostIp, string Key)
    {
        Console.WriteLine("API KEY ACCESED THIS ACTION : " + Key);
        Console.WriteLine("IP ACCESED THIS ACTION : " + HostIp);

        if (Key == null || Key == "")
        {
            return "No api key specified";
        }

        if (await _context.Api_Keys.Where(x=>x.Key == Key && x.Active == true).FirstOrDefaultAsync()==null)
        {
            return "Invalid Api Key";
        }

        if (Dataset != null || Dataset.Count() != 0)
        {
            List<Brand> FullSet = JsonConvert.DeserializeObject<List<Brand>>(Dataset);
            if (FullSet.Count() != 0)
            {
                Console.WriteLine("Reveived a batch of " + FullSet.Count() + " items from : " + HostIp);
                await _context.AddRangeAsync(FullSet);
                await _context.SaveChangesAsync();
                return "Updated a batch of " + FullSet.Count() + " Items";
            }
        }
        return "Received null";
    }

我真的不知道该从哪里去,这里是我的控制台输出。服务器控制台 :控制台

我已经尝试在我的启动

services.Configure<FormOptions>(x =>
        {
            x.BufferBody = false;
            x.KeyLengthLimit = 2048; // 2 KiB
            x.ValueLengthLimit = 4194304; // 32 MiB
            x.ValueCountLimit = 2048;// 1024
            x.MultipartHeadersCountLimit = 32; // 16
            x.MultipartHeadersLengthLimit = 32768; // 16384
            x.MultipartBoundaryLengthLimit = 256; // 128
            x.MultipartBodyLengthLimit = 134217728; // 128 MiB
        }); 

我找了多个论坛,但都不适合我。欢迎任何帮助

c# asp.net api post .net-core
1个回答
0
投票

我通过将大小增加到int.maxvalue来解决这个问题。

services.Configure<FormOptions>(x =>
        {
            x.BufferBody = false;
            x.KeyLengthLimit = int.MaxValue; // 2 KiB
            x.ValueLengthLimit = int.MaxValue; // 32 MiB
            x.ValueCountLimit = int.MaxValue;// 1024
            x.MultipartHeadersCountLimit = int.MaxValue; // 16
            x.MultipartHeadersLengthLimit = int.MaxValue; // 16384
            x.MultipartBoundaryLengthLimit = int.MaxValue; // 128
            x.MultipartBodyLengthLimit = int.MaxValue; // 128 MiB
        });
© www.soinside.com 2019 - 2024. All rights reserved.