MissingMetadataException when building UWP app with .Net native - linq

I have this UWP app that uses a project (UWP class library) which itself uses EF7 and SQLite.
I tried to build the app in the Release mode using .Net native tool chain, the build completes successfully (after a good long time, and after eating as much memory as it can), but the application crashes just after leaving the splash screen.
After following some advice on SO I tried the .Net native with Debug mode, the build finishes just like in the Release mode, but I get many errors on the output window and it is the same scenario as this one UWP - .NET Native tool chain compilation error
I followed #Matt Whilden advice, and I got rid of those errors, then tried again.
This time I got hit by this famous MissingMetadataException :
The output window shows this :
Exception thrown: 'System.AggregateException' in System.Private.Threading.dll
Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll
Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll
Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll
The thread 0x2a30 has exited with code 0 (0x0).
Exception thrown: 'System.Reflection.MissingMetadataException' in System.Private.Reflection.Core.dll
Additional information: 'Microsoft.Extensions.Caching.Memory.MemoryCacheOptions' is missing
metadata. For more information, please visit
http://go.microsoft.com/fwlink/?LinkID=392859
I tried to follow my code, during execution and I found out that it is caused by the first ever call to a DbSet table from my DbContext
public long GetLastTimeStamp()
{
//-----> Here is the line causing the error
var sortedArticles = DbContext.Articles.OrderByDescending(article => article.ArticlePubDate).ToList();
if (sortedArticles != null && sortedArticles.Count != 0)
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);
TimeSpan elapsedTime = sortedArticles.First().ArticlePubDate - epoch;
return (long)elapsedTime.TotalSeconds;
}
else
{
return 0;
}
}
This method above is called inside an Async method, just to know.
I tried, desperately, to call .ToList() by doing :
var sortedArticles = DbContext.Articles.ToList().OrderByDescending(article => article.ArticlePubDate).ToList();
But still get the same error.
This is really frustrating, I don't know how to solve this problem, not sure what and how I should change the Default.rd.xml, any one can help telling me how to achieve this build correctly ?

Please try to add type 'Microsoft.Extensions.Caching.Memory.MemoryCacheOptions' in Default.rd.xml (already present in your project).
cf. https://blogs.msdn.microsoft.com/dotnet/2014/05/21/net-native-deep-dive-help-i-hit-a-missingmetadataexception/
For example:
<?xml version="1.0" encoding="utf-8"?>
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<Type Name="Microsoft.Extensions.Caching.Memory.MemoryCacheOptions" Dynamic="Required All" />
</Application>
</Directives>

Related

COM method returning HRESULT value 0x80070057

I'm running the VS project(which is developed with VS 2015) accompanied with Introduction to 3D Game Programming with DirectX 12 in VS 2019.
But it threw an exception with a HRESULT value of -2147024809 like this:
mCommandList->Close() failed in
E:\programs\cpp_codes\d3d12\Common\d3dApp.cpp;line 213; HRESULT:-2147024809
-2147024809 Indicates that the arguments are not correct. But as it is seen there is not any bit of argument passed to ID3D12GraphicsCommandList::Close() method, which confused me badly.
This line of code threw no exception months ago.
Another COM method threw similar exception in d3dApp.cpp, which is
ThrowIfFailed(md3dDevice->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&depthStencilDesc,
D3D12_RESOURCE_STATE_COMMON,
&optClear,
IID_PPV_ARGS(mDepthStencilBuffer.GetAddressOf()))); failed in
E:\programs\cpp_codes\d3d12\Common\d3dApp.cpp;line 203; HRESULT:-2147024809
But as long as I changed the fifth argument from &optClear into nullptr, this exception stops from showing up. Also, this invocation of CreateCommittedResource threw no exception months ago.
I think it is because there is a mismatch in clear color variable, try to set same clear color in all the clearColor variable usage ...
Comments below my question suggested that debug layer will best help in such condition. What I actually did was clicking Debug->Graphics->Start Graphics Debugging in the menu bar of Visual Studio 2019. And then, detailed debug information showed up in the output window of Visual Studio 2019, it ran D3D12 ERROR: ID3D12Device::CreateCommittedResource: pOptimizedClearValue must be NULL when D3D12_RESOURCE_DESC::Dimension is not D3D12_RESOURCE_DIMENSION_BUFFER and neither D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET nor D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL are set in D3D12_RESOURCE_DESC::Flags. [ STATE_CREATION ERROR #815: CREATERESOURCE_INVALIDCLEARVALUE].
So this is the point, because I had set D3D12_RESOURCE_FLAG_NONE in D3D12_RESOURCE_DESC::Flags.
It's no wonder that this exception stopped from being thrown by CreateCommittedResource after I passed nullptr to the fifth parameter of CreateCommittedResource. After I had set D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, everything behaves well without any exception thrown.
And this is the code snippet ran into problem:
D3D12_RESOURCE_DESC depthStencilDesc;
depthStencilDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
//Assignments to other members of depthStencilDesc
//This line caused the problem.
depthStencilDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
D3D12_CLEAR_VALUE optClear;
//Assignments to the members of optClear
ThrowIfFailed(md3dDevice->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&depthStencilDesc,
D3D12_RESOURCE_STATE_COMMON,
&optClear,
IID_PPV_ARGS(mDepthStencilBuffer.GetAddressOf())));

Hitting exception when using Google Protobuf Any UnpackTo function in C++

google::protobuf::Any anyResponse = someResponse.response();
ResponseType unpackResp; //ResponseType is a subclass of google::protobuf::Message
if (anyResponse.UnpackTo(&unpackResp))
{
...
}
Running this piece of C++ code and access vialotion exception happens in anyResponse.UnpackTo(&unpackResp). Does someone know how to debug into this function? I checked anyResponse and it looks good.
It went through these files in google::protobuf:
call stack
Anyway I can see these files?

Getting detailed exception info for unhandled exceptions in Xamarin Android

If I get an unhandled exception in Xamarin Android, like always when debugging with Visual Studio, I get the following popup:
If I click Copy Details I get
Unhandled Exception: System.AggregateException: One or more errors occurred. occurred
How can I drill down into the inner exceptions?
Here's a snippet from one of our applications, we simply create a delegate against the UnhandledException event for the current domain, we do this within our applications 'MainApplication' class (Some people use 'MainActivity' instead if they have not implemented the lifecycle call back plugin).
Basically you want to do this in whichever class is you're applications primary starting point.
For reference 'ApplicationLog' is just a class I created to write to a file directory into a log file, you can do whatever you want inside this class, the key here is that 'e' will contain your stack trace information.
Regards you're exception it is worth noting that an AggregateException is most commonly thrown by code within a Task, or Parallel ForEach statements, or even SQL commands etc.
Hope this helps.
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
{
// Record the error in our application logs
ApplicationLog.AppendFile(
DateTime.Now.ToString() + " : " + "[CRITICAL GLOBALLY HANDLED EXCEPTION] - Critical exception has been hit! - Message: " + e.ExceptionObject +
System.Environment.NewLine + System.Environment.NewLine +
"========= Critcal Error has been hit, application closed =========" +
System.Environment.NewLine + System.Environment.NewLine
);
};

When running a Unity HoloLens program from Visual Studio, when an exception is thrown how do I get the line numbers in the stack trace?

Currently when an exception is thrown from within my Unity script while using my HoloLens the Debug Output in Visual Studio shows the stack trace without the line numbers.
How do I get the line numbers along with the stack trace? I'd be fine with it being logged somewhere else other than the Debug Output.
Here's some example output in Visual Studio:
Exception thrown: 'System.NullReferenceException' in Assembly-CSharp.dll
NullReferenceException: Object reference not set to an instance of an object.
at NewBehaviourScript.Update()
at NewBehaviourScript.$Invoke6Update(Int64 instance, Int64* args)
at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
(Filename: <Unknown> Line: 0)
And the corresponding Unity script (I made a Cube and attached a NewBehaviourScript component):
public class NewBehaviourScript : MonoBehaviour {
// Update is called once per frame
void Update ()
{
object a = null;
a.GetType();
}
}
I tried changing the build from Release to Debug doesn't give the line numbers.
I tried googling, and it looks like it's not showing the line numbers for others, as well: http://answers.unity3d.com/questions/1315985/null-reference-in-line-0.html
I tried asking on Microsoft's forums, but didn't receive any useful replies.
I don't think you would get the line number since it does not exist anymore. You get it in Unity editor because you are not running a full build of the application so Unity still has access to the non-compiled code. When you run on the device, it sends debug commands to the VS console about the printing and the errors but all the code is binary at that point, so there is no reason nor possibility to provide a line number.
Actually this is not specific to Hololens, but you would get the same in Android or iOS. Once build, the code is no longer the same, it does not even match one to one as the compiler performs optimizations.
What you can do is placed Debug commands to see where it happens.
public class NewBehaviourScript : MonoBehaviour {
// Update is called once per frame
void Update ()
{
object a = null;
#if DEBUG
if(a == null)
{
Debug.Log("[NewBehaviourScript] Running update with null a object");
}
#endif
a.GetType();
Debug.Log("[NewBeahviourScript] if this line prints, method did not crash");
}
}
In this example, you can use the DEBUG macros if you would have code running only for debug purpose. This way you can easily exclude it on export. The second call for Debug is not required in the macro since the build process will discard it when you set the build to Release or Master.

Raising obfuscated events with moq throws error

We have using Moq for two month now. However there is a problem which can not solve somehow.
In visual studio all tests succeeded just fine. On the build server there are several tests which failed. What they have in common is, that they use the "raise" method to throw an event. Our build server tests obfuscated what is good to find obfuscation errors. Every "normal" expectation like "Setup(something).Returns(something)" works. Only the raise event fails. the stacktrace looks like the following:
MESSAGE:
Test method Ade.Graphic.Presenter.Test.RoutingEngineTest.TestRouteOverLadderLinesWithFbd threw exception:
System.ArgumentException: Could not locate event for attach or detach method Void ᜀ(ᦜ[ᢈ]).
+++++++++++++++++++
STACK TRACE:
bei Moq.Extensions.GetEvent[TMock](Action`1 eventExpression, TMock mock)
bei Moq.Mock`1.Raise(Action`1 eventExpression, EventArgs args)
bei Ade.Graphic.Presenter.Test.RoutingEngineTest.TestRouteOverLadderLinesWithFbd()
The code for this is:
documentEventHandler.Raise(stub => stub.DocumentChanged += null,
new DocumentChangeEventArgs(DocumentChangeTypes.ViewUpdate));
We have no idea what is the difference between the code above and this
eventHandler.SetupGet(stub => stub.DocumentChangeNotify).Returns(documentEventHandler.Object);
because this code works fine.
Does anyone had the same problem or at least can tell what the difference is?
The error comes probably (not sure as not tested) from the fact that events (i.e. DocumentChanged) are actually generated as 2 accessors: add_DocumentChanged and remove_DocumentChanged . This is similar to the properties that have the get and set accessors.
What the obfuscator did most probably is rename this add_DocumentChanged and remove_DocumentChanged. However, looking at the moq source code, I can see that moq relies on the events accessor keeping the same name:
var ev = addRemove.DeclaringType.GetEvent(
addRemove.Name.Replace("add_", string.Empty).Replace("remove_", string.Empty));
ev == null in this case, which raises an error.
In your second examples, you're using delegates which are not broken down into add_ and remove_ accessors.
You're probably better off not obfuscating events.

Resources