resx files for DNX 4.5.1 ConsoleApp - asp.net-web-api2

resx files are not working with DNX 4.5.1 ConsoleApp
I added a folder "Resources" and two resx files "Resource.resx" and "Resource.de-DE.resx".
"Resource.resx" contains a string with the name "T" and the value "US".
"Resource.de-DE.resx" contains a string with the name "T" and the value "DE".
now I'm using this code:
public static void Main(string[] args)
{
Console.WriteLine(Resources.Resource.T);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
Console.WriteLine(Resources.Resource.T);
}
The output is:
US
US
The same code works perfectly in a .Net 4.5.1 application.

This seems to be a bug, if you run the application with "dnx run" it works, if you run it in VS it won't. See: https://github.com/aspnet/Localization/issues/151

Related

Setting the version number for .NET Core projects

What are the options for setting a project version with .NET Core / ASP.NET Core projects?
Found so far:
Set the version property in project.json. Source: DNX Overview, Working with DNX projects. This seems to set the AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion unless overridden by an attribute (see next point).
Setting the AssemblyVersion, AssemblyFileVersion, AssemblyInformationalVersion attributes also seems to work and override the version property specified in project.json.
For example, including 'version':'4.1.1-*' in project.json and setting [assembly:AssemblyFileVersion("4.3.5.0")] in a .cs file will result in AssemblyVersion=4.1.1.0, AssemblyInformationalVersion=4.1.1.0 and AssemblyFileVersion=4.3.5.0
Is setting the version number via attributes, e.g. AssemblyFileVersion, still supported?
Have I missed something - are there other ways?
Context
The scenario I'm looking at is sharing a single version number between multiple related projects. Some of the projects are using .NET Core (project.json), others are using the full .NET Framework (.csproj). All are logically part of a single system and versioned together.
The strategy we used up until now is having a SharedAssemblyInfo.cs file at the root of our solution with the AssemblyVersion and AssemblyFileVersion attributes. The projects include a link to the file.
I'm looking for ways to achieve the same result with .NET Core projects, i.e. have a single file to modify.
You can create a Directory.Build.props file in the root/parent folder of your projects and set the version information there.
However, now you can add a new property to every project in one step by defining it in a single file called Directory.Build.props in the root folder that contains your source. When MSBuild runs, Microsoft.Common.props searches your directory structure for the Directory.Build.props file (and Microsoft.Common.targets looks for Directory.Build.targets). If it finds one, it imports the property. Directory.Build.props is a user-defined file that provides customizations to projects under a directory.
For example:
<Project>
<PropertyGroup>
<Version>0.0.0.0</Version>
<FileVersion>0.0.0.0</FileVersion>
<InformationalVersion>0.0.0.0.myversion</InformationalVersion>
</PropertyGroup>
</Project>
Another option for setting version info when calling build or publish is to use the undocumented /p option.
dotnet command internally passes these flags to MSBuild.
Example:
dotnet publish ./MyProject.csproj /p:Version="1.2.3" /p:InformationalVersion="1.2.3-qa"
See here for more information: https://github.com/dotnet/docs/issues/7568
Not sure if this helps, but you can set version suffixes at publish time. Our versions are usually datetime driven, so that developers don't have to remember to update them.
If your json has something like "1.0-*"
"dotnet publish --version-suffix 2016.01.02" will make it "1.0-2016.01.02".
It's important to stick to "semvar" standards, or else you'll get errors. Dotnet publish will tell you.
Why not just change the value in the project.json file. Using CakeBuild you could do something like this (optimizations probably possible)
Task("Bump").Does(() => {
var files = GetFiles(config.SrcDir + "**/project.json");
foreach(var file in files)
{
Information("Processing: {0}", file);
var path = file.ToString();
var trg = new StringBuilder();
var regExVersion = new System.Text.RegularExpressions.Regex("\"version\":(\\s)?\"0.0.0-\\*\",");
using (var src = System.IO.File.OpenRead(path))
{
using (var reader = new StreamReader(src))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if(line == null)
continue;
line = regExVersion.Replace(line, string.Format("\"version\": \"{0}\",", config.SemVer));
trg.AppendLine(line);
}
}
}
System.IO.File.WriteAllText(path, trg.ToString());
}
});
Then if you have e.g. a UnitTest project that takes a dependency on the project, use "*" for dependency resolution.
Also, do the bump before doing dotnet restore. My order is as follows:
Task("Default")
.IsDependentOn("InitOutDir")
.IsDependentOn("Bump")
.IsDependentOn("Restore")
.IsDependentOn("Build")
.IsDependentOn("UnitTest");
Task("CI")
.IsDependentOn("Default")
.IsDependentOn("Pack");
Link to full build script: https://github.com/danielwertheim/Ensure.That/blob/3a278f05d940d9994f0fde9266c6f2c41900a884/build.cake
The actual values, e.g. the version is coming from importing a separate build.config file, in the build script:
#load "./buildconfig.cake"
var config = BuildConfig.Create(Context, BuildSystem);
The config file looks like this (taken from https://github.com/danielwertheim/Ensure.That/blob/3a278f05d940d9994f0fde9266c6f2c41900a884/buildconfig.cake):
public class BuildConfig
{
private const string Version = "5.0.0";
public readonly string SrcDir = "./src/";
public readonly string OutDir = "./build/";
public string Target { get; private set; }
public string Branch { get; private set; }
public string SemVer { get; private set; }
public string BuildProfile { get; private set; }
public bool IsTeamCityBuild { get; private set; }
public static BuildConfig Create(
ICakeContext context,
BuildSystem buildSystem)
{
if (context == null)
throw new ArgumentNullException("context");
var target = context.Argument("target", "Default");
var branch = context.Argument("branch", string.Empty);
var branchIsRelease = branch.ToLower() == "release";
var buildRevision = context.Argument("buildrevision", "0");
return new BuildConfig
{
Target = target,
Branch = branch,
SemVer = Version + (branchIsRelease ? string.Empty : "-b" + buildRevision),
BuildProfile = context.Argument("configuration", "Release"),
IsTeamCityBuild = buildSystem.TeamCity.IsRunningOnTeamCity
};
}
}
If you still want to have the Solution Level SharedVersionInfo.cs you can do it by adding these lines to your project.json file:
"buildOptions": {
"compile": {
"includeFiles": [
"../../SharedVersionInfo.cs"
]
}
}
Your relative path may vary, of course.
use external version.txt file with version, and prebuild step to publish this version in projects

Json.Net.Schema error when two string arrays

I get errors when I try to print a schema with two string[] or two List . A string[] and a List are ok .
having this in target class.
public string[] ovoList;
public string[] procList;
Causes an error on lines where schema is converted to string not where it is generated.
static void Main(){
JSchemaGenerator generator = new JSchemaGenerator();
JSchema schema = generator.Generate(typeof(UNIKK.UIEngine.UIFrame));
//Error is thrown on two lines below
Console.WriteLine(schema);
File.WriteAllText(#"OVOSchema.json", schema.ToString());
I tried with both Newtonsoft.JSON 6.0.8 and latest 7.x and with Newtonsoft.JSON.Schema 1.0.11 I grabbed them with nuget and am running on
Xamarin Studio
Version 5.9.5 (build 10)
Mono 4.0.3 ((detached/d6946b4)
on OS X
Errors trace is
System.Uri.EnsureAbsoluteUri () in /private/tmp/source-mono-mac-4.0.0-branch-c5sr3/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.3/mcs/class/System/System/Uri.cs:2062
System.Uri.GetComponents (components=System.UriComponents.Host|System.UriComponents.Port|System.UriComponents.Scheme|System.UriComponents.UserInfo, format=System.UriFormat.Unescaped) in /private/tmp/source-mono-mac-4.0.0-branch-c5sr3/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.3/mcs/class/System/System/Uri.cs:1731
System.Uri.Compare (uri1={#}, uri2={#/properties/ovoList}, partsToCompare=System.UriComponents.Host|System.UriComponents.Port|System.UriComponents.Scheme|System.UriComponents.UserInfo, compareFormat=System.UriFormat.Unescaped, comparisonType=System.StringComparison.InvariantCultureIgnoreCase) in /private/tmp/source-mono-mac-4.0.0-branch-c5sr3/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.3/mcs/class/System/System/Uri.cs:1768
System.UriParser.IsBaseOf (baseUri={#}, relativeUri={#/properties/ovoList}) in /private/tmp/source-mono-mac-4.0.0-branch-c5sr3/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.3/mcs/class/System/System/UriParser.cs:208
System.Uri.IsBaseOf (uri={#/properties/ovoList}) in /private/tmp/source-mono-mac-4.0.0-branch-c5sr3/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.3/mcs/class/System/System/Uri.cs:1740
The problem is that there is not Id. The scheme.ToString function wants procList properties to reference the ovoList properties but it is unable to create a ref because there is no base URI for the schema.
adding
schema.Id = new Uri ("http://www.example.com/");
Solved this.

I can't extract files in program files folder

I'm working on custom action and wix.The files are not extracting in program files (x86) folder.But the files are extracting correctly other than program files (x86). I have written code using .NET FRAMEWORK 4.0.
namespace Installer
{
public class CustomActions
{
[CustomAction]
public static ActionResult CustomAction1(Session session)
{
session.Log("Begin Extracting");
string FinalPath = session["APPDIR"];``
string zPath = #"C:\Users\AppData\Local\Temp\Install\7za.exe";
string ExtractPath = #"C:\Program Files (x86)\Samples\";
string sourcePath = #"C:\Program Files (x86)\Samples\source.zip";
try`
{
ProcessStartInfo pro = new ProcessStartInfo();``
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = "x \"" + sourcePath + "\" -o" + ExtractPath;
Process x = Process.Start(pro);
x.WaitForExit();
}
catch (System.Exception Ex)
{
}
return ActionResult.Success;
}
}
}
First of all you need to debug it properly. You're throwing away any error that might be thrown. Sorry to say this, but your question is unfortunately more like "how can I find out why my code is not working when I've thrown away any exceptions it might raise?"
There's no guarantee that the zip extension will work correctly just by starting it. It might work if WinZip is installed, but not if all that happens is that Explorer opens to look at the files.
You should use the classes that will unzip it. Example here:
https://msdn.microsoft.com/en-us/library/ms404280(v=vs.110).aspx

asp.net mvc 4: adding parameter to Scripts.Render path

I would like to do the following:
#Scripts.Render("~/bundles/jquery?version=1"])
the version value has to be dynamic and should match the value defined in a cookie.
How can I add this parameter to Scripts.Render ?
I've tried something like that with jQuery but with no luck:
#Scripts.Render("~/bundles/jquery?version=" + $.cookie('version'))
Replace
#Scripts.Render("~/bundles/jquery?version=1"])
with
#{string version = 1}
#Scripts.RenderFormat("<script type=\"text/javascript\" src=\"{0}?nocache="+ version +"\"></script>", "~/bundles/jquery")
as shown in this post: http://www.jomendez.com/2016/05/26/how-to-avoid-js-files-cache-script-bundle-with-razor/
By default MVC optimisations will automatically add a version parameter to the bundle links for release builds but not for debug. E.g. when you deploy your site, the link to /bundles/modernizr becomes something like /bundles/modernizr?v=inCVuEFe6J4Q07A0AcRsbJic and the JavaScript is minified.
If one of the files in the bundle has changed the parameter changes on next deployment, hence linked files are cached by browsers but reloaded from the server when they have changed in a new release.
For easier debugging optimisations are disabled in debug (= no version parameter added and no minified code). If you want to override this you can set the compilation debug attribute to false in web.config, or you can enable optimizations in code, like so:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
// Code removed for clarity.
BundleTable.EnableOptimizations = true;
}
For full details see
http://www.asp.net/mvc/overview/performance/bundling-and-minification, in particular the sections "Controlling Bundling and Minification" and "Bundle Caching".
For all I know #Scripts.Render("~/bundles/jquery") is not a path. It's just name. So if you want to use different versions you should create two bundles in your BundleConfig:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery-ver191").Include(
"~/Scripts/jquery-1.9.1js"));
bundles.Add(new ScriptBundle("~/bundles/jquery-ver202").Include(
"~/Scripts/jquery-2.0.2js"));
}
Than you can call necessary version in your view:
#Scripts.Render("~/bundles/jquery-ver191")
or
#Scripts.Render("~/bundles/jquery-ver202")
EDITED: On your comment: But this default code in BundleConfig is the same you want.
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
Or you don't have an oppurtunity to delete other version exept newest?
Create a mvc helper that changes the html generated and adds the version. The follow example works when a single file or multiple files (debug mode) are generated.
public static HtmlString GetScriptsWithVersion()
{
const string VERSION = "2.0.1"; //or get the version where you want
const string SCRIPT_END = "\"></script>";
string html = Scripts.Render("~/bundles/ui").ToString();
string versionParam = "?v=" + VERSION
html = html.Replace(SCRIPT_END, versionParam + SCRIPT_END);
return new HtmlString(html);
}

Getting assemblyinfo of a feature in Sharepoint

I have a method that collects the assemblyversion of a webpart. (works fine) :
private void GetVersion(object control, out string name, out string version)
{
name= control.GetType().ToString();
version = control.GetType().Assembly.GetName().Version;
}
Now I want to achieve the same for my features:
private void GetFeatureVersion(SPFeature feature, out string name, out string version)
{
name = feature.Definition.GetTitle(new System.Globalization.CultureInfo("en-us"));
version = feature.GetType().Assembly.GetName().Version;
}
But in the Assembly of feature.GetType() isn't the information of my feature, but of sharepoint (14.0.0.0). The name var is fine but thats no surprise as it is not read out of the type.
I added the following to the template.xml - File.
ReceiverAssembly="$SharePoint.Project.AssemblyFullName$"
That did the trick
If you want to get the version of the feature receiver assembly you can do the following:
string version = Assembly.Load(feature.Definition.ReceiverAssembly).GetName().Version;