dot net 6 use PhysicalFileProvider to get view - c#

In Linux docker and run using .NET 6, given
var executingFilePath ="/app/Views/Email/Invite.cshtml";
var viewPath ="/app/Views/Email/Invite.cshtml";
var applicationRelativePath = _razorViewEngine.GetAbsolutePath(executingFilePath, viewPath)!;
Console.WriteLine($"applicationRelativePath: {applicationRelativePath}");
var viewResult = _razorViewEngine.GetView(executingFilePath, viewPath, false);`
viewResult.Success is always false.
My docker file looks like this:
root#2824697b3537:/app# ls Views/Email/Invite.cshtml
Views/Email/Invite.cshtml
root#2824697b3537:/app# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 0.6 25351996 102772 ? Ssl 12:03 0:01 dotnet InnoYun.DotNet6.Starter.dll
root 59 0.0 0.0 4156 3308 pts/0 Ss 12:11 0:00 bash
root 66 0.0 0.0 6760 3008 pts/0 R+ 12:12 0:00 ps aux
root#2824697b3537:/app# ls /app/Views/Email/Invite.cshtml
/app/Views/Email/Invite.cshtml
ViewEngine.GetView always fails.
my Invite.cshtml is
#page
hello world
dotnetcore3 will compile the view into projectname.views.DLL
views dir is not necessary published and dot net 6 will compile an EmbeddedResource to the same projectname.dll.
I tried setting RazorViewEngineOptions to PhysicalFileProvider
like this
services.Configure<RazorViewEngineOptions>(opts =>
opts.FileProviders.Add(new PhysicalFileProvider("/app")
)
);
and still not use after I find package: Razor.Templating.Core
it can direct render cshtml to html without compile view to dll is very useful to me.

Related

Using Span<T> on ARM gives "System.DataMisalignedException:"

I am trying to read data and convert it from a Modbus RTU sensor using .NET 7. Everything is working on my Linux-x64 machine but not when i try to run it on the RevPi core S linux-arm...
Modbus RTU config
Sensor Instructions
I am using FluentModbus version 5.0.2:
fluentClient = new ModbusRtuClient()
{
BaudRate = 9600,
Parity = Parity.None,
StopBits = StopBits.One
};
fluentClient.Connect("/dev/ttyUSB" + port, ModbusEndianness.LittleEndian);
Console.WriteLine("Reading Calibration");
var calibration = fluentClient.ReadHoldingRegisters(1, 8704, 4);
var hexCalibration = Convert.ToHexString(calibration);
foreach (var item in calibration)
{
Console.Write(item);
}
foreach (var item in hexCalibration)
{
Console.Write(item);
}
//The code below works perfect on my machine running Ubuntu 22.04(linux-x64) but does not work //on RevPi Core running Debian custom Buster image (Linux-arm).
var floatCalibration = fluentClient.ReadHoldingRegisters<float>(1, 8704, 4);
Console.WriteLine("calibration K: " + floatCalibration[0]);
Console.WriteLine("calibration B: " + floatCalibration[1]);
Terminal output My computer:
Reading Calibration
000631541535764
0000003F9A993940
calibration K: 0.5
calibration B: 2.9
Terminal output RevPi Core:
Reading Calibration
000631541535764
0000003F9A993940
Unhandled exception. System.DataMisalignedException: A datatype misalignment was detected in a load or store instruction.
at exsys.Program.Main(String[] args) in /home/user/FunctionTest/Program.cs:line 179
Aborted
And this is line 179: Console.WriteLine("calibration K: " + floatCalibration[0]);
Is it not possible to use .Net7 Span<float> on linux Arm? I have tried different setups, but no luck yet. Maybe someone else have run in to this issue?
Tried dotnet publish to self contained linux-x64 and everything works fine. i only get the error when compiling to linux-arm and running it.
Tried different converting setups but no luck.
I have updated the image on the RevPi Core S released in November 2022, but it did not help.
Tried first running on .NET 6 and now on .NET 7. Same error on both.
FluentModbus is breaking alignment here:
https://github.com/Apollo3zehn/FluentModbus/blob/master/src/FluentModbus/Client/ModbusClient.cs#L155
and then calls MemoryMarshal.Cast which is a problem on ARM, because the documentation says
This method is supported only on platforms that support misaligned memory access or when the memory block is aligned by other means.
ARM is not a platform that supports misaligned memory access.
A good workaround would be to allocate a float[], call the version of ReadHoldingRegisters that returns Span<byte>, and call CopyTo(MemoryMarshal.AsBytes(destFloatArray)).

What is the syntax to use part of the version from CSPROJ and AppVeyor {build} to get a 2.0.x style build number?

Is it possible to have AppVeyor builds use the build number from the csproj file but replace the 'patch' version with the {build} variable?
For Example:
CSPROJ: <Version>2.1.0</Version>
AppVeyor: ??? What goes here? Manually setting 2.1.{build} will work but then to rev to 2.2, we have to update appveyor AND csproj.
Output: 2.1.15 (assuming build number is 15)
Is this possible with the build-in AppVeyor Patching system?
This should work. Just replace subfolder and file name in $xmlPath
install:
- ps: |
$xmlPath = "$env:appveyor_build_folder\mysubfolder\myproject.csproj"
$xml = [xml](get-content $xmlPath)
$version = ($xml.Project.PropertyGroup | ? {$_.Version}).Version
$env:newversion = ($version.Substring(0, $version.LastIndexOf(".") + 1)) + $env:appveyor_build_number
dotnet_csproj:
patch: true
file: '**\*.csproj'
version: $(newversion)
package_version: $(newversion)
assembly_version: $(newversion)
file_version: $(newversion)
informational_version: $(newversion)

Not able to Publish the ASP.Net app in Visual studio

I am trying to publish the app from Visual-Studio but I am getting the error:
The command "node node_modules/webpack/bin/webpack.js --env.prod"
exited with code 1. first azure app
C:...\firstazureapp
C:...firstazureapp\firstazureapp.csproj 497
I have followed this Article
Getting Started With Angular 5 And ASP.NET Core
Everything is working fine but at publish time only I am getting the error.
I have run this command In the node modules folder and it is giving these issue
C:\Users\acer\source\repos\testfromcmd>node node_modules/webpack/bin/webpack.js --env.prod
Hash: 40bd2f76867df4f7cc31ffb9aab17185511f568b
Version: webpack 2.5.1
Child
Hash: 40bd2f76867df4f7cc31
Time: 26026ms
Asset Size Chunks Chunk Names
main-client.js 1.73 MB 0 [emitted] [big] main-client
ERROR in main-client.js from UglifyJs
TypeError: Cannot read property 'reset' of undefined
at filterdFiles.forEach (C:\Users\acer\source\repos\testfromcmd\node_modules\webpack\lib\optimize\UglifyJsPlugin.js:81:21)
at Array.forEach ()
at Compilation.compilation.plugin (C:\Users\acer\source\repos\testfromcmd\node_modules\webpack\lib\optimize\UglifyJsPlugin.js:40:18)
at Compilation.applyPluginsAsyncSeries (C:\Users\acer\source\repos\testfromcmd\node_modules\tapable\lib\Tapable.js:142:13)
at self.applyPluginsAsync.err (C:\Users\acer\source\repos\testfromcmd\node_modules\webpack\lib\Compilation.js:635:10)
at Compilation.applyPluginsAsyncSeries (C:\Users\acer\source\repos\testfromcmd\node_modules\tapable\lib\Tapable.js:131:46)
at sealPart2 (C:\Users\acer\source\repos\testfromcmd\node_modules\webpack\lib\Compilation.js:631:9)
at Compilation.applyPluginsAsyncSeries (C:\Users\acer\source\repos\testfromcmd\node_modules\tapable\lib\Tapable.js:131:46)
at Compilation.seal (C:\Users\acer\source\repos\testfromcmd\node_modules\webpack\lib\Compilation.js:579:8)
at C:\Users\acer\source\repos\testfromcmd\node_modules\webpack\lib\Compiler.js:493:16
at C:\Users\acer\source\repos\testfromcmd\node_modules\tapable\lib\Tapable.js:225:11
at _addModuleChain (C:\Users\acer\source\repos\testfromcmd\node_modules\webpack\lib\Compilation.js:481:11)
at processModuleDependencies.err (C:\Users\acer\source\repos\testfromcmd\node_modules\webpack\lib\Compilation.js:452:13)
at process._tickCallback (internal/process/next_tick.js:150:11)
Child
Hash: ffb9aab17185511f568b
Time: 26026ms
Asset Size Chunks Chunk Names
main-server.js 2.04 MB 0 [emitted] [big] main-server
I solved my problem by changing the uglify version from 3.0.23 to 2.8.23 and this works for me thanks for help

How to use Regex in batch file

I am using batch file in tfs build step.
I wanted to parse a text file from batch script.
File content is something like this
-------------------------------------------------------------------------------
Changeset: 267406
User: myname
Date: Wednesday, August 12, 2015 5:33:28 PM
Comment:
TFS Changes
Items:
edit $/dummyPath/uDClient/PushVersion.bat
add $/dummyPath/uDClient/request.json
-------------------------------------------------------------------------------
Changeset: 267356
User: username
Date: Wednesday, August 12, 2015 12:16:58 PM
Comment:
Initial check-in
Items:
edit $/dummyPath/UserControls/Welcome.ascx
edit $/dummyPath/UserControls/Welcome.ascx.cs
edit $/dummyPath/UserControls/Welcome.ascx.designer.cs
deleted $/dummyPath/UserControls/p1.ascx
Required out put will be
$/dummyPath/uDClient/PushVersion.bat
$/dummyPath/uDClient/request.json
$/dummyPath/UserControls/Welcome.ascx
$/dummyPath/UserControls/Welcome.ascx.cs
$/dummyPath/UserControls/Welcome.ascx.designer.cs
$/dummyPath/UserControls/p1.ascx
I wanted to do this parsing in batch script, if it is not possible then i'll go for C# Regex through utility application.
So can anybody suggest me how can i do this in batch script file
I am using below command to get required output
for /f "tokens=2" %i in ('FINDSTR /C:^edit p.txt') do echo %i
but i have to run same commond three times (for add,delete also)
in c# regex we can specify list words to match
*(add|delete|edit)\s*
How i can use alternate matching words in findstr.
This code with your text in file.txt will print the output below it:
#echo off
for /f "tokens=1,* delims=$" %%a in ('find "/" ^<"file.txt"') do echo $%%b
output:
$/dummyPath/uDClient/PushVersion.bat
$/dummyPath/uDClient/request.json
$/dummyPath/UserControls/Welcome.ascx
$/dummyPath/UserControls/Welcome.ascx.cs
$/dummyPath/UserControls/Welcome.ascx.designer.cs
$/dummyPath/UserControls/p1.ascx
You can try to use a Power Shell script to do it.
Here is a powershell script which produces the desired output on your specified input:
select-string -Path 'C:\source\source.txt' -Pattern '\$.*' -AllMatches | % { $_.Matches } | % { $_.Value }

How to recognize dll's target platform [duplicate]

I've got an arbitrary list of .NET assemblies.
I need to programmatically check if each DLL was built for x86 (as opposed to x64 or Any CPU). Is this possible?
Look at System.Reflection.AssemblyName.GetAssemblyName(string assemblyFile).
You can examine assembly metadata from the returned AssemblyName instance:
Using PowerShell:
[36] C:\> [reflection.assemblyname]::GetAssemblyName("${pwd}\Microsoft.GLEE.dll") | fl
Name : Microsoft.GLEE
Version : 1.0.0.0
CultureInfo :
CodeBase : file:///C:/projects/powershell/BuildAnalyzer/...
EscapedCodeBase : file:///C:/projects/powershell/BuildAnalyzer/...
ProcessorArchitecture : MSIL
Flags : PublicKey
HashAlgorithm : SHA1
VersionCompatibility : SameMachine
KeyPair :
FullName : Microsoft.GLEE, Version=1.0.0.0, Culture=neut...
Here, ProcessorArchitecture identifies the target platform.
Amd64: A 64-bit processor based on the x64 architecture.
Arm: An ARM processor.
IA64: A 64-bit Intel Itanium processor only.
MSIL: Neutral with respect to processor and bits-per-word.
X86: A 32-bit Intel processor, either native or in the Windows on Windows environment on a 64-bit platform (WoW64).
None: An unknown or unspecified combination of processor and bits-per-word.
I'm using PowerShell in this example to call the method.
You can use the CorFlags CLI tool (for instance, C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\CorFlags.exe) to determine the status of an assembly, based on its output and opening an assembly as a binary asset you should be able to determine where you need to seek to determine if the 32BIT flag is set to 1 (x86) or 0 (Any CPU or x64, depending on PE):
Option | PE | 32BIT
----------|-------|---------
x86 | PE32 | 1
Any CPU | PE32 | 0
x64 | PE32+ | 0
The blog post x64 Development with .NET has some information about corflags.
Even better, you can use Module.GetPEKind to determine whether an assembly is PortableExecutableKinds value PE32Plus (64-bit), Required32Bit (32-bit and WoW), or ILOnly (any CPU) along with other attributes.
Just for clarification, CorFlags.exe is part of the .NET Framework SDK. I have the development tools on my machine, and the simplest way for me determine whether a DLL is 32-bit only is to:
Open the Visual Studio Command Prompt (In Windows: menu Start/Programs/Microsoft Visual Studio/Visual Studio Tools/Visual Studio 2008 Command Prompt)
CD to the directory containing the DLL in question
Run corflags like this:
corflags MyAssembly.dll
You will get output something like this:
Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 3.5.21022.8
Copyright (c) Microsoft Corporation. All rights reserved.
Version : v2.0.50727
CLR Header: 2.5
PE : PE32
CorFlags : 3
ILONLY : 1
32BIT : 1
Signed : 0
As per comments the flags above are to be read as following:
Any CPU: PE = PE32 and 32BIT = 0
x86: PE = PE32 and 32BIT = 1
64-bit: PE = PE32+ and 32BIT = 0
Just write your own. The core of the PE architecture hasn't been seriously changed since its implementation in Windows 95.
Here's a C# example:
public static ushort GetPEArchitecture(string pFilePath)
{
ushort architecture = 0;
try
{
using (System.IO.FileStream fStream = new System.IO.FileStream(pFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
using (System.IO.BinaryReader bReader = new System.IO.BinaryReader(fStream))
{
// Check the MZ signature
if (bReader.ReadUInt16() == 23117)
{
// Seek to e_lfanew.
fStream.Seek(0x3A, System.IO.SeekOrigin.Current);
// Seek to the start of the NT header.
fStream.Seek(bReader.ReadUInt32(), System.IO.SeekOrigin.Begin);
if (bReader.ReadUInt32() == 17744) // Check the PE\0\0 signature.
{
// Seek past the file header,
fStream.Seek(20, System.IO.SeekOrigin.Current);
// Read the magic number of the optional header.
architecture = bReader.ReadUInt16();
}
}
}
}
}
catch (Exception) { /* TODO: Any exception handling you want
to do, personally I just take 0
as a sign of failure */
}
// If architecture returns 0, there has been an error.
return architecture;
}
}
Now the current constants are:
0x10B - PE32 format.
0x20B - PE32+ format.
But with this method it allows for the possibilities of new constants. Just validate the return as you see fit.
DotPeek from JetBrains provides a quick and easy way to see msil (Any CPU), x86, and x64:
Try to use CorFlagsReader from this project at CodePlex. It has no references to other assemblies and it can be used as is.
[TestMethod]
public void EnsureKWLLibrariesAreAll64Bit()
{
var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies().Where(x => x.FullName.StartsWith("YourCommonProjectName")).ToArray();
foreach (var assembly in assemblies)
{
var myAssemblyName = AssemblyName.GetAssemblyName(assembly.FullName.Split(',')[0] + ".dll");
Assert.AreEqual(ProcessorArchitecture.MSIL, myAssemblyName.ProcessorArchitecture);
}
}
Below is a batch file that will run corflags.exe against all DLL files and EXE files in the current working directory and all sub-directories, parse the results and display the target architecture of each.
Depending on the version of corflags.exe that is used, the line items in the output will either include 32BIT, or 32BITREQ (and 32BITPREF). Whichever of these two is included in the output is the critical line item that must be checked to differentiate between Any CPU and x86. If you are using an older version of corflags.exe (pre Windows SDK v8.0A), then only the 32BIT line item will be present in the output, as others have indicated in past answers. Otherwise 32BITREQ and 32BITPREF replace it.
This assumes corflags.exe is in the %PATH%. The simplest way to ensure this is to use a Developer Command Prompt. Alternatively you could copy it from its default location.
If the batch file below is run against an unmanaged DLL or EXE file, it will incorrectly display it as x86, since the actual output from Corflags.exe will be an error message similar to:
corflags : error CF008 : The specified file does not have a valid managed header
#echo off
echo.
echo Target architecture for all exes and dlls:
echo.
REM For each exe and dll in this directory and all subdirectories...
for %%a in (.exe, .dll) do forfiles /s /m *%%a /c "cmd /c echo #relpath" > testfiles.txt
for /f %%b in (testfiles.txt) do (
REM Dump corflags results to a text file
corflags /nologo %%b > corflagsdeets.txt
REM Parse the corflags results to look for key markers
findstr /C:"PE32+">nul .\corflagsdeets.txt && (
REM `PE32+` indicates x64
echo %%~b = x64
) || (
REM pre-v8 Windows SDK listed only "32BIT" line item,
REM newer versions list "32BITREQ" and "32BITPREF" line items
findstr /C:"32BITREQ : 0">nul /C:"32BIT : 0" .\corflagsdeets.txt && (
REM `PE32` and NOT 32bit required indicates Any CPU
echo %%~b = Any CPU
) || (
REM `PE32` and 32bit required indicates x86
echo %%~b = x86
)
)
del corflagsdeets.txt
)
del testfiles.txt
echo.
I've cloned a super handy tool that adds a context menu entry for assemblies in Windows Explorer to show all available information:
Download from Releases · tebjan/AssemblyInformation.
One more way would be to use dumpbin from the Visual Studio tools on the DLL and look for the appropriate output:
dumpbin.exe /HEADERS <your DLL file path>
FILE HEADER VALUE
14C machine (x86)
4 number of sections
5885AC36 time date stamp Mon Jan 23 12:39:42 2017
0 file pointer to symbol table
0 number of symbols
E0 size of optional header
2102 characteristics
Executable
32 bit word machine
DLL
Note: The above output is for a 32-bit DLL file
One more useful option with dumpbin.exe is /EXPORTS. It will show you the function exposed by the DLL file
dumpbin.exe /EXPORTS <PATH OF THE DLL FILE>
A more generic way - use the file structure to determine bitness and image type:
public static CompilationMode GetCompilationMode(this FileInfo info)
{
if (!info.Exists)
throw new ArgumentException($"{info.FullName} does not exist");
var intPtr = IntPtr.Zero;
try
{
uint unmanagedBufferSize = 4096;
intPtr = Marshal.AllocHGlobal((int)unmanagedBufferSize);
using (var stream = File.Open(info.FullName, FileMode.Open, FileAccess.Read))
{
var bytes = new byte[unmanagedBufferSize];
stream.Read(bytes, 0, bytes.Length);
Marshal.Copy(bytes, 0, intPtr, bytes.Length);
}
// Check DOS header magic number
if (Marshal.ReadInt16(intPtr) != 0x5a4d)
return CompilationMode.Invalid;
// This will get the address for the WinNT header
var ntHeaderAddressOffset = Marshal.ReadInt32(intPtr + 60);
// Check WinNT header signature
var signature = Marshal.ReadInt32(intPtr + ntHeaderAddressOffset);
if (signature != 0x4550)
return CompilationMode.Invalid;
// Determine file bitness by reading magic from IMAGE_OPTIONAL_HEADER
var magic = Marshal.ReadInt16(intPtr + ntHeaderAddressOffset + 24);
var result = CompilationMode.Invalid;
uint clrHeaderSize;
if (magic == 0x10b)
{
clrHeaderSize = (uint)Marshal.ReadInt32(intPtr + ntHeaderAddressOffset + 24 + 208 + 4);
result |= CompilationMode.Bit32;
}
else if (magic == 0x20b)
{
clrHeaderSize = (uint)Marshal.ReadInt32(intPtr + ntHeaderAddressOffset + 24 + 224 + 4);
result |= CompilationMode.Bit64;
}
else return CompilationMode.Invalid;
result |= clrHeaderSize != 0
? CompilationMode.CLR
: CompilationMode.Native;
return result;
}
finally
{
if (intPtr != IntPtr.Zero)
Marshal.FreeHGlobal(intPtr);
}
}
Compilation mode enumeration
[Flags]
public enum CompilationMode
{
Invalid = 0,
Native = 0x1,
CLR = Native << 1,
Bit32 = CLR << 1,
Bit64 = Bit32 << 1
}
Source code with explanation is at GitHub.
Another way to check the target platform of a .NET assembly is inspecting the assembly with .NET Reflector...
##~#€~! I've just realized that the new version is not free! So, correction, if you have a free version of .NET reflector, you can use it to check the target platform.
cfeduke notes the possibility of calling GetPEKind. It's potentially interesting to do this from PowerShell.
Here, for example, is code for a cmdlet that could be used: https://stackoverflow.com/a/16181743/64257
Alternatively, at https://stackoverflow.com/a/4719567/64257 it is noted that "there's also the Get-PEHeader cmdlet in the PowerShell Community Extensions that can be used to test for executable images."
A tool is sigcheck:
sigcheck c:\Windows\winhlp32.exe
Output:
Sigcheck v2.71 - File version and signature viewer
Copyright (C) 2004-2018 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\winhlp32.exe:
Verified: Signed
Signing date: 20:05 02.05.2022
Publisher: Microsoft Windows
Company: Microsoft Corporation
Description: Windows Winhlp32 Stub
Product: Microsoft® Windows® Operating System
Prod version: 10.0.19041.1
File version: 10.0.19041.1 (WinBuild.160101.0800)
MachineType: 32-bit
sigcheck -nobanner c:\Windows\HelpPane.exe
Output:
c:\windows\HelpPane.exe:
Verified: Signed
Signing date: 00:42 23.04.2022
Publisher: Microsoft Windows
Company: Microsoft Corporation
Description: Microsoft Help and Support
Product: Microsoft® Windows® Operating System
Prod version: 10.0.19041.1151
File version: 10.0.19041.1151 (WinBuild.160101.0800)
MachineType: 64-bit
An alternative to already mentioned tools is Telerik JustDecompile (free tool) which will display the information next to the assembly name:
I like the ILSpy tool. It shows not only architecture, but the target framework as well:
// linq2db, Version=3.0.0.0, Culture=neutral, PublicKeyToken=e41013125f9e410a
// Global type: <Module>
// Architecture: AnyCPU (64-bit preferred)
// Runtime: v4.0.30319
// This assembly is signed with a strong name key.
// This assembly was compiled using the /deterministic option.
// Hash algorithm: SHA1
So it is possible to determine if it is .NET Core 2.1, .NET Framework 4.6 or any other one:

Categories

Resources