Global.cs file replacement in .Net Standard Class library - c#

I am new to .net standard/.net core/blazer. In .net framework class library we used to have Global.cs file to store global variables, needs to do the same in .net standard class library, whats the Microsoft recommended way?
OR to summarise above:
where to store Global variables in .Net standard class library project?

In Class library projects you put it in Web.config (or App.config) and it will be available from the class library as well. You basically add manually such file (app.config/webconfig).

If you're going to use a class library, I would recommend having any variables etc passed in via configuration parameters/configuration objects, and if you need defaults - have defaults created in your constructors.
I would personally shy away from having a class lib access web.config/appSettings.json, as you're then tieing your application to a specific implementation.

Related

Global using directive in c#

I'd like to create a namespace in c# that can be found in any project. Not just the one that it is located in. like the system namespace. Is that possible and if yes I'd like to know how.
I already googled and didn't find anything
Classes in the System namespace are part of the Base Class Library (BCL) that gets included as part of the .NET Runtime. The only way for you to have your class be as globally accessible as, say, the System.String class, would be to convince Microsoft to add your class into their BCL. That is rare, but not unheard of. The IObservable<> interface is an example of a type that was added that way.
However, there are tons of classes that people are using every day without having them added to the BCL. If you're willing to accept one additional step for people to take with their projects, in order to leverage your project, you can publish your project's output as a Nuget package. Then people only need to add your Nuget package (referenced by its package name), and they'll have access to the public API defined by the types in your DLLs.
Consumers of your package will still need to reference the namespaces of the types they want to use, either explicitly or via a using directive. In C#, a "global using directive" only makes the namespace globally available within the project that the directive is found in.
If you only want your types to be accessible from other projects found in the same solution, Nuget isn't necessary: you can add a project reference.
There are a lot of nuances I'm glossing over (i.e. differences between namespaces and DLLs and packages), but which it would be helpful for you to read about.
One of solution:
You need to create library(DLL) and refernce it in projects.
If you using visual studio 201x you can create project with type class library.
The library namespaces can be found use like this ´using MyNamespace;´
example of class in library:
adding refernce to project:
example of using your own class library:

How to use NLog from multiple projects in the same solution

I searched the net for a really simple question. My solution is one exe (WPF) project and four class libraries. I need a logging and I like NLog. How can I use it from all of the 5 projects in one solution ?
I don't know, do I need to create (or get somewhere) a wrapper class project referenced from all of the projects and use Nlog from there ? I saw something like this written for log4Net.
Or is there some pattern or best practice for this scenario ?
Just reference NLOG directly in each of the projects and use it. But you only need to configure it in your main application. The configuration will then be shared automatically.

Portable Class Library using F# without FSharp.Core.dll reference

I tried to create portable class library using F# which could be used for example from C# code. But looks like using standard F# constructs, like raise makes FSharp.Core.dll necessary to use created Portable Class Library.
raise(ArgumentNullException("parameter")) is transformed into
Operators.Raise<Unit>(new ArgumentNullException("source"));
(where Operators.Raise lives in Microsoft.FSharp.Core namespace) instead of just
throw new Exception
I assume that's not the only F# construct which is compiled to some static method from FSharp.Core.dll library.
Is there any way to create F# code (especially Portable Class Library) which after compilation does not require a reference to FSharp.Core.dll or any other FSharp-specific library?
If you compile the code with
--standalone
the compiler will do an equivalent of static linking. As a result, the references won't be needed at run time.
This is probably the best you can do.
Ideally you would be referencing a portable version of FSharp.Core that have been built against a certain profile, rather than statically linking everything.
I think there are portable versions of profile 47, 88, and 158.

Using ConfigurationManager.GetSection in a Class Library's App.config

I am writing a class library which has settings in its app.config and which will ultimately be called by a small number of other .NET applications. In order to get settings from it I'm using ConfigurationManager.GetSection such as this:
MyConfiguration process = (MyConfiguration)ConfigurationManager.GetSection("MyGroup/processes");
I've discovered though that the calling application has to have the same app.config inside it's own project in order for this to work, otherwise the class library will throw a NullReferenceException. I'm just wondering if this is normal behavior or if there's any way to ensure that only the class library needs to have app.config available?
Thanks :)
Your class library will always attempt to read from the app.config of the main application that references it. It will not use your class library config file at all.

Using Indexable Properties with i4o library

I want to define some properties on a class using the [Indexable()] attribute in order to use the class with the i4o library (http://www.codeplex.com/i4o) but when I try and compile the code Visual Studio says it cannot find the Namespace for Indexable.
Is this part of the CLR and which namespace/library do I need to get this to compile?
[Indexable()] isn't an attribute included in the .NET Framework. It was removed from i4o in favor of other approaches. See this blog post for details on how to use it.
Its not a part of the standard framework. You're going to need to identify what assembly contains this IndexableAttribute and reference it in your project.
Where is it located? I don't know. Who told you you needed to do this? Is it i4o? If so, its probably within one of the binaries that comes with the i4o project.

Categories

Resources