LoginSignup
1
0

named-pipe-wrapper .NET8

Last updated at Posted at 2024-03-26

Changes in NamedPipeWrapper Project

Ver 2.0 Master History

To port from .NET Framework 4.0 to .NET 8, change the following files.

Modify 3 files

  • NamedPipeServer.cs
  • PipeStreamWriter.cs
  • PipeStreamReader.cs

Add 1 file

  • Globals.cs

NamedPipeServer.cs

Before

public static NamedPipeServerStream CreatePipe(string pipeName, PipeSecurity pipeSecurity)
{
    return new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 0, 0, pipeSecurity);
}

After

public static NamedPipeServerStream CreatePipe(string pipeName, PipeSecurity pipeSecurity)
{
    var stream = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 0, 0);
    if (pipeSecurity is not null) stream.SetAccessControl(pipeSecurity); //Windows Only
    return stream;
}

PipeStreamWriter.cs

Before

private readonly BinaryFormatter _binaryFormatter = new BinaryFormatter();

try
{
    using (var memoryStream = new MemoryStream())
    {
        _binaryFormatter.Serialize(memoryStream, obj);
        return memoryStream.ToArray();
    }
}

After

try
{
    var byteArray = JsonSerializer.SerializeToUtf8Bytes(obj, Globals.JsonOptions);
    return byteArray;
}

PipeStreamReader.cs

Before

private readonly BinaryFormatter _binaryFormatter = new BinaryFormatter();

using (var memoryStream = new MemoryStream(data))
{
    return (T) _binaryFormatter.Deserialize(memoryStream);
}

After

using (var memoryStream = new MemoryStream(data))
{
    var obj = JsonSerializer.Deserialize<T>(memoryStream, Globals.JsonOptions);
    return obj;
}    

Globals.cs

Add Globals

namespace NamedPipeWrapper
{
    public static class Globals
    {
        public static readonly JsonSerializerOptions JsonOptions = new()
        {
            ReferenceHandler = ReferenceHandler.Preserve,  //cyclical references OK
        };
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0