小编Jac*_*060的帖子

使用Json.Net序列化对象会导致Out of Memory异常

免责声明:我确实完成了这里提供的大部分解决方案,但大部分都是在反序列化时讨论OOM异常.

我正在尝试使用Json.Net将对象(它是一个树)序列化为Json.一切都适用于小对象,但当我尝试大对象时,我得到OOM异常.因为它适用于相同数据类型的较小对象,我假设没有循环引用(我确实检查了我的数据结构).有没有办法我可以将我的对象转换为流(这是一个Windows应用商店应用程序)并使用该流生成Json?

 public static async Task<bool> SerializeIntoJson<T>(string fileName, StorageFolder destinationFolder, Content content)
    {
        ITraceWriter traceWriter = new MemoryTraceWriter();
        try
        {

            string jsonString = JsonConvert.SerializeObject(content, Formatting.Indented, new JsonSerializerSettings
            {
                PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                TypeNameHandling = TypeNameHandling.All,
                Error = ReportJsonErrors,
                TraceWriter = traceWriter,
                StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
            });
            System.Diagnostics.Debug.WriteLine(traceWriter);

            StorageFile file = await destinationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            await Windows.Storage.FileIO.WriteTextAsync(file, jsonString);
            return true;
        }
        catch (NullReferenceException nullException)
        {
            System.Diagnostics.Debug.WriteLine(traceWriter);
            logger.LogError("Exception happened while serializing input object, Error: " + nullException.Message);
            return false;
        }
        catch (Exception e)
        { …
Run Code Online (Sandbox Code Playgroud)

c# serialization out-of-memory json.net windows-store-apps

7
推荐指数
2
解决办法
2万
查看次数

Azure Functions(Http 触发)开始随机抛出 TaskCancelled 异常

我该如何解决此问题?

  • 我有几个使用以下触发器签名的 Azure 函数 [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")。

  • 这些功能按照消耗计划运行。

  • 它们每天会被触发 2-3 次。

该函数间歇性无法触发,但出现以下异常:

Azure 的诊断显示该任务在调用工作进程之前已取消。 诊断

public class AlpacaSample 
{
    private ILogger logger;

    public AlpacaSample(ILoggerFactory loggerFactory)
    {
        this.logger = loggerFactory.CreateLogger<AlpacaSample>();
    }

    [Function("AlpacaSample")]
    public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
    {
        try
        {
            this.requestData = req;
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var message = $"Received a trigger {requestBody}";
            this.logger.LogInformation(message);

            // Business logic removed for brevity 


            return await this.CreateStringResponse(req, "All Good");
        }

        catch (Exception ex)
        {
            string responseMessage =
                   $"Error occurrend while …
Run Code Online (Sandbox Code Playgroud)

.net c# azure-functions azure-http-trigger

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