MissingRuntimeArtifactException while changing IEn

2019-06-26 07:36发布

问题:

In Release mode, while changing the IEnumerable source to IQueryable using AsQueryable method which throws System.Reflection.MissingRuntimeArtifactException. This is code is working fine in Debug mode, please refer the below code snippet.

    ObservableCollection<object> data;
    IEnumerable source;
    public MainPage()
    {
        this.InitializeComponent();
        data = new ObservableCollection<object>();
        source = data as IEnumerable;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var querab1 = data.AsQueryable();
        var querab2 = source.AsQueryable();
    }

Is there any solution for this exception?

回答1:

Add the following line to the <Application> node in your runtime directives file (usually called Default.rd.xml and found in the Properties folder).

<Namespace Name="System.Linq" Dynamic="Required All" Serialize="Required All" XmlSerializer="Required All"/>

Using the Release mode invokes the .NET Native tool chain. It includes in the final app assembly only that code that is actually invoked by the app. This causes certain reflection and late-bound invocation code to not be included in your app. Using the runtime directives file allows you to override the default behavior and include the required metadata and implementation code.

PS: Your runtime directives file should look something like this:

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="*Application*" Dynamic="Required All" />
    <Namespace Name="System.Linq" Dynamic="Required All" Serialize="Required All" XmlSerializer="Required All" />
  </Application>
</Directives>