Comments (9)
Hm, this is possible bug with SystemTextJsonFormatter
.
If I install H.Formatters.Newtonsoft.Json
and change SystemTextJsonFormatter
to NewtonsoftJsonFormatter
, everything start working. Console log: Received: THIS IS TEST MESSAGE
.
Well, I really lost a lot of time with this.
from h.pipes.
Thanks for the problem. I'll check. I want to note that be sure to call Dispose before closing the program / ending the use of PipeServer/PipeClient, otherwise you may have problems after restarting. Pipes are the resource of the entire system and must be properly cleaned after use.
from h.pipes.
I want to note that be sure to call Dispose before closing the program
Hi @HavenDV
thank you for this, I didn't really realize that. It would undoubtedly be a good idea to add this information to the readme.
from h.pipes.
It would undoubtedly be a good idea to add this information to the readme.
Updated README, added Notes block to Usage
from h.pipes.
I can't repeat it in tests
Thanks, I applied to the code everything I understood. This article https://devblogs.microsoft.com/dotnet/configureawait-faq/ requires a more advanced programmer than me 😄
Your test has probably been simplified to a level where serialization is not used as with a normal object.
Modified version of unit test:
namespace HPipesTests
{
public class MyMessage
{
public string Text;
}
[TestClass]
public class SystemTextJsonFm
{
[TestMethod]
public async Task MessageString()
{
const string pipeName = "random_751902396";
const string message = "THIS IS TEST MESSAGE";
var receivedMessage = string.Empty;
var server = new PipeServer<string>(pipeName, formatter: new SystemTextJsonFormatter());
server.MessageReceived += (c, args) =>
{
Console.WriteLine($"Received: {args.Message}");
receivedMessage = args.Message;
};
var startTask = server.StartAsync();
var client = new PipeClient<string>(pipeName, formatter: new SystemTextJsonFormatter());
client.Connected += async (o, args) =>
{
Console.WriteLine($"Sending: {message}");
await client.WriteAsync(message);
Console.WriteLine($"Sent: {message}");
};
await client.ConnectAsync();
await startTask;
await Task.Delay(TimeSpan.FromMilliseconds(100));
Assert.AreEqual(message, receivedMessage);
}
[TestMethod]
public async Task MessageClass()
{
const string pipeName = "random_54821002374";
const string message = "THIS IS TEST MESSAGE";
var receivedMessage = string.Empty;
var server = new PipeServer<MyMessage>(pipeName, formatter: new SystemTextJsonFormatter());
server.MessageReceived += (c, args) =>
{
Console.WriteLine($"Received: {args.Message.Text}");
receivedMessage = args.Message.Text;
};
var startTask = server.StartAsync();
var client = new PipeClient<MyMessage>(pipeName, formatter: new SystemTextJsonFormatter());
client.Connected += async (o, args) =>
{
Console.WriteLine($"Sending: {message}");
await client.WriteAsync(new MyMessage() { Text = message });
Console.WriteLine($"Sent: {message}");
};
await client.ConnectAsync();
await startTask;
await Task.Delay(TimeSpan.FromMilliseconds(100));
Assert.AreEqual(message, receivedMessage);
}
}
}
Result: 1 Passed / 1 Failed
from h.pipes.
Thanks, you're right, the problem started when I used your MyMessage
class.
https://stackoverflow.com/questions/58139759/how-to-use-class-fields-with-system-text-json-jsonserializer
It is described in detail here. You just need to use the property.
public class MyMessage
{
public string Text { get; set; }
}
There is also an option here to add JsonSerializerOptions { IncludeFields = true } to the SystemTextJsonFormatter. I need to understand what caused the refusal to serialize fields in the System.Text.Json library in order to make this decision.
from h.pipes.
For now, I've just added the ability to customize the SystemTextJsonFormatter via the Options property:
var server = new PipeServer<MyMessage>(pipeName, formatter: new SystemTextJsonFormatter { Options = { IncludeFields = true } });
from h.pipes.
I can't repeat it in tests:
[TestMethod]
public async Task ParallelTest()
{
const string pipeName = "absolutely_random_name";
const string message = "THIS IS TEST MESSAGE";
var receivedMessage = string.Empty;
var server = new PipeServer<string>(pipeName, formatter: new SystemTextJsonFormatter());
server.MessageReceived += (c, args) =>
{
Console.WriteLine($"Received: {args.Message}");
receivedMessage = args.Message;
};
var startTask = server.StartAsync();
var client = new PipeClient<string>(pipeName, formatter: new SystemTextJsonFormatter());
client.Connected += async (o, args) =>
{
Console.WriteLine($"Sending: {message}");
await client.WriteAsync(message);
Console.WriteLine($"Sent: {message}");
};
await client.ConnectAsync();
await startTask;
await Task.Delay(TimeSpan.FromMilliseconds(100));
Assert.AreEqual(message, receivedMessage);
}
It works correctly.
I want to note that there are some peculiarities here when using asynchronous methods in Windows Forms. If you don't use ConfigureAwait(false), the default scheduler will be the UI scheduler. This can cause various problems with the initialization sequence.
Look towards using async void which contains try/catch inside. Also read this - https://devblogs.microsoft.com/dotnet/configureawait-faq/
from h.pipes.
Also try using Task.Run in general when creating the PipeServer, it might help.
from h.pipes.
Related Issues (20)
- some windows 10 and windows 11 clients can't connect to the PipeServer if hosted in a service HOT 17
- Dependencies Inferno on will fail if not on windows and on supported .net versions HOT 15
- Interesting Bug...version 2.0.34 HOT 6
- ReadAsync length HOT 4
- Generic message type <T> HOT 4
- Returned by server pipeName is null HOT 2
- Large messages on .NET 6 fail to send HOT 9
- PipeServer crashing with many connections, IOException: 'Pipe is broken' HOT 13
- SemaphoreFullException when CancellationToken is cancelled before semaphore is entered
- Feature request: Have a connection identifier on the server based on a client connection HOT 3
- Application getting terminated HOT 5
- The server stops working HOT 8
- Feature request: Support CreatePipeStreamFunc on the client as well
- Handles are not disposed HOT 1
- Implementation questions HOT 1
- Tests fail sometimes in Github Actions only HOT 1
- Identify which user is connected HOT 8
- Missing resiliency options HOT 14
- Pipe name handling on client side HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from h.pipes.