Hello Friends!!!! Nowadays most of the applications are built with the latest tech stack, but at the same time, those applications want to communicate with the legacy applications.
In this article, I am going to explain how to make communication between legacy Winforms (builds using .NET framework) application with a Modern ReactJS web application using SingalR.
SignalR is an open-source library for Microsoft ASP.NET that allows servers to send asynchronous messages to client-side web applications and it includes server-side and client-side JavaScript components.
Demo - Simple Chat Application
The chat application contains three parts,
- Winform application (.NET Framework 4.5)
- SignalR Hub (.NET Framework 4.5.2)
- React JS application
Winform Application
Create a new project with Windows Form App (.NET Framework).
Select Framework is .NET Framework 4.5.
Create simple chat form same as below,
Add below code for connecting the SingalR Hub.
Add the below code to send the message to the ReactJS application.
SignalR Hub
Add a new ASP.NET Web Application(.NET Framework) project in the solution with the name of SignalR_Hub. If you want, create a separate solution for SignalR Hub.
Add reference "Microsoft.ASPNet.SignalR.Core" using NuGet manager for SignalR_Hub project.
Create "ChatHub.cs" class.
Add below methods to add and remove the Connection Ids during connecting and disconnect the clients from SignarR Hub.
Add below the method for sending messages to the WinForm application. Here "SendMessageToWinForm" is a dynamic object which is subscribed to in the Winform Chat application.
Add below the method for sending messages to Web applications. Here "SendMessageToWebApp" is a dynamic object which is subscribed in the ReactJS Web application.
In the above two methods, "Clients.All" means send a message to all connected clients with the SignalR hub. If you want to send a message to a particular client then use Clients.Client(<conectionId of the client>).
Add below code in Web.config file to avoid CORS issue from web application. Here "Access-Control-Allow-Origin" value is "http://localhost:3000" because my ReactJS web application is running in "http://localhost:3000".
Create your own "Startup.cs" class and add the below code to map and run the signal hub.
If you want to add any authorization, then create a custom AuthorizeAttribute handler (example: SignalRAuthorizeAttribute) for validating the bearer token and add it to HubPipeline in the Startup class. I didn't use any Authorization in my demo project.
Add "SignalRAuthorize" to your "ChatHub' class.
Note the SignalR hub running URL from the Project properties. Here, my SignalR hub service is running in "http://localhost:57142/".
ReactJS Web Application
Create an empty react application. Please refer to the link to know about creating the reactjs application. Here, I am using JS and Hooks concepts in my reactjs application and used the VS Code editor for web application development.
Add "jquery-3.4.1.min.js" and "jquery.signalR-2.4.2.min.js". Download the js files from the below link,
Add reference of above two files in the index.html file using <script> tag.
Modify the App.js file with the below code. There are 3 functions in the App.js file.
- Connect() - Connect the SignalR hub
- Send() - Send the message to the Winform Application
- messageChange() - Store the textbox value into the local state.
How to Run
- In the .NET solution, Go to Debug -> Set Startup Projects -> Common Properties -> Startup Project,
- Select Multiple startup projects check box
- Change Action to Start for both project
Run the .NET solution. The SignalR Hub and Winform chat application will run.
Run the React web application using the "npm run start" command.
Click the "Connect" button in a WinForm application to connect the SignalR Hub. Once connected to the SignalR hub, will get the "Connected Successfully" message box.
Click the "Connect" button in a Web application.
In the WinForm application, type a message in the text box and click send button. The Web application will receive the message. You can see this in the "Chat History" text box.
Same as above, you can send messages from react application also.
SignalR hub can be hosted in the below ways,
- Cloud
- On-premises network
- Localhost in the user machine
- Windows service in the user machine
In this article, I discussed How to make communication between legacy .NET applications with a modern ReactJS Web application using SignalR. Hope you liked it.
If you have any doubts or comments about this, please let me know in the comments.
No comments:
Post a Comment