Azure SignalR未生效SignalROptions驼峰配置,本地与云端表现不一致
SignalR本地与Azure部署后序列化命名格式不一致
现象
- 本地运行时,SignalR发送的消息属性为驼峰命名,示例:
{"type":1,"target":"questions","arguments":[[{"id":"1","text":"Test","columnId":"1"}]]}
- 部署到Azure后,消息属性自动变为帕斯卡命名,示例:
{"type":1,"target":"questions","arguments":[[{"Id":"1","Text":"Test","ColumnId":"1"}]]}
已配置内容
我已在Startup.cs中配置SignalR序列化规则为驼峰命名:
builder.Services.Configure<SignalROptions>(o => o.JsonObjectSerializer = new JsonObjectSerializer( new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));
补充代码
SignalR消息发送的调用代码如下(GetAllColumnsMapped用于映射数据库查询结果):
var payload = await GetAllColumnsMapped(tenantId, questions); await signalRMessages.AddAsync(new SignalRMessage {Target = "columns", Arguments = new object[] {payload}});
解决办法
仅配置SignalROptions无法覆盖Azure Functions绑定的序列化逻辑,需要额外配置Functions全局JSON序列化规则:
- 若使用System.Text.Json,在
Startup.cs中添加:
builder.Services.Configure<JsonSerializerOptions>(options => { options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; });
- 若使用Newtonsoft.Json(旧版Functions默认),则配置:
builder.Services.AddMvc().AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); });
同时检查GetAllColumnsMapped返回的实体类,确保没有通过[JsonPropertyName("Id")]这类特性硬编码属性名称,如有需调整为驼峰格式或移除该特性。
内容的提问来源于stack exchange,提问作者dothem
相关产品推荐
相关产品推荐

