C# I want to be able to send data to Windows form applications

Asked 1 years ago, Updated 1 years ago, 72 views

I created a NodeJS server on my W10 PC, but I would like to send the data from there to a printing application (on the same PC) created by the C# Windows form application. What should I do?

My idea is to have the printing app have HTTP server function and send the data from NodeJS server via POST communication. I looked into some of these sites, but it was difficult for me and I could only understand the content in fragments.
 Please let me know if there are any sites or technical information that beginners (visualstudio/c# study period is about half a year) can understand.

c# http

2022-09-30 17:28

2 Answers

As a way to deliver data to Windows form applications, other than HTTP communication, how about creating an FTP server or shared folder and passing the file over?

Node.js app -->FTP server/shared folder -->Windows form app

Another variation is that you can query the Node.js app from the Windows form app for new files.The Node.js app at this time is an image that returns a file as an HTTP response when there is a file, or an HTTP code 404 when there is no file.

Windows form app-->(POST or GET)-->Node.js app

Both of the ideas that I've come up with this time, the Windows form app periodically checks for new files.If you check more frequently, it will be real-time-style, but if you get too many inquiries, FTP servers, shared folders, and Node.js apps may not be able to handle the load.If you want to try this idea, I recommend that you check it slowly.

Note:
Both applications are running on the same computer.
If that's the case, I think I can exchange files with local folders instead of FTP servers or shared folders.


2022-09-30 17:28

If you use the [self-host] method of ASP.NET Core HTTP server in the Windows application,
You will be able to receive HTTP POST data.

The components used for .NET Framework 4 and .NET Core are slightly different.

.NET Framework 4 or later

https://docs.microsoft.com/en-us/aspnet/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

I referred to the .

Add Microsoft.AspNet.WebApi.OwinSelfHostnuget package

Program.cs

static class program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STATHREAD]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string baseAddress="http://localhost:9000/";

            // Start OWIN host 
            using(WebApp.Start<Startup>(url:baseAddress))
            {
                // Create HttpClient and make a request to api/values 
                HttpClient client = new HttpClient();

                var response=client.GetAsync(baseAddress+"api/values").Result;

                Console.WriteLine(response);
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);

                mainForm = newForm1();
                Application.Run(mainForm);
            }
        }

        public static Form 1 mainForm;
    }

    public class startup
    {
        // This code configurations Web API.The Startup class is specified as a type
        // parameter in the WebApp.Start method.
        public void configuration (IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults:new {id=RouteParameter.Optional}
            );

            appBuilder.UseWebApi(config);
        }
    }

    public class ValuesController:ApiController
    {
        // GET api/values/5 
        public string Get (string name)
        {
            Program.mainForm.InvokeAddMessage("get"+name);
            return "hello" + name;
        }

        // POST api/values 
        public void Post (FromBody) US>string value)
        {
            Program.mainForm.InvokeAddMessage(value);
        }
    }

Rewrite
Visit Form1.cs to

public void InvokeAddMessage (string value)
        {
            if(this.InvokeRequired)
            {
                This.Invoke((MethodInvoker)delegate {InvokeAddMessage(value);});
            }
            else
            {
                this.textBox1.Text=value;
            }
        }

Add a method

http://localhost:9000/api/Values/test

Open to Windows Form

in the Windows Form

hello test

appears.

For

.NET Core 5.0

https://github.com/tonysneed/Demo.DotNetSelfHost

You can use the sample in to create it.


2022-09-30 17:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.