C# 为什么文件和文件流API在Ubuntu上写二进制数据时没有抛出异常?
我有一个应用程序,我试图将.NET Core 2.1和.NET Core 3.1作为构建的目标,但仍然存在这个问题。我正在复制从我们的API(一个Ubuntu 18.04.4 VM)到存储服务器(也是一个Ubuntu VM)的文件流处理过程,只是它从存储到我们的OCR服务(第三个Ubuntu VM)。Ubuntu虚拟机都安装了最新版本的.NET内核。这个文件流处理在一个自制的“文件服务”协议上工作,我不想详细解释这个协议,但只要说有一条“Create”消息就足够了,它告诉接收者创建一个文件句柄并写入第一个数据包(数据包高达64 kB),并且在接收到这个消息时“创建”消息,消费者被初始化,它将文件数据的其余部分作为独立队列中的“附加”消息使用,直到收到“关闭”消息(不包含有效负载)。在我实际尝试对OCR上接收的文件执行基本操作之前,这似乎效果很好(即,从PDF文档中拆分页面)并意识到文件已损坏。凭直觉,我检查了OCR服务器上的文件大小,确实比原始文件小64 kB。管道中API到存储部分从未出现过此问题。我检查了存储服务器,以确保从API传输数据时不损坏数据通过下载它 根据另一种预感,我决定注释掉将Append消息有效载荷写入文件的代码,以确保丢失的数据包不是原始的Create有效载荷。我是正确的,仅处理Create消息后,文件为0字节。我检查了C# 为什么文件和文件流API在Ubuntu上写二进制数据时没有抛出异常?,c#,linux,.net-core,io,ubuntu-18.04,C#,Linux,.net Core,Io,Ubuntu 18.04,我有一个应用程序,我试图将.NET Core 2.1和.NET Core 3.1作为构建的目标,但仍然存在这个问题。我正在复制从我们的API(一个Ubuntu 18.04.4 VM)到存储服务器(也是一个Ubuntu VM)的文件流处理过程,只是它从存储到我们的OCR服务(第三个Ubuntu VM)。Ubuntu虚拟机都安装了最新版本的.NET内核。这个文件流处理在一个自制的“文件服务”协议上工作,我不想详细解释这个协议,但只要说有一条“Create”消息就足够了,它告诉接收者创建一个文件句柄并
public FileServiceResponseEnvelope Create(FileServiceMessageEnvelope message, string filePath)
{
var response = CreateResponse(message);
var payload = SerializationHelper.DeserializeObject<SecureMessage>(message.SignedPayload).Payload;
if (!File.Exists(filePath))
{
// 23 is enough for the first 8 bytes and the hyphens separating them.
_logger.LogStatement($"Creating new file at [{filePath}] while appending [{payload.Length} bytes] and hash that starts with {BitConverter.ToString(payload).Substring(0, 23)}...", LogLevel.Trace, nameof(Create));
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
File.WriteAllBytes(filePath, payload);
}
else
{
var error = $"File at [{filePath}] already exists and cannot be created.";
response.ErrorMessage = error;
response.Success = false;
throw new InvalidOperationException(error);
}
return response;
}
public FileServiceResponseEnvelope Create(FileServiceMessageEnvelope message, string filePath)
{
var response = CreateResponse(message);
var payload = SerializationHelper.DeserializeObject<SecureMessage>(message.SignedPayload).Payload;
if (!File.Exists(filePath))
{
// 23 is enough for the first 8 bytes and the hyphens separating them.
_logger.LogStatement($"Creating new file at [{filePath}] while appending [{payload.Length} bytes] and hash that starts with {BitConverter.ToString(payload).Substring(0, 23)}...", LogLevel.Trace, nameof(Create));
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite))
{
stream.Seek(0, SeekOrigin.Begin);
stream.Write(payload, 0, message.Count);
}
}
else
{
var error = $"File at [{filePath}] already exists and cannot be created.";
response.ErrorMessage = error;
response.Success = false;
throw new InvalidOperationException(error);
}
return response;
}
public fileservicesponseenvelope创建(fileservicesessageenvelope消息,字符串filePath)
{
var response=CreateResponse(消息);
var payload=SerializationHelper.DeserializeObject(message.SignedPayload).payload;
如果(!File.Exists(filePath))
{
//23对于前8个字节和分隔它们的连字符来说已经足够了。
_logger.LogStatement($“在[{filePath}]处创建新文件,同时追加[{payload.Length}字节]和以{BitConverter.ToString(payload.ToString(0,23)}开头的散列…”,LogLevel.Trace,nameof(Create));
CreateDirectory(Path.GetDirectoryName(filePath));
writealBytes(文件路径、有效负载);
}
其他的
{
var error=$“位于[{filePath}]的文件已存在,无法创建。”;
response.ErrorMessage=错误;
回答:成功=错误;
抛出新的InvalidOperationException(错误);
}
返回响应;
}
文件流API
public FileServiceResponseEnvelope Create(FileServiceMessageEnvelope message, string filePath)
{
var response = CreateResponse(message);
var payload = SerializationHelper.DeserializeObject<SecureMessage>(message.SignedPayload).Payload;
if (!File.Exists(filePath))
{
// 23 is enough for the first 8 bytes and the hyphens separating them.
_logger.LogStatement($"Creating new file at [{filePath}] while appending [{payload.Length} bytes] and hash that starts with {BitConverter.ToString(payload).Substring(0, 23)}...", LogLevel.Trace, nameof(Create));
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
File.WriteAllBytes(filePath, payload);
}
else
{
var error = $"File at [{filePath}] already exists and cannot be created.";
response.ErrorMessage = error;
response.Success = false;
throw new InvalidOperationException(error);
}
return response;
}
public FileServiceResponseEnvelope Create(FileServiceMessageEnvelope message, string filePath)
{
var response = CreateResponse(message);
var payload = SerializationHelper.DeserializeObject<SecureMessage>(message.SignedPayload).Payload;
if (!File.Exists(filePath))
{
// 23 is enough for the first 8 bytes and the hyphens separating them.
_logger.LogStatement($"Creating new file at [{filePath}] while appending [{payload.Length} bytes] and hash that starts with {BitConverter.ToString(payload).Substring(0, 23)}...", LogLevel.Trace, nameof(Create));
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite))
{
stream.Seek(0, SeekOrigin.Begin);
stream.Write(payload, 0, message.Count);
}
}
else
{
var error = $"File at [{filePath}] already exists and cannot be created.";
response.ErrorMessage = error;
response.Success = false;
throw new InvalidOperationException(error);
}
return response;
}
public fileservicesponseenvelope创建(fileservicesessageenvelope消息,字符串filePath)
{
var response=CreateResponse(消息);
var payload=SerializationHelper.DeserializeObject(message.SignedPayload).payload;
如果(!File.Exists(filePath))
{
//23对于前8个字节和分隔它们的连字符来说已经足够了。
_logger.LogStatement($“在[{filePath}]处创建新文件,同时追加[{payload.Length}字节]和以{BitConverter.ToString(payload.ToString(0,23)}开头的散列…”,LogLevel.Trace,nameof(Create));
CreateDirectory(Path.GetDirectoryName(filePath));
使用(var stream=newfilestream(filePath,FileMode.CreateNew,FileAccess.Write,FileShare.ReadWrite))
{
stream.Seek(0,SeekOrigin.Begin);
stream.Write(有效负载,0,message.Count);
}
}
其他的
{
var error=$“位于[{filePath}]的文件已存在,无法创建。”;
response.ErrorMessage=错误;
回答:成功=错误;
抛出新的InvalidOperationException(错误);
}
返回响应;
}
使用WriteByte的FileStream API
public FileServiceResponseEnvelope Create(FileServiceMessageEnvelope message, string filePath)
{
var response = CreateResponse(message);
var payload = SerializationHelper.DeserializeObject<SecureMessage>(message.SignedPayload).Payload;
if (!File.Exists(filePath))
{
// 23 is enough for the first 8 bytes and the hyphens separating them.
_logger.LogStatement($"Creating new file at [{filePath}] while appending [{payload.Length} bytes] and hash that starts with {BitConverter.ToString(payload).Substring(0, 23)}...", LogLevel.Trace, nameof(Create));
Directory.CreateDirectory(Path.GetDirectoryName(filePath));;
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite))
{
stream.Seek(message.Position, SeekOrigin.Begin);
foreach (byte b in payload)
{
_logger.LogStatement($"Writing [{b}] to file...", LogLevel.Trace, nameof(Create));
stream.WriteByte(b);
}
}
}
else
{
var error = $"File at [{filePath}] already exists and cannot be created.";
response.ErrorMessage = error;
response.Success = false;
throw new InvalidOperationException(error);
}
return response;
}
public fileservicesponseenvelope创建(fileservicesessageenvelope消息,字符串filePath)
{
var response=CreateResponse(消息);
var payload=SerializationHelper.DeserializeObject(message.SignedPayload).payload;
如果(!File.Exists(filePath))
{
//23对于前8个字节和分隔它们的连字符来说已经足够了。
_logger.LogStatement($“在[{filePath}]处创建新文件,同时追加[{payload.Length}字节]和以{BitConverter.ToString(payload.ToString(0,23)}开头的散列…”,LogLevel.Trace,nameof(Create));
CreateDirectory(Path.GetDirectoryName(filePath));;
使用(var stream=newfilestream(filePath,FileMode.CreateNew,FileAccess.Write,FileShare.ReadWrite))
{
stream.Seek(message.Position,SeekOrigin.Begin);
foreach(有效负载中的字节b)
{
_logger.LogStatement