C# Library class accessing a resource file - c#

I recently made a API on AWS API Gateway. To make accessing it easier, I created a dev kit library to make accessing the Gateway methods easier (using .Net 6). To make getting the API URI's easier to access and store, I put all of the URI's into a json file in the library.
When doing unit testing on the library itself, everything passed. The library accesses the json file with the below code
using (StreamReader reader = new StreamReader(Path.Combine(Directory.GetCurrentDirectory(), "Resources\\config.json")))
{
json = reader.ReadToEnd();
}
I published the library as a Nuget package and then installed it on a .Net web app. When the app tries calls a method from this dev kit, it throws the following error:
Message=Could not find a part of the path 'C:\Projects*ParentFolder**Subfolder**ProjectFolder*\Resources\config.json'
Which the file path given is the file path of the project, not the filepath that leads to the config.json file. So the question is, how do you structure the file path to access a file if it is in a library that is designed to be a stand alone library like a dev kit? Or is it just a bad idea and should I approach storing the URI's for the methods in a different manner?
FYI - the I do know the issue has nothing to do with the AWS API Gateway or Lambda functions. Those are running correctly and performing in a consistent manner.
TIA!

The current directory is a property of the process, which will be the application referencing the library. If you want the path of the library itself then I would think that something like this would do the job:
var rootFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Related

Downloading a file from JFrog Artifactory using its URL

I'm using JFrog Artifactory and want to download file using C# and WebClient. The URL is like /filename.zip
But it downloads as a HTML page. Says container is damaged. And also the HTML says "you needs to enable JavaScript".
How can I fix that?
The Retrieve Folder or Repository Archive API allows to download an archive file (supports zip/tar/tar.gz/tgz) containing all the artifacts that reside under the specified path (folder or repository root). However it does not support filtering by properties.
The Artifactory CLI supports concurrently downloading multiple files. It also supports downloading files we matches a set of property values. The CLI, however, will use multiple HTTP requests for doing so.
A third option would be developing a custom user plugin which allows downloading an archive of artifacts matching a set of properties. An execution user plugin can be executed as a REST API call. There is a sample plugin in the JFrogDev GitHub account which can serve as a good start point. This plugin allows downloading the content of a directory as an archive.

Using Realm Sync with Azure Functions

I'm trying to use Realm Cloud in an Azure Function but it keeps giving me an error:
make_dir() failed: Permission denied Path: /realm-object-server/
Is there a way to configure Azure Functions to have permissions to create files? I'm new to Azure Functions, this is my first one so I'm not really sure of all the particulars.
I have found one solution to this. Inside Azure function, you can only create any file or folder inside the temp folder. So if you use following sync configuration, it should work. It worked for me.
var configuration = new FullSyncConfiguration(new Uri("/~/<your-realm-name>", UriKind.Relative), _realmUser, Path.Combine(Path.GetTempPath(), "realm-object-server"));
So basically, here you are passing the folder name to store the realm locally. If you don't pass the folder name, it will try to store it in a default location where you will get the access error.
I am not familiar with Realm, but functions has permissions to interact with the file system by default. See these links for information on the app service file system (this applies for functions too):
https://learn.microsoft.com/en-us/azure/app-service/operating-system-functionality#file-access
https://github.com/projectkudu/kudu/wiki/Understanding-the-Azure-App-Service-file-system
If the function is deployed using run from package, then the wwwroot is readonly. But since the path in the error message doesn't point to wwwroot, this is probably not the issue.
My best guess is that the code that is failing is trying to write to in an inappropriate location. The information in the links above should help resolve that. Maybe check to see if realm has a config setting that lets you specify which location it should be creating "realm-object-server" in.
You can use
SyncConfigurationBase.Initialize(UserPersistenceMode.NotEncrypted, basePath: Path.GetTempPath());

Better solution to store data for dll assembly

I'm developing a dll that is supposed to be commonly used (in nuget for example). Simple description: my DLL simplifies message exchange with a particular service. It allows to send a request, then retrieve a response. Service is asynchronous and it can create a response in a hour or a day after accepting a request, so after making a request my dll calls service every few minutes to check out for response. The problem is that the app that uses the dll can be restarted therefore storing a request queue in memory isn't a good option (I don't want to lose info about requests). Neither is serializing it to file, because I can't know for sure where my dll will be used - it could be pc app, mvc. My main options is: serialize to file, but give an option to set a address where to place serialized files via web/app.config or make a user to think about it. But maybe there is some better solution about how to store requests queue?
I would put theses type of configuration or data files in a subfolder to the %appdata% folder. You will have write access to files in this folder and the documentation is extensive. Read more here.
in C# you can easily get this folder using:
var appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Or use Program Data:
var programdata = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

DNN Database Repository Reuse in Console Application

I have a project based on the Chris Hammond, Christoc, module template. I have a ton of code that I use to access data an external database. In my repositories I change the database from the default to whichever I need for that particular object. I do so with code that looks like this:
using (IDataContext ctx = DataContext.Instance(MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY))
{
var rep = ctx.GetRepository<Product>();
products = rep.Get().ToList();
}
The default database is switched in the call to .Instance(). The repositories are used by my custom DNN modules. The repository is part of the solution that contains multiple custom modules. When I compile and install using the Extensions part of DNN, everything works well. In the code above, MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY is found in a file MyModuleSettingsBase.cs file of my module solution. It is set to a simple string like "ProductDatabase". In the solution for the base DNN install (not the module solution), within the web.config file, there is a value in <connectionStrings> with name="ProductDatabase" which contains the actual connection string. This all links up fine on the DNN website.
Now I am writing a console application that does some monitoring of the site. I want to access the database to check values in the product table. I would like to reuse all of the repository code I have written. In an attempt to do so, I added a reference to the MyModules.dll file so I would only have one copy of the base code. This works to give me access to all the objects and the associated repositories but when I attempt to query data it fails. When debugging I can see that it fails on the line:
using (IDataContext ctx = DataContext.Instance(MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY))
When viewed in a debugger, the string value MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY is correctly set to "ProductDatabase" but the code is unable to link this with the actual connection string. I don't know where it would be checking for the connections string when running from my console application. I attempted to put a <connectionStrings> section into my App.config file but this didn't do the trick.
Is it possible to have MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY map to the connection string in an external application which references the DLL?
If so, where can I set the value of my connection string so it matches up to the key value stored in MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY?
I was faced similar problem 3 months ago, at that time I want to use DNN core libraries in my console application but I was failed.
I placed my queries in DNN official forum website and I got a valid response from Wes Tatters (DNN MVP).
Here is the post link: Reference URL
As your requirement of monitoring, I suggest you to create DNN Schedule Application. You can schedule it within DNN (Host->AdvancedSettings->Schedule), even good point is that you can use your repositories (DNN Libraries) in that schedule application.
I hope it solved your problem. Let me know if you have any questions.

How to set root path for static files in ServiceStack self-host

All of the ServiceStack self-host examples serve static files from the same directory as the console or service executable assembly.
Is there a way to change the rooth path to something else?
When I set Config.WebHostPhysicalPath to a different path from within AppHostHttpListenerBase.Configure, my html file had to exist in both places for ServiceStack to return anything.
I was wondering the same thing so I dug into the SS source and found an issue that prevents the Config.WebHostPhysicalPath from working with static files. You can read about it here: https://github.com/ServiceStack/ServiceStack/issues/352
Update 2012-12-03
My pull request for this issue has just been merged into ServiceStack/master.
https://github.com/ServiceStack/ServiceStack/pull/357

Categories

Resources