Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core Application is run with different port with electron.net with cross platrom

I've developed an application using .NET Core and Electron.NET and created a login form.

For login, I have created separate web API project and call the login API on login button click.

When I call API from an application, it gives an error about "Cross-Origin", so I need to register an IP Address and Port in that API but right now I am facing an issue like .net core application is run on the different-different port each time.

While call sign-in in API from the window environment it gives me below port:8001

While calling the same API from the Ubuntu it gives me port 35941. So now I am facing an issue like, we have the different project for web API and it allows us to call web API on the specific port but due to each time different port generated by the electron.net, we can not call the web API in CORS (cross-origin) and it throws an error.

How can I opt-out with this situation?

like image 836
Chetan Sanghani Avatar asked Jul 28 '18 10:07

Chetan Sanghani


People also ask

How can I change port number in asp net core?

To change the port the application is using, Open the file lunchSetting. json. You will find it under the properties folder in your project and as shown below. Inside the file, change applicationUrl port (below is set to 5000) to a different port number and save the file.

Can you use C# with electron?

And as it is C# core, it is cross-platform as well. Cross-platform desktop app powered by Electron using React for UI and extended functionality by C# goodness. And now not only you can use your frontend skills, but backend as well.

How does electron Net work?

Electron.NET is an open-source tool that adds value to ASP. NET Core by providing C# developers with a vehicle for delivering cross-platform desktop applications for Windows, Linux, and macOS. It is also compatible with third-party components such as the MVC controls in ComponentOne Studio Enterprise.

Is .NET core faster than node?

By its built-in IIS server, Net Core is likely to prevail. IIS employs kernel-mode caching, which implies that requests for the static page will not even make it out of the kernel. It can perform very CPU-intensive operations faster than node. js on the net core side.


3 Answers

Since version 5.22.14 of Electron.NET - Is it possible to define a separate port in the electron.manifest.json file.

An example of the manifest file: https://github.com/ElectronNET/Electron.NET/blob/master/ElectronNET.WebApp/electron.manifest.json

Add the following entry here:
"aspCoreBackendPort": 8080

like image 141
Gregor Biswanger Avatar answered Nov 14 '22 03:11

Gregor Biswanger


Quickest solution is to set the environmental variable with a static port (ASPNETCORE_URLS=http://+:3333) defined before dotnet start up. A great node package for setting ENV variables cross platform is cross-env - https://www.npmjs.com/package/cross-env

This may be the first of many launch settings you wish to change. You should strongly consider setting up a launchsettings.json file with these configurations. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1#development

like image 23
simsom Avatar answered Nov 14 '22 03:11

simsom


There is a static class BridgeSettings in Electron.NET that gives you the port settings so you can use it to set the port in the CORS settings. For example:

app.UseCors((builder) => builder.WithOrigins($"http://localhost:{BridgeSettings.WebPort}"));
like image 30
AlesD Avatar answered Nov 14 '22 03:11

AlesD