I am attempting to use a resource(.resx) file in an ASP.NET Core project. I had it working in rc1 using a namedResource, but can't seem to get it working in RTM. Here is what my project.json looks like:
"buildOptions": {
"embed": {
"include": [ "Resources/resource.resx" ]
}
}
And I'm accessing it like this:
Resources.ResourceManager.GetString("Field1");
It seems the resources are not getting embedded as There is nothing there when I debug.
Any help would be much appreciated.
For string in resource use:
Remove embed.
var x = ResourceFoo.Field1;
For file in resource use:
Include embed for file (ex.: /Folder1/**/*)
Assembly.GetExecutingAssembly().GetManifestResourceStream("Folder1.Folder2.file.ext");
Related
I have converted my .net core 2.2 to .net core 3.1. I am getting issue with following below.
Getting complain about MvcJsonOptions inside IOptions<MvcJsonOptions> options.
Also getting issue with MvcOptions inside IOptions<MvcOptions>.
Another one is, options.SerializerSettings inside SetupSerialiserSettings(options.SerializerSettings).
public JsonDeserialiser(IOptions<MvcJsonOptions> options) : this(options.Value.SerializerSettings)
{
}
services.AddSingleton<IObjectModelValidator>(
s =>
{
var options = s.GetRequiredService<IOptions<MvcOptions>>().Value;
});
services.AddControllers()
.AddJsonOptions(options => SetupSerialiserSettings(options.SerializerSettings))
Firstly,you can refer to the link and find MvcJsonOptions only applies to 2.1, 1.0, 1.1, 2.0, 2.2.And you can also refer to Breaking changes to Microsoft.AspNetCore.App in 3.0.
In .net core 3.1,the internal usage of Json.NET in ASP.NET Core has be replaced by the new platform-provided JSON APIs.Refer to The future of JSON in .NET Core 3.0.
So you can try to add package Microsoft.AspNetCore.Mvc.NewtonsoftJson,and use like the following code(from the official doc):
services.AddControllers().AddNewtonsoftJson(options =>
{
...
});
I've jumped many hurdles, but alas I still see the default 404 iamge rendered by Nancy.
I've 'mastered' project.json so I can embed my assets (including /assets/views/index.html), using the following block in the project.json file:
"buildOptions": {
"embed": [
"assets/**",
"Content/**",
"fonts/**",
"Scripts/bootstrap.js",
"Scripts/jquery-2.2.2.js",
"Scripts/mustache.js",
"favicon.ico"
//, "Scripts/**" - include this and build fails with errors in *.targets (!)
]
}
This is confirmed when debugging and querying assembly resources. All good.
My Bootstapper contains:
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
DependencyResolver.ConfigureDependencies(container);
ResourceViewLocationProvider
.RootNamespaces
.Add(GetAssembly(), "<name to assembly>"); //embedded resource directory location
}
And yet in my IndexModule:
public class IndexModule : NancyModule
{
public IndexModule()
{
Get("/", args => Response.AsFile("assets/views/index.html", "text/html"));
}
}
This renders nothing... could I be missing anything ? It was working fine with .Net Framework
I want to integrate my project on ASP.NET Core 1.0 with Microsoft Azure KeyVault. But it seems like Microsoft.Azure.KeyVault package is not compatible yet with "netcoreapp1.0" framework (I try to download the package using NuGet package manager and the "incompatible package" error message is shown). Therefore I import "net451" framework in the project.json shown in this block:
"frameworks": {
"netcoreapp1.0": {
"imports": [
"net451",
"dotnet5.6",
"portable-net45+win8"
]
}
},
After importing "net451" framework, the error is now gone. Now I want to initiate a new KeyVaultClient class shown in this block:
public void GetKeyVaultSecret()
{
var keyVaultClient = new KeyVaultClient(this.GetTokenAsync);
// ....
}
private async Task<string> GetTokenAsync(string authority, string resource, string scope)
{
var authenticationContext = new AuthenticationContext(authority);
var authenticationResult =
await authenticationContext.AcquireTokenAsync(resource, this.clientAssertionCertificate);
return authenticationResult.AccessToken;
}
The problem is I got this error message on this.GetTokenAsync which I have search the solution for hours without any luck: Argument 1:cannot convert from 'method group' to 'KeyVaultClient.AuthenticationCallback'
If I change
var keyVaultClient = new KeyVaultClient(this.GetTokenAsync);
to:
var keyVaultClient = new KeyVaultClient((authority, resource, scope) => this.GetTokenAsync(authority, resource, string.Empty));
I still got error message: Cannot convert lambda expression to type 'KeyVaultClient.AuthenticationCallback' because it is not a delegate type
Anybody knows how to solve this problem? Thanks.
Regards,
Alvin
The error cannot convert from 'method group' is because you have overloads or extensions (ie more than one method) with the name GetTokenAsync. Try to rename one of them, and it should work.
So after a long time abandoning this question, I've decided to look into it once more thanks to #fernacolo's answer. Turns out that at that time I used version 1.0.0 of the Microsoft.Azure.KeyVault package (which was the latest version back then, shown in Figure 1). Now version 2.0.0 of the package is available and when I find the changelog, I saw this post https://learn.microsoft.com/en-us/azure/key-vault/key-vault-dotnet2api-release-notes which stated that ".NET Core is supported by the 2.0 version of the Azure Key Vault .NET/C# library".
Figure 1. Version history of Microsoft.Azure.KeyVault package
The error is now gone without the need of importing "net451" framework in the project.json.
I try to encrypt a file with RSA, but it don't have the toXmlString() and fromXmlString method. how to use RSA class in .net core ?
And I want to encrypt with private key and decrypt with public key ,so others can only read this file but can't generate this file , Is that possible ?
While the ToXmlString and FromXmlString methods are not available, ImportParameters(RSAParameters) and ExportParameters(bool) are.
If you have existing XML formatted values that you need to continue to read, the XML format of the keys isn't very interesting:
Public only:
<RSAKeyValue>
<Modulus>[base64-encoded value]</Modulus>
<Exponent>[base64-encoded value]</Exponent>
</RSAKeyValue>
Public+Private:
<RSAKeyValue>
<Modulus>[base64-encoded value]</Modulus>
<Exponent>[base64-encoded value]</Exponent>
<P>[base64-encoded value]</P>
<Q>[base64-encoded value]</Q>
<DP>[base64-encoded value]</DP>
<DQ>[base64-encoded value]</DQ>
<InverseQ>[base64-encoded value]</InverseQ>
</RSAKeyValue>
(where each named XML element maps to the field of the same name on the RSAParameters struct)
Otherwise you can/should use whatever serialization mechanism is the best suited for you and your application.
Currently you get less when using pure .net core:
In the end this means you can build on .NET Core today, and expect
more features to light up later, so things will get easier as time
goes on. Whether you want to be an early adopter with the less
featured framework now, or jump in later when more features have been
added and the third party eco-system has caught up is going to be a
(tough) decision that we all have to make on your own.
You will have them available if you target the full .net framework in .net core.
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"net461": {}
}
}
They won't be available with for example:
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
I'm trying to get an example of EF 7 with Azure Table Storage to work in VS 14 CTP3, but I am having no luck with the dependency injection stuff. I was able to get an example with SQL done fairly easily, but I am seeing an issue that doesn't make sense: The referenced package is there and being pulled in, and if I look at it, it contains the correct namespaces, methods, clases etc., but the compile doesn't like it.
Here is my project.json:
{
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-alpha3",
"EntityFramework.AzureTableStorage": "7.0.0-alpha3",
"Microsoft.AspNet.RequestContainer": "1.0.0-alpha3"
},
"frameworks" : {
"net451" : { },
"k10" : { }
}
}
using System;
using Microsoft.AspNet.Builder;
using Microsoft.Data.Entity; /* <- 'missing reference' unless I add EntityFramework to project.json */
using Microsoft.Data.Entity.AzureTableStorage; /* <- ALWAYS errors w/ 'missing reference' */
using Microsoft.Framework.DependencyInjection;
namespace WebApplication2
{
public class Startup
{
public void Configure(IBuilder app)
{
app.UseServices(services =>
{
services.AddEntityFramework() /* <-- this errors too */
.AddAzureTableStorage();
services.SetupOptions<DbContextOptions> //,- says it can't find this
(config => config.UseAzureTableStorage("UseDevelopmentStorage=true"));
});
}
}
}
The strange thing is, if I right click and 'go to definition' on any of the 'missing' classes or methods, it brings them up, and I can see that I'm using them as defined. Am I missing something terribly obvious? Or is this stuff just not fully cooked yet?
Your project.json has both frameworks mentioned so VS builds both of them. If your intention is to just build for net451, you should remove the following from your project.json -
"k10" : { }