UWP app typeof returns null in Release build - reflection

After upgrading an Windows Store App from win 8.1 to 10, we are having trouble using simple reflection in our app. In my application i have a type MyMobile.ViewModels.ShellViewModel, but calling typeof(ShellViewModel) returns null on application start.
I suspect this has something to do with following:
The "Compile with .NET Native tool chain" is enabled i project properties, and we have the following content Runtime Directive (Default.rd.xml).
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<!--
An Assembly element with Name="*Application*" applies to all assemblies in
the application package. The asterisks are not wildcards.
-->
<Assembly Name="*Application*" Dynamic="Required All" />
</Application>
</Directives>
Which is the default runtime directive file added when creating the UWP project. As per my understanding this should be enough to run "typeof" on types in your own assembly?

Related

Reading from app.config in .NET Core application referencing .NET Standard library

I'm converting over some existing code from .NET Framework to .NET Standard for use in .NET Core apps. The library project has been converted over and is building fine. It contains some database access code that uses EF Core. Due to the legacy nature of the consumers for this code I wanted to continue using the App.Config or Web.Config files with Configuration Manager. To do this I added the System.Configuration.ConfigurationManager NuGet package to the library project.
The library is validated with a unit test project that uses MSTest. The project was targeting .NET Framework 4.7.1, which could consume the .NET Standard 2.0 library package. When targeting .NET Framework, all of the unit tests were passing.
After changing the unit test project to target .NET Core 2.0, the database code is no longer able to find the connection strings stored in the App.Config file of the unit test project. When I debug the test and inspect the ConfigurationManager.ConnectionStrings collection, I only see one defined that appears to be a SQL Express connection likely coming from the Machine.Config file.
Has anyone had success accessing App.Config from a .NET Core app passing through a .NET Standard library with the ConfigurationManager compatibility library?
Still there is a known issue in Microsoft.Net.Test.Sdk and it is because when you use ConfigurationManager in test applications using .Net Core, ConfigurationManager is looking for testhost.dll.config and not your standard assembly config file.
There is a non-pleasant workaround for this issue based on this discussion in github which you can copy your App.Config file into your output directory with name testhost.dll.config by putting this element in your test csproj file:
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
</Target>
And then you can use ConfigurationManager.OpenMappedExeConfiguration to load your config file.
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "testhost.dll.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
And for example for reading an appsetting you can do like this:
var setting = Config.AppSettings.Settings[key];

Xamarin forms portable library with obfuscation tool .Net Reactor

I am working on Xamarin forms project (with portable project)
I am trying to obfuscate the portable dll using .Net Reactor I have done that using a batch file which is set as a post build event for the portable project and everything works fine (i.e the generated dll is obfuscated successfully )
here is the script code
"C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.exe" -project
"reactor.nrproj" -file "myProject.dll"
copy "myProject_Secure\myProject.dll" "myProject.dll"
the problem is when I build the droid project I got the following error
Exception while loading assemblies: System.IO.FileNotFoundException:
Could not load assembly 'Windows, Version=255.255.255.255,
Culture=neutral, PublicKeyToken='. Perhaps it doesn't exist in the
Mono for Android profile? File name: 'Windows.dll' at
Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference
reference, ReaderParameters parameters) in
/Users/builder/data/lanes/5147/c2a33d8e/source/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:line
220 at
Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver
resolver, ICollection`1 assemblies, AssemblyDefinition assembly,
Boolean topLevel) at
Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver
resolver) myProject.Droid
I am sure that problem comes from the obfuscation because everything works fine without the obfuscation
Does anybody work with .Net reactor to obfuscate Xamarin forms projects?
BTW, I have un-check all the quick settings of the .Net reactor and the problem is still shows up
Obfuscating of Xamarin Android is useless its better to turn on "Embed assemblies to native code" which will convert all code into a binary .so file. so it will be difficult for hacker or reverse engineer to read that code and do reverse engineering.you can use AOT but that is in experimental and might create problem for some device after updated on play store. in order to play safe i would suggest to go with "Embed assemblies to native code"
You need to set "2. Protection Settings"->"Anti ILDASM"->"Suppress Decompilation" to "False" as it confuses the build process in this case. In case it still doesn't work set "1. General Settings"->"Project Targets Mono Framework" to "True".

System.TypeLoadException: Inheritance security rules violated by type: 'System.Net.Http.Formatting.JsonContractResolver'

I'm getting a very annoying error about System.Net.Http.Formatting.JsonContractResolver having inheritance security rules violated in a project which uses PCLs.
Here is the setup and how I tracked down the problem.
Project 1 - Custom.WebApi (portable, targeting .net 4.5, Core 5.0, Win8, Xamarin.Android, Xamarin.iOS, Xamarin.iOS(Classic))
This project is an abstraction over HttpClient to unify the return types of Get, Post, Delete, ... and depends ONLY on Newtonsoft.Json and .NET with this profile.
We are using Newtsonsoft.Json pcl version 6.0.8.($packagesFolder\Newtonsoft.Json.6.0.8\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll)
Project 2 - Custom.WepApi.Tests (unit test project)
This is a test project for the WebApi project. It references full version of 6.0.8 ($packagesFolder\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll)
This project is able to run tests against WebApi project no problem.
Project 3 - MyWebsite (ASP.NET WebApi site)
This project is a WebApi project where the response types are unified and found in the Custom.WebApi project. This references a bunch of 3rd party stuff, but also Newtonsoft.Json v 6.0.8 full ($packagesFolder\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll)
Project 4 - MyWebsite.Tests
To test the web project, I have a test project that calls endpoints using Custom.WebApi implementation. This project also references newtonsoft.json version 6.0.8. However, this references is purely to fix errors observed when running. Nothing in this project depends on Newtonsoft.Json, but if I do not explicitly add a reference to it, it will fail with TypeLoadException. Adding the reference seems to make that go away.
When I run the Project 4 tests, they all fail, indicating that the website is having TypeLoadException on Startup. After a TON of investigation, I noticed that when the MyWebsite project builds, the Newtonsoft.Json.dll in the bin folder is overwritten with the portable version and fails.
If I change the test project to reference the same portable newtonsoft.json.dll, the test project fails with the same error. Using the full version, no error is observed.
If I manually replace the dll in the bin folder of MyWebsite, it is overwritten again upon build. I have made sure the references are correct in MyWebsite .csproj file, including the hint path.
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
So, either I need to resolve why it believes there is an inheritance security rule violation (looking at the source code did not reveal anything obvious about access modifiers not matching), or it needs to stop overwriting the Newtonsoft.Json.dll in the MyWebsite bin folder.
To further complicate things, this replacement only occurs SOMETIMES. If I Start Without Debugging, it seems to not happen all the time and executing the Unit Tests succeeds against the deployment when the dll is not replaced with portable version.
You can resolve this by setting "Copy Local" to false on the Newtonsoft.Json reference in your PCL projects. I'll show you tomorrow.

Adding project reference in visual studio

I added a project reference of my Business Logic project to my console application. I can reference the project and its classes in the code of my console application. Weirdly, when I build the project, the build fails and the classes which referenced says Type is not defined.Has anybody come across an issue like this?
You have to import name space in your console application class where you are trying to use the business logic application class.
I have selected the incorrect target framework .net framework client profile 4.0 where it should be .net framework 4. Issue rectified and now I can build the project.

VS2012 / IIS7 building website causing conflicts

I have an old website (asp.net 2.0) that is running on IIS7 on a server happily.
I want to make a change to the code, so I have copied all the source code and appropriate binaries to my desktop (running VS 2012).
However whether I am using IIS or the inbuilt webserver I am getting the following error;
The type 'foo' in 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\livesite\169c38ce\cc32b84b\App_Web_qbel5zk4.28.cs' conflicts with the imported type 'foo' in 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\livesite\169c38ce\cc32b84b\App_Web_m030ztdd.dll'. Using the one in 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\livesite\169c38ce\cc32b84b\App_Web_qbel5zk4.28.cs'.
This same code worked fine on an older XP PC running VS 2005. I am assuming that this is down to the .net framework, the app pool in iis is set to run as asp.net 2 classic, but the assemblies are being generated under v4 of the framework.
I have tried deleting temporary asp.net files and I am still getting the same issue.
There is only one instance of each of the classes it is objecting about in the project. So how do I fix it so that this works in VS 2012, but still using .net 2.0 ?
Maybe it is a matter of dynamic compilation. By default the "batch" attribute of the "compilation" element in your web.config is true. Something like this :
<compilation defaultLanguage="c#" debug="false" batch="true" targetFramework="4.0" />
With batch="true", whenever a control has to be compiled in a directory, the server compiles all controls (everything?) in its directory.
That may results in conflicts for elements referenced from different locations in your site.
You should try to set batch="false" and to remove your temporary files.
If you still encounter problems, try to do a dummy edit-and-save on the corresponding ascx/cs files.
Hope this will help

Resources