Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么文件和文件流API在Ubuntu上写二进制数据时没有抛出异常?_C#_Linux_.net Core_Io_Ubuntu 18.04 - Fatal编程技术网

C# 为什么文件和文件流API在Ubuntu上写二进制数据时没有抛出异常?

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”消息就足够了,它告诉接收者创建一个文件句柄并

我有一个应用程序,我试图将.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字节。我检查了的长度ong>创建有效负载,并将其作为哈希打印出来,以确保它不是出于任何原因都是0——没有问题。但是,每次我将字节写入实际文件时,都不会写入任何数据,也不会抛出任何错误。文件句柄和目录是以0字节的大小创建的,所有日志消息都会打印出来ed.我现在将发布我尝试过的所有不同方式,将最初的64 kB数据写入文件:

文件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));
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