.net core assembly can not concurrently run on linux - c#

I wrote an dotnet core app for running on linux.
published a single executable file with self-contained enabled.
dotnet publish -c Release -r linux-x64 -o ${dst_path} -v n /p:PublishTrimmed=true /p:PublishSingleFile=true
But strangely it can only run a single instance.
Because when it's running, it will remove the execute permission of the file.
$ ./Test
10/21/2020 11:50:27 AM [Info] Server started at ws://0.0.0.0:12500 (actual port 12500)
$ ls -l Test <---------Test is running, ls -l is executed in another terminal
-rw-rw-rw- 1 root root 53081298 10月 21 11:50 Test
And execute permission is added back after the runnning process exited.
$ ls -l Test <------------Test is not running.
-rwxrwxrwx 1 root root 53081298 10月 21 11:50 Test
So I can not start more than one process.
I am really confused with this problem.
Could anyone help?

Sorry for bothering, I found the root reason.
Just in case other may encount the same stupid situation, leave a message here.
My problem is caused by virutalBox.
Because I put my published assembly Test in the shared directory of my host between shared virtual machine.
After move my file Test to virtual machine's home directory, it works well.
And thank you for your help #jdoer1997

Related

Unable to obtain lock file access (Dotnet CLI)

I use Dev Containers to attach to a container and debug. And it was working just fine.
Recently however it shows this error when I hit F5 or run dotnet build from VS Code's terminal:
/usr/share/dotnet/sdk/7.0.102/NuGet.targets(132,5): error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/fcd2970c0c875310ff5855562ac5f3954170bddb' for operations on '/Crm/AdminApi/obj/project.nuget.cache'. This may mean that a different user or administrator is holding this lock and that this process does not have permission to access it. If no other process is currently performing an operation on this file it may mean that an earlier NuGet process crashed and left an inaccessible lock file, in this case removing the file '/tmp/NuGetScratch/lock/fcd2970c0c875310ff5855562ac5f3954170bddb' will allow NuGet to continue. [/Crm/AdminApi/Api.csproj]
And it shows the above message after trying to re-install every dependency, while NuGet has cached those dependencies already.
It works if:
1- I open a root bash using docker exec -it container_name bash and run dotnet build
2- I open a non root bash, and simply run sudo dotnet build
3- I open a VS Code termianl (which shows vscode as the user) and run sudo dotnet build
I tried sudo chown -R vscode:vscode /tmp/NuGetScratch/ as mentioned in dotnet error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/ and here, but that did not change anything. I then tried sudo chmod -R 777 /tmp/NuGetScratch and again no results. I even verified that the owner is changed using ls /tmp -lah | grep NuGet and this is the results:
drwxrwxrwx 1 vscode vscode 4.0K Feb 9 10:43 NuGetScratch
What else can I do?
This was recently logged as an issue: https://github.com/NuGet/Home/issues/12420
Possible workarounds listed from https://github.com/NuGet/Home/issues/12420#issuecomment-1423774814:
Run dotnet nuget locals temp -c to clear the /tmp/NuGetScratch (If there is a sticky bit in /tmp permissions then it won't work)
Set environment variable NUGET_ConcurrencyUtils_DeleteOnClose to 1 before running restore, so the lock files will be cleared after restore (This change is only available in NuGet 6.2 and above, probably .NET 7 preview6 and above)
Set environment variable NUGET_SCRATCH to a path. This variable will override the default temp folder. But it's only applied to NuGet version 6.2 and later, probably .NET 7 preview6 and above).

Host ASP.NET Core in Linux with Apache Docker

I am referencing this article on Microsoft's documentation:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-3.1
Has anyone tried to accomplish these steps in Docker container?
I have been at this for a couple of days and I can't get the kestrel-helloapp.service file to start my application automatically when I run the container.
After running the container I am able to manually go into it and start my application with dotnet WebApplication3.dll.
I am under the impression that this should happen automatically after enabling the service file.
The only way I am able to get it to work is by adding this to the Dockerfile:
ENTRYPOINT ["dotnet" ,"WebApplication3.dll"]
But when I do this it causes the Apache server to not start up automatically.
Here is my Dockerfile:
FROM centos:7
# install sudo and dotnet sdk
RUN yum install sudo -y
RUN sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
RUN yum install epel-release -y
RUN yum install dnf -y
RUN sudo dnf install dotnet-sdk-3.1 -y
# copy app files over
COPY ["./publish/", "/var/www/helloapp/publish/"]
# install apache and enable it
RUN sudo yum -y install httpd mod_ssl
RUN systemctl enable httpd.service
RUN yum install initscripts -y
RUN sudo service httpd configtest
# copy and enable service file
COPY ["./kestrel-helloapp.service", "/etc/systemd/system/"]
RUN sudo systemctl enable kestrel-helloapp.service
# start apache
CMD ["-D", "FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]
EXPOSE 80
docker run command:
docker run -v "C:\Users\Nick\source\repos\docker-testing\version1\helloapp.conf:/etc/httpd/conf.d/helloapp.conf" -e "ASPNETCORE_URLS=http://+:8080" -p 80:80 -p 8080:8080 -t version1
helloapp.conf file:
<VirtualHost *:*>
RequestHeader set "X-Forwarded-Proto" expr="http"
</VirtualHost>
<VirtualHost *:8080>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
ServerName www.example.com
ServerAlias *.example.com
ErrorLog /var/log/httpd/helloapp-error.log
CustomLog /var/log/httpd/helloapp-access.log common
</VirtualHost>
kesterl-helloapp.service file:
[Unit]
Description=Example .NET Web MVC App running on CentOS 7
[Service]
WorkingDirectory=/var/www/helloapp/publish
ExecStart=/usr/local/bin/dotnet /var/www/helloapp/publish/WebApplication3.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=apache
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
I know the configuration is correct because everything works fine when I start the application manually. The service file just seems to be not starting the application on boot.
Any help would be greatly appreciated. Thanks!
I've run into a similar problem (just with an ubuntu base image). The problem that you are likely experiencing is related to the fact that there is one process launched from the docker container on entry point and that is not the system daemon (init, systemd, not sure which one centos is using). As a result, your services are actually not started as you described, because they would be launched on system run level change, by that exact same system daemon.
In my opinion, not launching the system daemon is a good thing as you want to minimize services running inside of your container.
On the other hand, you might actually want multiple services inside of the container. An actual solution to your problem is to write an entry point shell script and launch the services that you want to run in parallel of your main application. In my case, I wanted a customized Jenkins image, which has an entry point of /usr/local/bin/jenkins.sh. You can find this in the Dockerfile of the base image that you are using.
I've replaced the original entry point:
ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]
with:
ENTRYPOINT ["/bin/tini", "--", "/docker-entrypoint.sh"]
Where the content of /docker-entrypoint.sh is:
#! /bin/bash
/usr/bin/cron & # This is the additional service I wanted in the background
/usr/local/bin/jenkins.sh

Installing Matlab on a Windows Docker

Trying to install Matlab run time on a docker image along with the project I'm working on, the project is an engine that will run a variety of measurements based on what is given to it, many of these measurements use Matlab. When I run the docker though I get an error that the "MWArray assembly failed to be initialized" or that a matlab dll is missing.
I'm trying to run this in Docker for Windows due to a company requirement, and have been unable to successfully get the DockerFile to recognize the MCR. Below is the code that I've been playing with to get the MCR onto a docker.
FROM mcr.microsoft.com/dotnet/framework/runtime:4.7.2-windowsservercore-ltsc2019
ADD http://ssd.mathworks.com/supportfiles/downloads/R2017b/deployment_files/R2017b/installers/win64/MCR_R2017b_win64_installer.exe C:\\MCR_R2017b_win64_installer.zip
# Line 3: Use PowerShell
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
# Line 4: Unpack ZIP contents to installation folder
RUN Expand-Archive C:\\MCR_R2017b_win64_installer.zip -DestinationPath C:\\MCR_INSTALLER
# Line 5: Run the setup command for a non-interactive installation of MCR
RUN Start-Process C:\MCR_INSTALLER\bin\win64\setup.exe -ArgumentList '-mode silent', '-agreeToLicense yes' -Wait
# Line 6: Remove ZIP and installation folder after setup is complete
RUN Remove-Item -Force -Recurse C:\\MCR_INSTALLER, C:\\MCR_R2017b_win64_installer.zip
WORKDIR /app
COPY /Project/bin/Debug/*.dll ./
COPY /Project/bin/Debug/Project.exe .
ENTRYPOINT ["C:\\app\\Project.exe"]
Edit: I think I've found a working solution, following the idea from the other anwser about the ltsc2019 not working with Matlab 2017b. The below code has worked with 2017b inside a docker.
FROM mcr.microsoft.com/windows:1809
Windows Server 2019 is not supported by MATLAB R2017b, and support for it was not introduced until MATLAB R2019a.
For MATLAB R2017b you’ll need Windows Server 2016.
That’s not to say there may not be other issues as well.

.NetCore for Mac - Publish to Native Mac App/Binary?

I've been searching for quite some time now, and can't seem to find an answer to this problem. Found only two questions/answers on SO and they still don't answer this question (https://stackoverflow.com/search?q=netcore+publish+mac+app).
I'm working with DotNetCore on Mac, using Visual Studio as the IDE. The app is a Console App, not an ASP.Net app, simple "Hello World" app in C#:
...
Console.Writeline("Hello World");
...
So here's the question... To run the app, I know I can use the "dotnet" command to run it. I'm trying to build/publish the app, as you normally would do in Windows by creating an .exe file, but now on Mac by creating a native binary file.
I have found zero documentation on how to do this, and deploy the application as a self contained app that can run independently without having to call the program using the "dotnet" command. Maybe I'm looking in the wrong places but haven't even found anything on Microsoft's documentation, they all point to documentation for building ASP.Net apps on .NetCore.
Any suggestions?
Found the answer by looking at the "dotnet publish" options:
dotnet publish -c Release --self-contained -r osx.10.13-x64
Where --self-contained includes all required libraries, and -r specifies the runtime target.
$ dotnet publish -c Release --self-contained -a x64
Determining projects to restore...
Restored /Users/walshca/code/temp/MutexThrow/MutexThrow.csproj (in 155 ms).
MutexThrow -> /Users/walshca/code/temp/MutexThrow/bin/Release/net6.0/osx-x64/MutexThrow.dll
MutexThrow -> /Users/walshca/code/temp/MutexThrow/bin/Release/net6.0/osx-x64/publish/
dotnet publish docs
Then I run ./bin/Release/net6.0/osx-x64/publish/MutexThrow
This didn't specify the --output cli arg, so you can see in the build output it defaulted to [project_file_folder]/bin/[configuration]/[framework]/[runtime]/publish/
(In dotnet 6.0 instead of -r runtime target, you can specify --arch x86 and it uses the default RID for your system.)
If your project props sets a different build output, can you find the executable by enumerating files by unix file permissions:
$ gci -r -file | ? UnixMode -match 'x$' | % FullName
/Users/walshca/code/temp/MutexThrow/obj/Release/net6.0/osx-x64/apphost
/Users/walshca/code/temp/MutexThrow/bin/Release/net6.0/osx-x64/MutexThrow
/Users/walshca/code/temp/MutexThrow/bin/Release/net6.0/osx-x64/publish/MutexThrow

SU command for updates

I'm trying to update an app remotely. All devices are rooted.
Due to the nature of the apps and devices, there are no users, the devices monitor a range of sensors and send the info back to a server from key locations.
I know that
"pm install -r app.apk\n"
will install a downloaded apk.
But how would I get it to run without a user.
Once this command executes the app stops and all it's services stop aswell.
So is there a command to install + run ?
am start -n com.package.name/com.package.name.ActivityName
does not get executed and wont start the services because this code is not reached after the install command
edit:
this is the code once the apk is downloaded
dataOutputStream.WriteBytes("mount -o rw,remount -t /system\n");
dataOutputStream.Flush();
dataOutputStream.WriteBytes("chmod -R 777 "+localPath+"\n");
dataOutputStream.Flush();
dataOutputStream.WriteBytes("mount -o ro,remount -t /system\n");
dataOutputStream.Flush();
dataOutputStream.WriteBytes("pm install -r "+localPath+"\n");
dataOutputStream.Flush();
//The code below is not reached because the install kills the app
dataOutputStream.WriteBytes("am start -n com.company.remote/com.company.remote.RebootServices\n");
dataOutputStream.Flush();
Whenever you install an app through Android Studio, it installs your app and launches it immediately afterwards. By looking into the commands it executes, I have found the following line, it might help you:
adb shell am start -n "com.package.name/com.package.name.ActivityName" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Notice the intent filter flags: -a for "action" and -c for "category" as defined in manifest. If you have an intent filter, you can use those too.
EDIT: After reading the comment of SushiHangover, this is indeed a duplicate of this question: https://stackoverflow.com/a/4567928/3673616
According to the answer from there, instead of am start[...] you need to use adb shell am start[...]

Categories

Resources