当将HTTP函数触发到查询实体状态时,程序崩溃了吗?
我是Azure功能和耐用实体的首发。我想做以下操作:
创建一个耐用的实体,它具有称为SystemList的状态;
创建一个队列触发函数以在我的队列中消耗消息并将其存储在我刚创建的实体状态中。
创建一个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:
Create a durable entity and it has a state called systemList;
Create a queue trigger function to consume messages in my queue and store them in the entity state I just created.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做到序列化对象并进行编码,然后发送。
因此,代替
createresponse
返回httpresponsemessage
。首先,您需要添加诸如:
然后您可以返回的软件包:
请参阅以下文章 callon campbell
参考以下文档。
You can do is serialize the object and encode it and then send it.
So instead of
createResponse
return aHttpResponseMessage
.First you need to add packages such as:
Then you can return :
Refer the following article by Callon Campbell
Refer the following documentation.