-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I can't transmit a simple message [question] #13
Comments
Hm, this is possible bug with |
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. |
Hi @HavenDV |
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. |
Updated README, added Notes block to Usage |
Also try using Task.Run in general when creating the PipeServer, it might help. |
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. 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 |
Thanks, you're right, the problem started when I used your https://stackoverflow.com/questions/58139759/how-to-use-class-fields-with-system-text-json-jsonserializer 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. |
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 } }); |
I'm having trouble moving a simple message (my first step with this library).
This is a minimal new .NET Framework 4.7.2 project (Desktop Application) with nothing only Load event for form.
I only added libraries: H.Pipes and H.Formatters.System.Text.Json
This is the entire form code.
Console output is like this (message.Text is always empty):
I get the same problem even with two separated client-server applications.
Please advise me where I am making a mistake?
The text was updated successfully, but these errors were encountered: