I've been reading up on the GUI situation for C# on linux and while it seems obvious that there is no support for Forms (or Windows Forms I guess) on Linux, there seems to be a lot of confusion for me regarding the GUI development. For context, I have just recently begun to learn C# and was looking to practice some simple commands while also trying to make a functional GUI with buttons that will execute some commands for me. I'm not exactly sure what next steps to take because any forums / wikis regarding this topic usually go like "Is it possible" while someone on the other end replies "Just use C++ lol".
Would really appreciate any help and especially some beginning point or links for me to go to so I can start learning all of this. Loving C# so far!
--
Réponses :
Également proposés :
On your local machine publish your project by running the following from the project folder:
$ dotnet publish -c Release -o ../_deploy
Create a website folder on the server:
$ mkdir -p /var/www/YOUR-WEBSITE
And copy the contents of ../_deploy from your machine to /var/www/YOUR-WEBSITE/ on the server.
Change the owner of your website’s directory so it would belong to NGINX’s user (www-data):
$ chown -R www-data:www-data /var/www/
Create a systemd config for Kestrel instance:
$ nano /etc/systemd/system/kestrel-YOUR-WEBSITE.service
Edit it like that:
[Unit]
Description=YOUR-WEBSITE
[Service]
WorkingDirectory=/var/www/YOUR-WEBSITE/
ExecStart=/usr/bin/dotnet /var/www/YOUR-WEBSITE/YOUR-WEBSITE.dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-YOUR-WEBSITE
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
Enable and start it:
$ systemctl enable kestrel-YOUR-WEBSITE.service
$ systemctl start kestrel-YOUR-WEBSITE.service
You can check its status:
$ systemctl status kestrel-YOUR-WEBSITE.service
If everything is okay, then it will show something like that:
● kestrel-YOUR-WEBSITE.service - YOUR-WEBSITE
Loaded: loaded (/etc/systemd/system/kestrel-YOUR-WEBSITE.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2017-08-10 11:30:09 UTC; 1s ago
Main PID: 15628 (dotnet)
Tasks: 14
Memory: 25.4M
CPU: 1.380s
CGroup: /system.slice/kestrel-YOUR-WEBSITE.service
└─15628 /usr/bin/dotnet /var/www/YOUR-WEBSITE/YOUR-WEBSITE.dll
Install NGINX:
$ apt-get install nginx
Now, there are 2 options how NGINX can send requests to Kestrel: via TCP or via Unix socket.
Your Program.cs:
// ...
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5000/")
.UseStartup<Startup>();
// ...
NGINX config (/etc/nginx/sites-available/default):
# ...
server {
listen 80;
listen [::]:80;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# ...
This should be better in terms of performance as there is no TCP overhead. Even if there was no performance impact, on a local machine “talking” through Unix socket simply makes more sense than communicating over TCP.
Your Program.cs:
// ...
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseLibuv()
.ConfigureKestrel(
(context, serverOptions) =>
{
// /var/www/YOUR-WEBSITE
string root = Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location
);
// if we are (behind NGINX) and on Linux, then can use sockets
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
// path to socket has to be absolute
string socket = Path.Combine(root, "kestrel.sock");
serverOptions.ListenUnixSocket(socket);
}
else
{
serverOptions.Listen(IPAddress.Loopback, 5000);
}
}
)
.UseStartup<Startup>();
// ...
For that to work you need to add Transport.Libuv
package, so you’ll have the following in your .csproj:
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="2.2.0" />
NGINX config in this case:
# ...
location / {
proxy_pass http://unix:/var/www/YOUR-WEBSITE/kestrel.sock;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# ...
Save it and restart NGINX:
$ systemctl restart nginx.service
Now open your web-browser and go to http://YOUR-SERVER-IP/. It fucking works!