Not getting path to class library project from within class library project - c#

I am calling a helper method in my Services class library project from a controller in my UI web application. I cannot get the proper path to the templates in the services project. I have tried dozens of ways but every time the base path of the full path points to the UI project.
C:\Users\TFD\OneDrive\TestEmal.UI\TestEmal.UI\bin\Debug\netcoreapp2.0\EmailService\EmailTemplates\EmailMaster_Body.html
I am building the path in the Services class library project
private static readonly string ThisDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
This came from the accepted solution from this post, accepted answer by mark Amery
Class library path SO
I have tried every permutation of Path and of Assembly but every one returns the path to the Web UI application.
How do I get the base path of the Services class library project without hardcoding or using Replace?

You cannot. A class library is compiled into the application that references it. After build/publish, the DLL for the library resides in the same directory as all the other DLLs for your application, meaning any paths will always be relative to your web app directory, not your class library directory.
It's not entirely clear what you're trying to achieve here, but if there's simply some file or files in your class library project directory that your class library needs to reference, you need to add them to your project and set them to copy on build in the properties pane for each file in Visual Studio. This will result in the file(s) coming along for the ride and ending up in your web app's build/publish directory as well. Your paths will still be relative to the web app, not the class library.
Alternatively, you can have a build task that does the copy instead, but that's a little more complicated to set up.

Related

Firebase functions build error when referencing multiple c# projects

I was able to deploy my firebase c# function with no issues, however, when i referenced another c# project so that i could utilize another object i get error saying project doesn't exist.
So was able to deploy following with no problem:
namespace CloudFunctions
{
public class Login : IHttpFunction
{
public async Task HandleAsync(HttpContext context) {
await context.Response.WriteAsync("Hello World!");
}
}
}
This class lives in a project called CloudFunctions. I added a project reference to a project called Services so that i could call the login service and i get the following error:
The referenced project '../Services/Services.csproj' does not exist
This is how i am deploying:
gcloud functions deploy login --entry-point CloudFunctions.Login --runtime dotnet3 --trigger-http --allow-unauthenticated
I can't imagine we would be required to have everything in one project in order to deploy?
You need to make all of the projects available to the buildpack (i.e. deploy from the parent directory) but specify the project that contains the entry point as well, using the GOOGLE_BUILDABLE build-time environment variable.
From the deployment documentation in the Functions Framework GitHub repo:
Real world functions are often part of a larger application which will usually contain common code for data types and common business logic. If your function depends on another project via a local project reference (a <ProjectReference> element in your .csproj file), the source code for that project must also be included when deploying to Google Cloud Functions. Additionally, you need to specify which project contains the function you wish to deploy.
In a typical directory structure where all projects sit side-by-side within one top-level directory, this will mean you need to deploy from that top-level directory instead of from the function's project directory. You also need to use the --set-build-env-vars command line flag to specify the GOOGLE_BUILDABLE build-time environment variable. This tells the Google Cloud Functions deployment process which project to build and deploy. Note that the GOOGLE_BUILDABLE environment variable value is case-sensitive, and should match the directory and file names used.
When deploying a function with multiple projects, it's important to make sure you have a suitable .gcloudignore file, so that you only upload the code that you want to. In particular, you should almost always include bin/ and obj/ in the .gcloudignore file so that you don't upload your locally-built binaries.
Sample deployment command line from the examples directory:
gcloud functions deploy multi-project \
--runtime dotnet3 \
--trigger-http \
--entry-point=Google.Cloud.Functions.Examples.MultiProjectFunction.Function \
--set-build-env-vars=GOOGLE_BUILDABLE=Google.Cloud.Functions.Examples.MultiProjectFunction

Is it necessary to deploy the XML file in a class library?

I have developed a lot of class library projects in VS 2012 to be used in Windows Forms and Web forms applications.
The question is simple. Do I need to deploy the DLL file itself together with the XML file that is created?
For example, the class library project is called DataWare. Upon building, I got 5 files in Release folder (this project reference Entity Framework):
DataWare.dll
DataWare.pdb
DataWare.dll.config
EntityFramework.dll
EntityFramework.xml
I know that ".pdb" file contains debugging information, so there is no need to deploy. The ".config" file is not taken into account. Instead the App.config or Web.config are.
Regarding this, I think I have to deploy just DataWare.dll and EntityFramework.dll.
However, the main doubt is if I need to deploy EntityFramework.xml as well.
Regards
Jaime
The XML file contains the doc comments for the public types & members in the assembly.
You only need it if you want Visual Studio to show documentation in IntelliSense.
If you're deploying a consumer-facing app (as opposed to a developer-facing reusable library), you do not need it.
No, in most cases you do not need it. If there is an external DLL that needs to be copied local and referenced using the config, then you might need to, but that is somewhat rare.

How to read the properties of an .resx file in a Web site project of asp. Net from a dll assembly

I'm new in .Net and working with two projects in c# a class library project(dll) and a website asp project.
I need to read some properties from a file .resx that is in the App_GlobalResources folder of the website.
Is there a way to read these properties in the .resx website file from a dll assembly more specifically in the method onPreRender??
Thanks for you attention
It sounds to me that you are having some problems with structuring and dependencies in your solution. (Trying to reference the website from a DLL)
Generally speaking, your DLL should not need to access the resources of the website on its own - you should only pass them in through as parameters when calling various methods that are contained in the DLL itself.
Have you thought about migrating the resource file to the DLL?
That would allow both DLL and the website to read from it.
Another option would be to migrate the setting you need to the .config file which you can read by using the ConfigurationManager class ( MSND Link )
You should be able to use it like this, even from your Code repository project:
string settingValue = ConfigurationManager.AppSettings["YouSettingNameHere"].ToString();
However, if you really want to keep your current solution structure you can follow the answer that Pavel Chuchuva gave on a similar question here.

reusing app_code dll from website in another app

I have a project which was done in a website. moving to a web app will be a real pain.
It does get published, and thats how it gets moved to production.
as a result of the publishing, all the classes inside the app_code folder get compiled into an app_code.dll file.
I want to import this dll into another project.
it imports fine. but when i try to deckare an object from inside, my new project cant get a reference to the object.
Can this be done?
Just don't do it. Create a class library project and encapsulate the functionality into a named dynamic link library, which is the output, that can be shared and versioned properly (as opposed to its history being mixed with the nature the website that uses it) in its own right.

C# Get path of class library

I have a class library that uses some xml files found in its own directory.
When referencing this library from another project, how do I ensure the library is working from it's own directory?
I tried Assembly.GetExecutingAssembly().Location but that still returns the path of the startup project.
I know this is an old post but in the event someone else stumbles on it from a search, the easiest way I've found to do this is:
Public Class Foo
Public Shared Function GetMyPath() As String
Return Path.GetDirectoryName(GetType(Foo).Assembly.Location)
End Function
End Class
This is happening because your startup project has two options as to loading the library
Copy it to it's own folder (what would cause the behaviour you are experimenting)
Load it from GAC (what would make impossible to load any XML from it's folder)
So you have two options here:
Import the XML files in the project and embed them as "embedded resources" in the class library and load it in memory at runtime
Import the XML files in the project and change the "copy to output directory" property of the file to "true".
I don't think you can unless you start it in a new AppDomain, and that's more trouble than it's worth. Just copy the files it needs into your own project, and put them in the place it expects relative to your working directory.
(You might also want to ask the original developer why the paths to the needed files can't be given as a parameter. This would be the normal way of solving this problem.)
I'm sure there is some functions to get the list of loaded assemblies from the ExecutingAssembly, loop across this to find you assemble(class lib) then get it's location. Sorry for vague answer.

Categories