.NET Core 3 - Load static files on Docker enviroment - c#

I got some stupid question, but honestly I rly desperate right now.
In my .NET Core3 app, Im loading HTML template to variable with helps of absoluth path
var pathToFile = _env.ContentRootPath
+ Path.DirectorySeparatorChar.ToString()
+ "Templates.html"
var builder = new BodyBuilder();
using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
{
builder.HtmlBody = SourceReader.ReadToEnd();
}
In my testing localhost environment it works fine, bcs its accessing the file on my disk.
But the problem here is, I cant reach this file if I run my app on Linux Docker environment.
How to properly load HTML files to variable on Docker Env with .NET Core3 ?
Thanks for your hints

Related

System.IO.DirectoryNotFoundException on server with the path from local machine

I have created a web api with c# .net core 3.1 and I use a directory to save generated invoices there. the path to the directory on my local machine is C:\CU World\Backend\CU-API\CU-API\Generated Invoices\
On my local machine the api works fine. But on my linux server it does not work... I'm getting this error:
I do not understand why I am getting this error when I publish this to linux server with apache2 installed on it. Can somebody help me, and expain me how to fix it?
I could fix it by adding this to my Startp.cs
var fileProviderPath = Environment.GetEnvironmentVariable("FILE_PROVIDER_PATH");
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(fileProviderPath),
RequestPath = "/GeneratedInvoices"
});

NLog in different versions for multiple projects

In my project I am using NLog assembly with file version 4.6.6. My application is being loaded by the external app (no access to the source code) which also uses NLog with file version 4.7.2.
Everything from the external app and my app is being logged correctly. The strange thing that I am facing is that in my app I add the logging rules:
var uniqueLogName = "mylogger" + Guid.NewGuid();
var rule = new LoggingRule(uniqueLogName, ConvertLogLevel(SelectedLogLevel), myFileTarget);
LogConfig.LoggingRules.Add(rule);
var uniqueObjectsLogName = "myObjectslogger" + Guid.NewGuid();
var objectsRule = new LoggingRule(uniqueObjectsLogName, ConvertLogLevel(SelectedLogLevel), myObjectTarget);
LogConfig.LoggingRules.Add(objectsRule);
And for some reason I am getting folders with these rule names created on C drive (see the link below) and honestly, I don't even know where to look for the issue. Can this be some sort of configuration on the NLog?

access outside project folder with Asp.net core application on linux

I'm having some issues trying to access a folder outside the application root folder with an asp.net application deployed on Linux.
The applcation is deployed at the moment (for testing purposes) in /home/pbl/projects/pbl-web/.
I want to access a folder of an external hard drive I've mounted on the system, and it's located in /mnt/ExtDist/Data.
I'm using a PhysicalFileProvider with the following path: /mnt/ExtDist/Data. This path is configured in app.settings.json file and retrieved through the IConfiguration configuration variable.
Here's a part of the code in the Startup.cs file :
public void ConfigureServices(IServiceCollection services)
{
...
var imagePath = configuration.GetSection("PhotoManagementSettings")["ImagesFolderPath"];
var rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
this.imagePhysicalFileProvider = new PhysicalFileProvider(imagePath));
I've tried in different ways with no luck so far:
passing the absolute path
passing the relative path and combining with the rootPath variable (see the code above).
The PhysicalFileProvider is getting me the following error:
Unhandled exception. System.IO.DirectoryNotFoundException: /mnt/ExtDist/Data/
Testing the code in windows and giving it an absolute path like i.e "C:\Test" works fine.
So there's something weird in linux that is failing, but I cannot understand why. Any clues ?
Thanks in advance
Paolo

In Docker.DotNet how do you build an image by specifying the directory of the Dockerfile?

I am trying to build an image and start a container, for the purposes of integration testing, in a C# xunit test. I currently have the code below, but can't find away to specify the path to the docker file. The client does have Images.BuildImageFromDockerfileAsync but that deals with unpacking TAR files.
using(var client = new DockerClientConfiguration(
new Uri(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "npipe://./pipe/docker_engine"
: "unix:/var/run/docker.sock")).CreateClient())
{
await client
.Images
.CreateImageAsync(
new ImagesCreateParameters(),
new AuthConfig(),
new Progress<JSONMessage>());
}
I want to do the equivalent of docker build -t yyyy -f ./zzzzzz/Dockerfile . using the Docker.DotNet api but I can't work out a way to do it. Any help would be kindly appreciated.
The docker build command effectively creates a Tar of the entire working directory and sends it to the build daemon. So much so that if you run docker build - it will expect a tar stream from stdin.
You could do something like this to tar up the build directory and pass it to BuildImageFromDockerFileAsync: https://github.com/dotnet/Docker.DotNet/issues/309#issuecomment-547316442

Execute local R script via R.NET

I'm looking to use R.NET to execute an existing R script but haven't had success. Is this actually possible? I've run the example code successfully, so my basic setup is ok.
My code looks like the following:
static void RTest()
{
var envPath = Environment.GetEnvironmentVariable("PATH");
var rBinPath = System.Environment.Is64BitProcess ? #"C:\Program Files\R\R-3.0.1\bin\x64" : #"C:\Program Files\R\R-3.0.1\bin\i386";
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
using( var engine = REngine.CreateInstance("RDotNet") )
{
engine.Initialize();
using( var fs = File.OpenRead(#"C:\R-scripts\r-test.R") )
{
engine.Evaluate(fs);
}
}
}
Which I'm running in a console app for testing (eventually I want to have it run server-side in a web app).
The r-test.R script works when run in RStudio so there's no problem there and should result in writing a csv file to disk. I do see some 'Loading required package' messages being output to the console so something is working but the resultant csv file does not appear.
As indicated in response to this post in the R.NET discussions, you can use engine.Evaluate(#"source('c:/path/to/r-test.R')"). Although a lot depends on the content of your script of course, it should work. That said your code looks like it should work as well, though I have not tried your approach.
It is possible that R.NET chokes on some particular R statement within your script. If you have visual studio it should be possible for you to attach to the process if you use R.NET compiled from source with debug symbols. If you have Visual Studio this is the easiest option; MonoDevelop / Xamarin studio is also an option, though a bit more involved. This should help you identify the troublesome line.
Hope this helps

Categories

Resources