Changes in NamedPipeWrapper Project
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
};
}
}