Get value of version out of project.json - c#

In a DNX application, which uses a "project.json" file, is there a way to read the value of the "version" property out of the "project.json" file?
I'm writing a library that writes something to the current HTTP response and I would like to show the version of the application in there.
Any help on how this can be done is highly appreciated.

If you set the version attribute during build (or in any other way) you can do this like that:
using System;
using System.Reflection;
[assembly:AssemblyVersionAttribute("1.2.3")]
namespace Test
{
class Program
{
public static void Main()
{
var assembly = typeof(Program).GetTypeInfo().Assembly;
var name = assembly.GetName();
Console.WriteLine($"{name.Name}: {name.Version}");
}
}
}
I did it using the new dotnet cli which is replacing dnx but it should work with dnx dnxcore50 as well.

Are you writing a Class Library or an ASP.NET application?
If a class Library, you could copy the version string to a resource file that you read in during run-time to grab the version. It's kind hard to do this sort of thing with class libraries since you don't get the beauty of a Startup and IoC.
If ASP.NET, then just add a version into your appsettings.json configuration (or a custom json file to store settings) and read it in at startup: http://docs.asp.net/en/latest/fundamentals/configuration.html

Multipe ways of doing this if you are running in a the web application, not a class library.
First way custom attributes data (should check if attribute is available):
this.GetType().Assembly.GetCustomAttributesData()
.First(x => x.AttributeType.FullName == "System.Reflection.AssemblyInformationalVersionAttribute")
.ConstructorArguments[0];
Second way
var name = this.GetType().AssemblyQualifiedName;
name = name.Substring(name.IndexOf("Version=") + 8);
var verion = name.Substring(0, name.IndexOf(", "));

Related

Why does ConfigurationManager not contain a definition for OpenExeConfiguration? C#

I am sort of new to config files. I am aware that I can add key value pairs and that it's possible to access them and change them on the fly. I am attempting to implement the ChangeConfiguration method on https://blogs.msdn.microsoft.com/youssefm/2010/01/21/how-to-change-net-configuration-files-at-runtime-including-for-wcf/
However, I am getting:
"'ConfigurationManager' does not contain a definition for 'OpenExeConfiguration'"
...and I get the same for trying to use ConfigurationManager.RefreshSection()
I am aware that the instructions date back to 2010 so by the looks of it, these instructions seem to no longer be the correct procedure to do this...?
Context
Web UI tests using Specflow, Selenium WebDriver, NUnit
Class Library targeting .NET Framework 4.6.1
Trying to add key value pairs at runtime in App.config
using System.Configuration;
using System.Reflection;
namespace CoreSeleniumFramework.Managers
{
public class ConfigurationManager
{
static void ChangeConfiguration()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings.Clear();
appSettings.Settings.Add("name", "bar");
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
}
Answered by #Nastaran Hakimi
Ok so this is a thing... need to use...
System.Configuration.ConfigurationManager.OpenExeConfiguration
...when it seems I should just need...
using System.Configuration;
Edit: if you get "object reference not set to an instance of an object" (or in other words, GetEntryAssembly() returns null), use GetCallingAssembly()

Including a file when I publish my Azure function in Visual Studio

I know this seems like a simple thing but I can't find any help online.
I want to include a file (.html) along with my Azure function when I publish it using Visual Studio. Then I want to be able to access this file in my Azure function.
Why? It seems like only the .dll gets sent to the server when I publish.
This file will be an .html file that will be an email template. I want to read it in my function and then send emails out.
Any help is much appreciated.
I see I can use [send grid in Azure functions][1], but it looks like I can only send out one email and not multiple emails, which is what I want.
First, you need to add the html file to your project, and in the properties, set Copy to Output Directory to "Copy if newer".
Then in your function code, take in an additional ExecutionContext context parameter (note that this is Microsoft.Azure.WebJobs.ExecutionContext and not System.Threading.ExecutionContext). And when you need to access your html file, you can then write:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "test.html");
That's assuming you added the file at the root of your VS project. If you instead added it in some Data folder (better practice), you'd write:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "Data", "test.html");
See here for full working sample.
I have the same scenario as you have. However, I cannot access ExecutionContext because it is only available in requests. My scenario needs to get the template included in AzFunc project but not in the context of AzFunc's functions. I got it null when I go with the interface - implementation class approach.
Thanks to this guy, I use IOptions<ExecutionContextOptions> in my class to get the root directory of the Azure Func.
My Azure Func project (NET 6, Azure Function v4)
using Microsoft.Extensions.Options;
using Microsoft.Azure.WebJobs.Host.Bindings;
namespace AzureFuncApi
{
public class TemplateHelper : ITemplateHelper
{
private readonly IOptions<ExecutionContextOptions> _executionContext;
public TemplateHelper (IOptions<ExecutionContextOptions> executionContext)
{
_executionContext = executionContext;
}
public string GetTemplate()
{
var context = _executionContext.Value;
var rootDir = context.AppDirectory; // <-- rootDir of AzFunc
var template = Path.Combine(rootDir, "test.html"); // <-- browse for your template. Here's an example if you place test.html right in the root of your project
// return your template here, raw, or after you do whatever you want with it...
}
}
}
My different project defines the interface and uses it there, independently of the real implementation
namespace DifferentProject
{
public interface ITemplateHelper
{
string GetTemplate(); // Use this to get the template
}
}

Xamarin PCL Refit 3.0.1 , Doesn't look like a Refit interface

I recently started working on a Xamarin Android/iOS project with a PCL class where I want to put all the logic in. Like my Refit interfaces, ReactiveUI ViewModels et cetera, but every time when trying to execute my code I get a error saying that my interface is not a Refit interface. Currently my interface looks like this.
public interface IMyApi
{
[Post("/authenticate")]
IObservable<Models.ApiResponses.AuthenticationResponse> SigninRaw([Body] JObject credentials);
[Get("/service")]
IObservable<Models.ApiResponses.MyListResponse> GetServiceListRaw();
[Get("/service/{id}/idstatus")]
IObservable<Models.ApiResponses.IdResponse> GetIdStatusRaw(string Id);
}
As far as I know this looks good and this also works when I'm trying to load this from a specific platform like iOS project. But when trying to do it from a PCL if fails! I have installed the Refit package in both of my platform specific project Android & iOS and I referenced a dll in the PCL, what did I miss?
If there is need for more information or you have any question, please do not hesitate to ask.
Well without further ado, thank you for reading and hopefully someone can assist me with this, because I starting to loose my mind the past couple of days.
Edit: added calling method.
Here I calling it from a ViewModel
var client = new HttpClient(NetCache.UserInitiated)
{
BaseAddress = new Uri("https://api.address.com")
};
var api = RestService.For<IMyApi>(client); <= here it crashes
var response = api.SigninRaw(token);
I've managed to track this down, there are actually a few issues at play. Fortunately there are ways to work around them.
The first problem is that the PCL interfaces aren't being detected in the first place. Refit runs a utility at compile time (InterfaceStubGenerator) which scans the subfolders for interface classes and generates implementation code for each one. These are all packed into an intermediate file called RestStubs.g.cs which gets included in with the assembly. This utility, however, is only run on the projects that Refit has been added to via nuget, and since that doesn't include PCL projects the interfaces in those projects never get processed. The solution is to call this utility manually in a pre-build step and include the generated file in each of the platform projects. Go to your PCL project's property settings and add the following to the pre-build steps:
..\..\..\..\packages\refit.3.0.1\tools\InterfaceStubGenerator.exe ..\..\..\ProjName.Droid\RefitStubs.cs ..\..\..\ProjName
..\..\..\..\packages\refit.3.0.1\tools\InterfaceStubGenerator.exe ..\..\..\ProjName.iOS\RefitStubs.cs ..\..\..\ProjName
That will generate RefitStubs.cs for your platform projects, so add each file to it's respective project.
Ordinarily that would be the end of it were it not for another problem. The RestService.For<> generics that you call to get the implementations make the assumption that the implementation classes reside in the same assembly as their corresponding interfaces. Obviously that isn't the case for PCL projects. To get around this you need to implement your own version of the RestService class, this will probably serve most of your needs:
public static class PclRestService
{
public static T For<T>(string hostUrl)
{
var className = "AutoGenerated" + typeof(T).Name;
var typeName = typeof(T).Namespace + "." + typeof(T).Name.Replace(typeof(T).Name, className);
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var generatedType = assembly.GetType(typeName);
var requestBuilder = RequestBuilder.ForType<T>();
var client = new HttpClient(new HttpClientHandler()) { BaseAddress = new Uri(hostUrl) };
return (T)Activator.CreateInstance(generatedType, client, requestBuilder);
}
}
You then call it like so:
var netService = PclRestService.For<IMyApi>("http://jsonplaceholder.typicode.com");
var result = await netService.GetDataOrSomething();
One last problem you may encounter is multiple declarations of the PreserveAttribute class, which is declared at the top of the stub files. So long as you don't add Refit interfaces to your platform projects this shouldn't happen, but if it does then you have 3 options:
modify the InterfaceStubGenerator utility to not create that code
write a pre-processor to strip that code out once it has been generated
go to Refit's "refit.targets" file and comment-out the line "<Exec Command="$(RefitExecCmd)" />
The Refit tools folder includes the template file used to generate stub files but for some strange reason it's ignored altogether and statically linked to the application, so editing it in the tools folder doesn't do anything at all.

How to retrieve Product Version?

Is there a way to retrieve the product version of an ASP.NET 5 web application?
This is what I have in my project.json:
"version": "4.0.0-alpha1"
How would I be able to retrieve this from within the application? I used to be able to do this on older ASP.NET versions:
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
However, now it just gives me 0.0.0.0 all the time. Any ideas?
Add a reference to System.Reflection in your project.json file if you don't already have one.
"dependencies": {
"System.Reflection": "4.1.0-beta-23516" // Current version at time of posting
}
Then, you can get the value from the AssemblyInformationalVersionAttribute InformationalVersion property.
private static string GetRuntimeVersion() =>
typeof(SomeClassInYourAssembly)
.GetTypeInfo()
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
Inject IApplicationEnvironment anywhere you need the version. So for instance in the Configure method of the Startup class:
public void Configure(IApplicationBuilder app,
IApplicationEnvironment applicationEnvironment)
{
app.UseIISPlatformHandler();
app.Run(async (context) =>
{
await context.Response.WriteAsync(applicationEnvironment.ApplicationVersion);
});
}
Source: "Services Available in Startup" http://docs.asp.net/en/latest/fundamentals/startup.html
For a command line application with .NET 6, I found this to work.
(Anything using the Assembly Location seems to fail when the application is published to a self-contained file.)
First, set the version (in this example: 1.4.3) in the properties: (search for "version")
Then use this code to get the number you put in:
using System.Linq;
using System.Reflection;
var version = typeof(Version) // "Version" is the name of any local class
.GetTypeInfo()
.Assembly
.GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.First()
.InformationalVersion;

I can't read my embedded XML file from my second C# project

I have a Visual Studio Solution with two projects inside.
The first project is a windows service, and this project contains one XML file inside a folder (called Configurations). This XML file is called Databases.xml. I have changed Databases.xml Build Action from content to embedded resource, and now I want to access this XML file from my other project in my solution, which is a WPF application.
I have therefore added an reference to my windows service project inside my WPF project, and would now like to access my XML file from my WPF project.
My problem is that when I am trying to access the embedded resource then I can't find out which type to use and what the path/namespace to my assembly and XML file should be. When I am using the
string[] names = this.GetType().Assembly.GetManifestResourceNames();
my names array is filled out with some resources from my WPF project. What I want is to access the ResourceNames and of course my Databases.xml file from my Windows Service project.
Please help me since this problem is driving me nuts.
If you need any additional information, please let me know.
My Solution Update 26-07-2013
I found out that the real problem occured when I couldn't use my first windows Service projects namespace as a type for my assembly. my Windows Service consists of a service class (with OnStart() and OnStop() method inside), and in order to use this class as my namespace type, I needed to add another reference to my WPF project. I needed to add a reference to System.ServiceProcess namespace, in order to use my Windows Service Class as a type for my assembly in my WPF Project.
In Order to access my Databases.xml file, I have come up with this solution. Remember to insert your own projects name and class name instead of my placeholders (<Windows Service Project Name> etc).
//Remember to add a reference to System.ServiceProcess in order to be able to use your WIndows Service Project as an assembly type.
using (Stream stream = typeof(<Windows Service Project Name>.<Windows Service Class Name>).Assembly.GetManifestResourceStream("<Windows Service Project Name>.<Folder Name>.Databases.xml"))
{
//Load XML File here, for instance with XmlDocument Class
XmlDocument doc = new XmlDocument();
doc.Load(stream);
}
So my real problem was that I didn't include the System.ServiceProcess reference to my second project.
You've to refer to Windows Service project Assembly to make it work
The problem with your code is This.GetType().Assesmbly gives current Assembly In your case WPF Assembly and obviously you'll not find what you need there.
Try this
Assembly windowsServiceAssembly = typeof(SomeTypeFromThatAssembly).Assembly;
string[] names = windowsServiceAssembly.GetManifestResourceNames();
Hope this helps.
If your class is static class then use this methods:
internal static string GetFromResources(string resourceName)
{
var asm = Assembly.GetExecutingAssembly();
var resource = asm.GetManifestResourceNames().First(res => res.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase));
using (var stream = asm.GetManifestResourceStream(resource))
{
if (stream == null) return string.Empty;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
For example if your embedded resource file names on the this project is 'MyFile.txt' then use this static method same this code:
var myFileData = GetFromResources("MyFile.txt");

Categories

Resources