C# Reference a Class Library on an Absolute Path - class-library

I have a class library and want more than one application to reference it without setting the (Copy Local) property of the reference to True, i.e. I want the absolute path of the class library.
When I add reference to the DLL file of the class library it throws a run time error for not finding the location of the DLL file.
Is it possible to reference the class library with absolute path ?
MORE DETAILS
I was trying to test a specific scenario; I have a static class with a static variable in the class library and I made 2 test WinForm applications identical to each other (that simply set and get a static variable) that reference the class library.
I wanted to check if the static variable is shared between those 2 applications or not but I found they are not because the class library DLL file is copied to each application output directory.
This test was to decide if I should make a class in a class library in an application I am developing be static or no-static ?

Related

How to identify the right assembly to reference when abstracting logic to class libraries and choose between referencing an assembly or NuGet package

I am attempting to abstract some of our common WebApi bootstrapping logic into projects that all WebApis can reference. The general idea being that all the builder.Services.Blah() and app.UseOtherBlah() calls are standardised across our projects.
My assumption was that I would just create a class library and reference any assemblies I needed.
But I'm running into confusions due to the class library obviously not being a Web API project that's been all nicely set up for the web bootstrapping process.
The code below exists in a class library project as the only class. It is my attempt to abstract out the logging configuration. It doesn't do anything useful at the moment as I am just proofing the concept of abstracting this out into a class library.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace XXXX.Hosting
{
public static class ExtendILoggingBuilder
{
public static ILoggingBuilder ConfigureLoggingProviders(this ILoggingBuilder logging, ConfigurationManager configuration)
{
// TODO: load providers from config
logging.ClearProviders();
logging.AddConsole();
return logging;
}
}
}
I need references (of some kind) to Microsoft.Extensions.Configuration and Microsoft.Extensions.Logging.
VS offers me the choice for Microsoft.Extensions.Logging between an assembly and a NuGet package.
But for Microsoft.Extensions.Configuration, I only get offered a NuGet package.
When I install the package, the dlls inside are not the dlls listed in the documentation page for the ConfigurationManager class here.
And I can't find the dll listed anywhere on my disk. Nor is it offered via NuGet.
My question is:
How do I work out a) what dll I need and b) where to get it from?
I don't really want a mixture of NuGet package references and assembly references. And I don't really want to reference anything I don't need to reference.

How to add new DLL locations to WebAPI's controller discovery?

ASP.NET WebAPI has a much appreciated ability to discover ApiController classes in external DLLs even if those DLLs are not referenced. For example, I may have MyWebApiProject that has a set of ApiControllers. I could then create a completely separate project called MyApiProjectPlugin that contains ApiController classes also. I have been able to add the MyApiProjectPlugin.dll file to the bin folder with the first MyApiProject.dll and the original project will discover all the controllers in the plugin project. I really like that ability.
However, What I would like to do is be able to add the plugin project to a sub directory inside of the bin folder. Something like bin/plugins. When I tried this, the original MyApiProject was unable to discover the plugin's controllers.
Is there a simple way to get WebAPI to look for ApiController classes in the bin's subdirectories? If I can avoid rewriting a controller factory from scratch I would like to.
You can write an assembly resolver.
public class PluginsResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());
assemblies.Add(Assembly.LoadFrom(#"<Path>\MyApiProjectPlugin.dll"));
return assemblies;
}
}
In the Register method in WebApiConfig, register the resolver.
config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver());

Class library Database Connection String

I have a solution with multiple project using the same domain model. I thus created a class library that holds my domain models. This class library also contains other parameters that are used within my projects. I then add the reference to the class library in each of my projects.
My class library also has some repository classes derived from this example.
I however have an issue with connecting to a database. I want my class library to be able to connect to the database since I defined my database context class in there, where I set my database sets. With a single project, I usually define my connection string in my web.config file. But the class library has no web.config file. How do I set my connection string?
EDIT
Say i have the constructor of my database context, mydbcontext, defined in the class library as
public mydbcontext() : base(ConfigurationManager.ConnectionStrings["DatabaseCon"].ConnectionString)
{
}
If I understanding this right, will it be OK to just set the name of the connection string of each project to "DatabaseCon"?
You don't.
Pass in the connection string as a dependency to whatever classes that require it.
You can encapsulate the access to it - but you should instantiate it in whatever program that uses this library. This program will hold the connection string in its configuration.
You should define the connection string in the web.config file of the application that is using the class library. As an alternative you could hardcode the connection string into the constructor of your DbConext inside the class library - pretty bad approach because you won't be able to modify it from the outside - for example you will have hard time managing different connection strings for the different environments - staging, production, ...

using App.xaml.cs Reference in another project present in same solution?

hi i am having a different projects in my solution in the initial project (default project) i am accessing the global reference to App.xaml.cs in this way :-
App objref = (App)Application.Current;
But now i have added new project to my solution and trying to access the app.xaml.cs in the same way as defined earlier but i am not able to access app.xaml.cs ?
1)can i know the reason
2)What should i do if i want to use it in both the projects ?
Please let me know
Thanks in advance.
You can access it, but the new project will not be familiar with the derived App class that is in your project. To explain further we need to take inheritance into consideration.
There's a generic definition for the Application class that exposes a number of predefined methods. Your App.xaml.cs is a new class definition that is derived from the Application class. It has the methods it inherited plus what ever methods and properties that you've added. To make use of these any code that is seeking to use your extra properties or methods must have access to the class definition. Your classes in the other projects that you've added do not have access to this definition.
You'll need to make a class or interface definition that both projects have access to. There are several ways of organizing this. I'll present one.
Create your main project in the solution. This contains your
App.xaml.cs.
Create your class library project that contains the
other code.
Create a third project called Common that only contains
an Interface definition.
On the Interface definition define all of the methods/properties
that you want both your class library and main project to have
access too.
Have App.Xaml.cs implement this interface.
In the Class Library access var appReference =
(IMyInterfaceName)Applcation.Current. You'll have access to the
methods that were defined in the interface

MVC - The call is ambiguous between the following methods or properties

I can't figure out why I'm getting this error. I only have one class with one method in my project that looks like:
public static class Extensions
{
public static string Chop(this string s, int length)
{
...
return "";
}
}
I'm calling this in my View like so: #item.PostContent.Chop(20)
It's giving me a compiler error:
The call is ambiguous between the following methods or properties: 'Extensions.Chop(string, int)' and 'Extensions.Chop(string, int)'
Any help is appreciated. Thank you.
I'm guessing that you have put your Extensions class file in an App_Code folder, is that correct? If so, move it outside of App_Code and get rid of the folder. App_Code has no place in MVC applications which by default are created as Web Application projects as opposed to Web Site projects.
Web Applications are compiled to a dll at build time, and anything in App_Code is also compiled into a separate dll at run time. Hence there being two methods and the ambiguity.

Resources