更新
这是fixed in next release (5.0.0-preview4)。
原答案
我测试了float 和double,有趣的是,在这种特殊情况下,只有double 有问题,而float 似乎工作正常(即在服务器上读取0.005)。
检查消息字节表明 0.005 作为 Float32Double 类型发送,这是一个 4 字节/32 位 IEEE 754 单精度浮点数,尽管 Number 是 64 位浮点数。
在控制台运行以下代码确认上述情况:
msgpack5().encode(Number(0.005))
// Output
Uint8Array(5) [202, 59, 163, 215, 10]
mspack5 确实提供了一个强制 64 位浮点的选项:
msgpack5({forceFloat64:true}).encode(Number(0.005))
// Output
Uint8Array(9) [203, 63, 116, 122, 225, 71, 174, 20, 123]
但是,signalr-protocol-msgpack 不使用forceFloat64 选项。
虽然这解释了为什么float 在服务器端工作,but there isn't really a fix for that as of now。让我们拭目以待Microsoft says。
可能的解决方法
TL;DR
JS 客户端向 C# 后端发送单个浮点数的问题导致了一个已知的浮点问题:
// value = 0.00499999988824129, crazy C# :)
var value = (double)0.005f;
对于在方法中直接使用double,该问题可以通过自定义MessagePack.IFormatterResolver来解决:
public class MyDoubleFormatterResolver : IFormatterResolver
{
public static MyDoubleFormatterResolver Instance = new MyDoubleFormatterResolver();
private MyDoubleFormatterResolver()
{ }
public IMessagePackFormatter<T> GetFormatter<T>()
{
return MyDoubleFormatter.Instance as IMessagePackFormatter<T>;
}
}
public sealed class MyDoubleFormatter : IMessagePackFormatter<double>, IMessagePackFormatter
{
public static readonly MyDoubleFormatter Instance = new MyDoubleFormatter();
private MyDoubleFormatter()
{
}
public int Serialize(
ref byte[] bytes,
int offset,
double value,
IFormatterResolver formatterResolver)
{
return MessagePackBinary.WriteDouble(ref bytes, offset, value);
}
public double Deserialize(
byte[] bytes,
int offset,
IFormatterResolver formatterResolver,
out int readSize)
{
double value;
if (bytes[offset] == 0xca)
{
// 4 bytes single
// cast to decimal then double will fix precision issue
value = (double)(decimal)MessagePackBinary.ReadSingle(bytes, offset, out readSize);
return value;
}
value = MessagePackBinary.ReadDouble(bytes, offset, out readSize);
return value;
}
}
并使用解析器:
services.AddSignalR()
.AddMessagePackProtocol(options =>
{
options.FormatterResolvers = new List<MessagePack.IFormatterResolver>()
{
MyDoubleFormatterResolver.Instance,
ContractlessStandardResolver.Instance,
};
});
解析器并不完美,因为先转换为 decimal 然后再转换为 double 会减慢处理速度,而 it could be dangerous。
但是
根据 cmets 中指出的 OP,如果使用具有 double 返回属性的复杂类型,这无法解决问题。
进一步调查揭示了 MessagePack-CSharp 中问题的原因:
// Type: MessagePack.MessagePackBinary
// Assembly: MessagePack, Version=1.9.0.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be
// MVID: B72E7BA0-FA95-4EB9-9083-858959938BCE
// Assembly location: ...\.nuget\packages\messagepack\1.9.11\lib\netstandard2.0\MessagePack.dll
namespace MessagePack.Decoders
{
internal sealed class Float32Double : IDoubleDecoder
{
internal static readonly IDoubleDecoder Instance = (IDoubleDecoder) new Float32Double();
private Float32Double()
{
}
public double Read(byte[] bytes, int offset, out int readSize)
{
readSize = 5;
// The problem is here
// Cast a float value to double like this causes precision loss
return (double) new Float32Bits(bytes, checked (offset + 1)).Value;
}
}
}
当需要将单个float数字转换为double时使用上述解码器:
// From MessagePackBinary class
MessagePackBinary.doubleDecoders[202] = Float32Double.Instance;
v2
MessagePack-CSharp v2 版本中存在此问题。我已经提交了an issue on github、though the issue is not going to be fixed。