Code Monkey home page Code Monkey logo

Comments (9)

atiris avatar atiris commented on May 26, 2024 1

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.

HavenDV avatar HavenDV commented on May 26, 2024 1

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.

atiris avatar atiris commented on May 26, 2024 1

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.

HavenDV avatar HavenDV commented on May 26, 2024 1

It would undoubtedly be a good idea to add this information to the readme.

Updated README, added Notes block to Usage

from h.pipes.

atiris avatar atiris commented on May 26, 2024 1

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.

HavenDV avatar HavenDV commented on May 26, 2024 1

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.

HavenDV avatar HavenDV commented on May 26, 2024 1

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.

HavenDV avatar HavenDV commented on May 26, 2024

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.

HavenDV avatar HavenDV commented on May 26, 2024

Also try using Task.Run in general when creating the PipeServer, it might help.

from h.pipes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.