当将HTTP函数触发到查询实体状态时,程序崩溃了吗?

发布于 2025-02-08 06:14:51 字数 3398 浏览 3 评论 0原文

我是Azure功能和耐用实体的首发。我想做以下操作:

  1. 创建一个耐用的实体,它具有称为SystemList的状态;

  2. 创建一个队列触发函数以在我的队列中消耗消息并将其存储在我刚创建的实体状态中。

  3. 创建一个HTTP触发函数以获取实体的当前状态;

我在第三步中遇到了一些问题。

该代码如下:

//HttpTriggerFunction.cs:
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using System.Net.Http;
using System.Net;
using Newtonsoft.Json.Linq;

namespace **.Function
{
    public static class HttpTriggerQueryEntity
    {
        [FunctionName("QueryEntity")]
        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function)] HttpRequestMessage req,
            [DurableClient] IDurableEntityClient client)
        {
            var entityId = new EntityId(nameof(ProcessingListEntity), "processing_message_total");
            EntityStateResponse<JObject> stateResponse = await client.ReadEntityStateAsync<JObject>(entityId);
           
            //await client.SignalEntityAsync(entityId, "Empty");

            return req.CreateResponse(HttpStatusCode.OK,stateResponse.EntityState);
        }
    }
}

//blockData.cs
#nullable enable
using System;

namespace **.Function
{
    public class BlockData
    {
        public string? id { get; set; }
        public string? SessionId { get; set; }
        public string? MachineId { get; set; }
        public DateTime? Start { get; set; }
        public DateTime? End { get; set; }
        public string? Activity {get; set;}
        public string? BlobId { get; set; }
    }
}
//Entity.cs
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using System.Collections.Generic;
namespace **.Function
{
    [JsonObject(MemberSerialization.OptIn)]
    public class ProcessingListEntity
    {
        public ProcessingListEntity(){
            systemList = new Dictionary<string, BlockData>();
        }

        [JsonProperty("systemList")]
        public Dictionary<string, BlockData> systemList {get; set;}

        public void Add(BlockData blockData) {

            if (!this.systemList.ContainsKey(blockData.MachineId)) {
                this.systemList.Add(blockData.MachineId, blockData);
                return;
            }
            else {
                this.systemList[blockData.MachineId].Start = blockData.Start;
                this.systemList[blockData.MachineId].End = blockData.End;
            }
            
        }

        public void Empty() => this.systemList = new Dictionary<string, BlockData>();

        public Dictionary<string, BlockData> Get() => this.systemList;

        [FunctionName(nameof(ProcessingListEntity))]
        public static Task Run([EntityTrigger] IDurableEntityContext ctx)
            => ctx.DispatchAsync<ProcessingListEntity>();
    }
}

但是,当我在本地环境中运行它并将HTTP查询发送到程序中时。但是,当我更改httptriggerfunction.cs中的以下行时:

return req.CreateResponse(HttpStatusCode.OK,stateResponse.EntityState);

可以运行

 return req.CreateResponse(HttpStatusCode.OK,stateResponse.EntityState + "");

并返回所需的字符串。但是我想返回的是JSON字符串。

那么我该怎么办来修复我的代码?谢谢你!

I am a starter in Azure Functions and durable Entities. I want to do the following things:

  1. Create a durable entity and it has a state called systemList;

  2. Create a queue trigger function to consume messages in my queue and store them in the entity state I just created.

  3. create a http trigger function to get the current state of the entity;

And I met some problems in the 3rd step.

The code is as follows:

//HttpTriggerFunction.cs:
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using System.Net.Http;
using System.Net;
using Newtonsoft.Json.Linq;

namespace **.Function
{
    public static class HttpTriggerQueryEntity
    {
        [FunctionName("QueryEntity")]
        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function)] HttpRequestMessage req,
            [DurableClient] IDurableEntityClient client)
        {
            var entityId = new EntityId(nameof(ProcessingListEntity), "processing_message_total");
            EntityStateResponse<JObject> stateResponse = await client.ReadEntityStateAsync<JObject>(entityId);
           
            //await client.SignalEntityAsync(entityId, "Empty");

            return req.CreateResponse(HttpStatusCode.OK,stateResponse.EntityState);
        }
    }
}

//blockData.cs
#nullable enable
using System;

namespace **.Function
{
    public class BlockData
    {
        public string? id { get; set; }
        public string? SessionId { get; set; }
        public string? MachineId { get; set; }
        public DateTime? Start { get; set; }
        public DateTime? End { get; set; }
        public string? Activity {get; set;}
        public string? BlobId { get; set; }
    }
}
//Entity.cs
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using System.Collections.Generic;
namespace **.Function
{
    [JsonObject(MemberSerialization.OptIn)]
    public class ProcessingListEntity
    {
        public ProcessingListEntity(){
            systemList = new Dictionary<string, BlockData>();
        }

        [JsonProperty("systemList")]
        public Dictionary<string, BlockData> systemList {get; set;}

        public void Add(BlockData blockData) {

            if (!this.systemList.ContainsKey(blockData.MachineId)) {
                this.systemList.Add(blockData.MachineId, blockData);
                return;
            }
            else {
                this.systemList[blockData.MachineId].Start = blockData.Start;
                this.systemList[blockData.MachineId].End = blockData.End;
            }
            
        }

        public void Empty() => this.systemList = new Dictionary<string, BlockData>();

        public Dictionary<string, BlockData> Get() => this.systemList;

        [FunctionName(nameof(ProcessingListEntity))]
        public static Task Run([EntityTrigger] IDurableEntityContext ctx)
            => ctx.DispatchAsync<ProcessingListEntity>();
    }
}

But when I run it in my local environment and send Http query to it, the program stop. But when I changed the following line in HttpTriggerFunction.cs:

return req.CreateResponse(HttpStatusCode.OK,stateResponse.EntityState);

to

 return req.CreateResponse(HttpStatusCode.OK,stateResponse.EntityState + "");

It can run and return the required string. But what I want to return is Json string.

So what can I do to fix my code? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2025-02-15 06:14:51

您可以做到序列化对象并进行编码,然后发送。

因此,代替createresponse返回httpresponsemessage

首先,您需要添加诸如:

using system.text;

using NewtonSoft.Json;

然后您可以返回的软件包:

return new HttpResponseMessage(HttpStatusCode.OK){Content = new StringContent(JsonConvert.SerializeOject(stateResponse.EntityState , Formating.Indented),Encoding.UTF8,"Response.json")}:

请参阅以下文章 callon campbell

参考以下文档

You can do is serialize the object and encode it and then send it.

So instead of createResponse return a HttpResponseMessage.

First you need to add packages such as:

using system.text;

using NewtonSoft.Json;

Then you can return :

return new HttpResponseMessage(HttpStatusCode.OK){Content = new StringContent(JsonConvert.SerializeOject(stateResponse.EntityState , Formating.Indented),Encoding.UTF8,"Response.json")}:

Refer the following article by Callon Campbell

Refer the following documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文