Azure Functions 中的 Switch 表达式导致异常
Switch expression in an Azure Function causes an exception
编写 C# Azure 函数并尝试使用 C#8 开关表达式。
根据文档,https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.switchexpressionexception?view=netcore-3.1,他们表明这应该适用于 .net core 3.1
这段使用 switch 表达式的代码不起作用:
try
{
var data = (JObject)eventGridEvent.Data;
IDHT22 sensor = eventGridEvent.EventType switch
{
"TemperatureChangedEvent" => new TemperatureSensorEvent(data["sensor_id"].Value<string>(), data["name"].Value<string>(), data["temperature_c"].Value<double>()),
"HumidityChangedEvent" => new HumiditySensorEvent(data["sensor_id"].Value<string>(), data["name"].Value<string>(), data["humidity"].Value<double>())
};
if (sensor != null)
await sensors.AddAsync(sensor);
}
catch (System.Exception ex)
{
throw new InvalidOperationException(ex.Message, ex);
}
执行时会出现此错误消息:
[4/30/2020 7:21:06 PM] Executed 'SaveSensorEvent' (Failed, Id=8db720e1-aa19-4f53-b102-3aaa83f19667)
[4/30/2020 7:21:06 PM] System.Private.CoreLib: Exception while executing function: SaveSensorEvent. > SensorData: Could not load type 'System.Runtime.CompilerServices.SwitchExpressionException' from assembly 'System.Runtime.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
如果我使用传统开关,一切都很好。看来我可能需要更新 Runtime.Extensions - 虽然不确定如何在 .net 核心应用程序中执行此操作。
有什么想法吗?
显然,您的代码中缺少默认大小写。
它应该是这样的:
IDHT22 sensor = eventGridEvent.EventType switch
{
"TemperatureChangedEvent" => new TemperatureSensorEvent(data["sensor_id"].Value<string>(), data["name"].Value<string>(), data["temperature_c"].Value<double>()),
"HumidityChangedEvent" => new HumiditySensorEvent(data["sensor_id"].Value<string>(), data["name"].Value<string>(), data["humidity"].Value<double>()),
_ => balabala
};
编写 C# Azure 函数并尝试使用 C#8 开关表达式。
根据文档,https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.switchexpressionexception?view=netcore-3.1,他们表明这应该适用于 .net core 3.1
这段使用 switch 表达式的代码不起作用:
try
{
var data = (JObject)eventGridEvent.Data;
IDHT22 sensor = eventGridEvent.EventType switch
{
"TemperatureChangedEvent" => new TemperatureSensorEvent(data["sensor_id"].Value<string>(), data["name"].Value<string>(), data["temperature_c"].Value<double>()),
"HumidityChangedEvent" => new HumiditySensorEvent(data["sensor_id"].Value<string>(), data["name"].Value<string>(), data["humidity"].Value<double>())
};
if (sensor != null)
await sensors.AddAsync(sensor);
}
catch (System.Exception ex)
{
throw new InvalidOperationException(ex.Message, ex);
}
执行时会出现此错误消息:
[4/30/2020 7:21:06 PM] Executed 'SaveSensorEvent' (Failed, Id=8db720e1-aa19-4f53-b102-3aaa83f19667) [4/30/2020 7:21:06 PM] System.Private.CoreLib: Exception while executing function: SaveSensorEvent. > SensorData: Could not load type 'System.Runtime.CompilerServices.SwitchExpressionException' from assembly 'System.Runtime.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
如果我使用传统开关,一切都很好。看来我可能需要更新 Runtime.Extensions - 虽然不确定如何在 .net 核心应用程序中执行此操作。
有什么想法吗?
显然,您的代码中缺少默认大小写。
它应该是这样的:
IDHT22 sensor = eventGridEvent.EventType switch
{
"TemperatureChangedEvent" => new TemperatureSensorEvent(data["sensor_id"].Value<string>(), data["name"].Value<string>(), data["temperature_c"].Value<double>()),
"HumidityChangedEvent" => new HumiditySensorEvent(data["sensor_id"].Value<string>(), data["name"].Value<string>(), data["humidity"].Value<double>()),
_ => balabala
};