SharedResource not found in .NetCore 6 MVC - .net-core

I am trying to do localication on .Net Core 6, MVC application. I could have done in view section, but I can not have a sharedResource.resx. so I did as following
in Startup , in ConfigureServices I added the following
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
options => { options.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource));
});
then in Configure Method I added the following
var supportedCulture = new List<CultureInfo>()
{
new CultureInfo("fa-IR"),
new CultureInfo("en-US")
};
var options = new RequestLocalizationOptions()
{
SupportedCultures = supportedCulture,
SupportedUICultures = supportedCulture,
DefaultRequestCulture = new RequestCulture("fa-IR"),
RequestCultureProviders = new List<IRequestCultureProvider>()
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
}
};
app.UseRequestLocalization(options);
Then I created the class File named "SharedResource" in Model folder of the application.
Then I created a folder named "Resources" on root level of the application.
Exactly under the Resources Folder I created two resource files. name SharedResource.en-US and SharedResource.fa-IR, with elements inside of them.
The I addedd these lines above the view
#using System.Globalization
#using Microsoft.AspNetCore.Mvc.Localization
#inject IHtmlLocalizer<SharedResource> SharedLocalizer
but when I want to have the value of #SharedLocalizer["Year"] , it doesn't work and shows that it has not found the sharedresource
[![enter image description here][1]][1]
where is the my problem?
[1]: https://i.stack.imgur.com/nuG56.jpg

the class SharedResource class file should be exactly under the root directory. that solved the problem.

Related

How do I create a CSharpCompilation using Roslyn in Blazor WebAssembly?

I am trying to write a Blazor WebAssembly (WASM) app that accepts some code (from some text input field) and compiles the code using Roslyn.
I'm using Roslyn's CSharpCompilation class to create the compilation. Its Create method takes four parameters, one of which is a list of MetadataReferences (aka assembly references). In other (non-blazor) type applications, like a C# console app, you could get these MetadataReferences based on Asssembly Location, like this:
var locatedAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !string.IsNullOrEmpty(a.Location)).ToArray();
foreach (var assembly in locatedAssemblies)
{
MetadataReference reference = MetadataReference.CreateFromFile(assembly.Location);
}
This unfortunately no longer works in Blazor WASM, because the Locations of the assemblies are empty.
I had tried getting assemblies in different ways, like AppDomain.CurrentDomain.GetAssemblies() and Assembly.GetEntryAssembly().GetReferencedAssemblies(), but all had empty Locations. I also tried calling Assembly.Load(), but to no avail.
Does anyone know how to get MetadataReferences in Blazor WASM, or how I would otherwise create a compilation in Blazor WASM?
(I'm also aware of MetadataReference.CreateFromStream() that I'll probably need to use, but it still requires the assembly location).
Thanks in advance.
I also wanted to compile C# inside a Blazor WASM app and found your question without an answer. After some digging I was able to create a working demo (repo link below.) Basically get the bytes for each assembly with HttpClient and use MetadataReference.CreateFromImage(bytes).
Full basic example repo I created: https://github.com/LostBeard/BlazorWASMScriptLoader
ScriptLoaderService.cs source:
using Microsoft.AspNetCore.Components;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Immutable;
using System.Reflection;
namespace BlazorWASMScriptLoader
{
// requires "Microsoft.CodeAnalysis.CSharp"
// can be added via nuget
public class ScriptLoaderService
{
HttpClient _httpClient = new HttpClient();
public ScriptLoaderService(NavigationManager navigationManager)
{
_httpClient.BaseAddress = new Uri(navigationManager.BaseUri);
}
async Task<MetadataReference?> GetAssemblyMetadataReference(Assembly assembly)
{
MetadataReference? ret = null;
var assmeblyName = assembly.GetName().Name;
var assemblyUrl = $"./_framework/{assmeblyName}.dll";
try
{
var tmp = await _httpClient.GetAsync(assemblyUrl);
if (tmp.IsSuccessStatusCode)
{
var bytes = await tmp.Content.ReadAsByteArrayAsync();
ret = MetadataReference.CreateFromImage(bytes);
}
}
catch { }
return ret;
}
public async Task<Assembly?> CompileToDLLAssembly(string sourceCode, string assemblyName = "")
{
if (string.IsNullOrEmpty(assemblyName)) assemblyName = Path.GetRandomFileName();
var codeString = SourceText.From(sourceCode);
var options = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp11);
var parsedSyntaxTree = SyntaxFactory.ParseSyntaxTree(codeString, options);
var appAssemblies = Assembly.GetEntryAssembly()?.GetReferencedAssemblies().Select(o => Assembly.Load(o)).ToList();
appAssemblies.Add(typeof(object).Assembly);
var references = new List<MetadataReference>();
foreach (var assembly in appAssemblies)
{
var metadataReference = await GetAssemblyMetadataReference(assembly);
if (metadataReference == null)
{
// assembly may be located elsewhere ... handle if needed
continue;
}
var metadataReferene = metadataReference;
references.Add(metadataReferene);
}
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
syntaxTrees: new[] { parsedSyntaxTree },
references: references,
options: new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
concurrentBuild: false,
optimizationLevel: OptimizationLevel.Debug
)
);
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
{
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
return null;
}
else
{
ms.Seek(0, SeekOrigin.Begin);
var assembly = Assembly.Load(ms.ToArray());
return assembly;
}
}
}
}
}

C# .net-core Web API Serilog: log files to different folders based on property (i.e. controller name)

Our client wants log files stored in separate folders based on the controller. For example, all logs that hit the Shipping controller would be stored in C:\logs\shipping\ while those that hit the orders controller would be in c:\logs\orders and so on. Below is my ConfigureLoggingServices method. I'm using Serilog and writing to a file and to Seq. I'm using middleware to capture the client user and session ID (stored in the request header) and pushing these values to the log entry with LogContext.PushProperty(). I somehow need to inject the name of the controller into the path of the log file. Is this possible? Thanks
private void ConfigureLoggingServices()
{
var appName = Configuration.GetValue<string>("Logging:AppName", string.Empty);
var SeqURL = Configuration.GetValue<string>("Logging:SeqURL", string.Empty);
var pool = Environment.UserName;
//string logFile = "C:\\Logs\\" + {Controller} + "\\lis_api.log";
string logFile = "C:\\Logs\\lis_api.log";
const string customTemplate = "[{LIS_User} {Timestamp:HH:mm:ss.fff} {Level:u3} {SessionID}] {RequestMethod} {RequestPath} {Message:lj}{NewLine}{Exception}";
var name = Assembly.GetExecutingAssembly().GetName();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Enrich.FromLogContext()
.Enrich.WithMachineName()
.Enrich.WithProperty("Application", appName)
.Enrich.WithProperty("Version", $"{name.Version}")
.Enrich.WithProperty("AppPool", pool)
.Destructure.ByTransforming<User>(x => new { x.ID, x.Name, x.Controller })
// File Sink - Async
.WriteTo.Async(a => a.
File(string.Format(string.Format(logFile)),
rollingInterval: RollingInterval.Day,
outputTemplate: customTemplate,
fileSizeLimitBytes: 40000000,
shared: true,
retainedFileCountLimit: 50,
rollOnFileSizeLimit: true))
.WriteTo.Seq(SeqURL)
.CreateLogger();
LoggerFactory = CreateLoggerFactory();
}
.WriteTo.Map(
"Controller",
"(None)",
(ctrl, wt) => wt.File($"C:\\Logs\\{ctrl}\\lis_api.log"))

Modify contents of static files

how can I modify the response before it is sent to the client when I use Microsoft.Owin.StaticFiles?
FileServerOptions options = new FileServerOptions();
options.FileSystem = new Microsoft.Owin.FileSystems.PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, "Content/"));
options.DefaultFilesOptions.DefaultFileNames = new string[] { "index.htm", "index.html" };
options.StaticFileOptions.OnPrepareResponse = (r) =>
{
r.OwinContext.Response.WriteAsync("test");
};
options.EnableDefaultFiles = true;
app.UseFileServer(options);
"test" is never written into the response. I tried to use another middleware which waits until the StaticFiles Middleware is executed:
app.Use((ctx, next) =>
{
return next().ContinueWith(task =>
{
return ctx.Response.WriteAsync("Hello World!");
});
});
FileServerOptions options = new FileServerOptions();
options.FileSystem = new Microsoft.Owin.FileSystems.PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, "Content/"));
options.DefaultFilesOptions.DefaultFileNames = new string[] { "index.htm", "index.html" };
options.EnableDefaultFiles = true;
app.UseFileServer(options);
But this didn't work. How can I modify the response?
On prepare response is not meant to modify the content of a static file.
You are only allowed to add the header.
I needed to pass some variable that change to a static web page and I got around it by using
On prepare response and passed the variables as cookies for the page.
This works nicely for a few variables but if you want to change a page significantly you are better of using mvc components.
appBuilder.UseStaticFiles(new StaticFileOptions()
{
RequestPath = new PathString(baseUrl),
FileSystem = new PhysicalFileSystem(staticFilesLocation),
ContentTypeProvider = new JsonContentTypeProvider(),
OnPrepareResponse = r => r.OwinContext.Response.Cookies.Append("baseUrl",_webhostUrl)
});

Moq Parent does not have a default constructor. The default constructor must be explicitly defined

I am using entity framework db first approach and generated edmx file.
I am trying to implement enter code here Unit testing with MOQ framework.
Here is my code
[TestMethod]
public void DeleteApplication()
{
var mockContext = new Mock<ARMSContext>();
var data = GetMemoryApplications();
var mockSet = new Mock<DbSet<Application>>();
mockSet.As<IQueryable<Application>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Application>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Application>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Application>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var test=appcontroller.Delete(1, 1);
}
I am getting the Parent does not have.. exception while calling mockSet.Object.
could you pls to resolve this exception?
From what you have given, I can't see how you are using Mock<DbSet<Application>>, nor Mock<ARMSContext>.
Here is the sample given from MSDNs "Testing with a mocking framework (EF6 onwards)", which hooks the DbSet up to the Context:
[TestMethod]
public void GetAllBlogs_orders_by_name()
{
var data = new List<Blog>
{
new Blog { Name = "BBB" },
new Blog { Name = "ZZZ" },
new Blog { Name = "AAA" },
}.AsQueryable();
var mockSet = new Mock<DbSet<Blog>>();
mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<BloggingContext>();
mockContext.Setup(c => c.Blogs).Returns(mockSet.Object);
var service = new BlogService(mockContext.Object);
var blogs = service.GetAllBlogs();
Assert.AreEqual(3, blogs.Count);
Assert.AreEqual("AAA", blogs[0].Name);
Assert.AreEqual("BBB", blogs[1].Name);
Assert.AreEqual("ZZZ", blogs[2].Name);
}
}

How to unit test code that uses HostingEnvironment.MapPath

I have some code that uses HostingEnvironment.MapPath which I would like to unit test.
How can I setup HostingEnvironment so that it returns a path and not null in my unit test (mstest) project?
Why would you have a code that depends on HostingEnvironment.MapPath in an ASP.NET MVC application where you have access to objects like HttpServerUtilityBase which allow you to achieve this and which can be easily mocked and unit tested?
Let's take an example: a controller action which uses the abstract Server class that we want to unit test:
public class HomeController : Controller
{
public ActionResult Index()
{
var file = Server.MapPath("~/App_Data/foo.txt");
return View((object)file);
}
}
Now, there are many ways to unit test this controller action. Personally I like using the MVcContrib.TestHelper.
But let's see how we can do this using a mocking framework out-of-the-box. I use Rhino Mocks for this example:
[TestMethod]
public void Index_Action_Should_Calculate_And_Pass_The_Physical_Path_Of_Foo_As_View_Model()
{
// arrange
var sut = new HomeController();
var server = MockRepository.GeneratePartialMock<HttpServerUtilityBase>();
var context = MockRepository.GeneratePartialMock<HttpContextBase>();
context.Expect(x => x.Server).Return(server);
var expected = #"c:\work\App_Data\foo.txt";
server.Expect(x => x.MapPath("~/App_Data/foo.txt")).Return(expected);
var requestContext = new RequestContext(context, new RouteData());
sut.ControllerContext = new ControllerContext(requestContext, sut);
// act
var actual = sut.Index();
// assert
var viewResult = actual as ViewResult;
Assert.AreEqual(viewResult.Model, expected);
}
Well I was writing a test today for code that I don't control and they used
private static String GetApplicationPath()
{
return HostingEnvironment.ApplicationVirtualPath.TrimEnd('/');
}
so here is a C# reflection hack to set that value
var path = "/aaaa/bb";
HostingEnvironment hostingEnvironment;
if (HostingEnvironment.IsHosted.isFalse())
new HostingEnvironment();
hostingEnvironment = (HostingEnvironment)typeof(HostingEnvironment).fieldValue("_theHostingEnvironment");
var virtualPath = "System.Web".assembly()
.type("VirtualPath").ctor();
virtualPath.field("_virtualPath", path);
//return virtualPath.prop("VirtualPathString");
//return virtualPath.prop("VirtualPathStringNoTrailingSlash");
hostingEnvironment.field("_appVirtualPath", virtualPath);
//hostingEnvironment.field("_appVirtualPath") == virtualPath;
return HostingEnvironment.ApplicationVirtualPath == path;
//using System.Web.Hosting
It will depend on what mocking or isolation framework you are using. You might want to look into either a) creating a wrapper type around the static property that can be mocked, or b) using a framework which can mock static properties - e.g. Moles or Typemock Isolator
As i faced same issue i changed my code bit.
From
strhtmlTemplate = File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath(Lgetfilepath.CVal));
To
strhtmlTemplate = File.ReadAllText(HttpContextFactory.Current.Server.MapPath(Lgetfilepath.CVal));
For Unit test
public HttpContextBase mockHttpContextBase()
{
var moqContext = new Mock<HttpContextBase>();
var moqRequest = new Mock<HttpRequestBase>();
var moqServer = new Mock<HttpServerUtilityBase>();
var moqPath = new Mock<ConfigurationBase>();
moqContext.Setup(x => x.Request).Returns(moqRequest.Object);
moqContext.Setup(x => x.Server.MapPath(#"~\Data\xxxxxxx")).Returns(Environment.CurrentDirectory+#"\xxxxxx");
setupApplication(moqContext);
return moqContext.Object;
}
Now we while Writing TestClass you need to refer above method to mock. Hope it will helpful for your TestCases.
MockDataUT mockData = new MockDataUT();
var mockRequestContext = new HttpRequestContext();
HttpContextFactory.SetCurrentContext(mockData.mockHttpContextBase());
Just use this code..
Make a new folder name Reference in root directory and added your file inside this folder.
Use this
public static XElement GetFile()
{
HttpContext.Current = new HttpContext(new HttpRequest("", "http://www.google.com", ""), new HttpResponse(new StringWriter()));
var doc = new XmlDocument();
var file = HttpContext.Current.Server.MapPath("\\") + "abc.xml";
doc.Load(file);
var e = XElement.Load(new XmlNodeReader(doc));
return e;
}

Resources