Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blazor] IL Linker not working correctly with IQueryable extensions #12917

Closed
danroth27 opened this issue Feb 9, 2019 · 14 comments

Comments

@danroth27
Copy link

commented Feb 9, 2019

From @ivanchev on Friday, 08 February 2019 09:55:31

Describe the bug

If you create a class library with some IQueryable extensions, for instance:

        public static int CustomCount(this IQueryable source)
        {
            if (source == null) throw new ArgumentNullException("source");
            return source.Provider.Execute<int>(
                Expression.Call(
                    typeof(Queryable), "Count",
                    new Type[] { source.ElementType }, source.Expression));
        }

Using that in a WebAssembly Blazor project produces error.

components.webassembly.js:1 WASM: [System.InvalidOperationException] No generic method 'Count' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

To Reproduce

Steps to reproduce the behavior:

  1. Add the extension method to a new .netstandard 2.0 class library
  2. Create a new simple Blazor project, and reference the class library project
  3. In the FetchData page, call the following:
    protected override async Task OnInitAsync()
    {
        forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");

        var count = forecasts.AsQueryable().CustomCount();
    }
  1. See error

Expected behavior

No error thrown.

If you disable the IL Linker the issue is not observed:

<BlazorLinkOnBuild>false</BlazorLinkOnBuild>

Tested with both 0.7 and 0.8 versions.

Copied from original issue: aspnet/AspNetCore#7383

@danroth27

This comment has been minimized.

Copy link
Author

commented Feb 9, 2019

From @danroth27 on Saturday, 09 February 2019 00:16:42

@ivanchev Thanks for reporting this issue!

Since this appears to be an IL linker issue I'm going to move this over to the Mono folks.

@lewing

This comment has been minimized.

Copy link
Member

commented Feb 12, 2019

I'm having trouble reproducing this. I've attached the test case I'm using
QueryableTest.zip

@lewing lewing self-assigned this Feb 12, 2019

@lewing

This comment has been minimized.

Copy link
Member

commented Feb 12, 2019

Reproduced it now, looking into it

@lewing lewing removed the needs more info label Feb 12, 2019

@lewing

This comment has been minimized.

Copy link
Member

commented Feb 12, 2019

@marek-safar I can reproduce with the mono/linker master 6b87e44f

adding <type fullname="System.Linq.Queryable*" /> to Linker.xml in the System.Core sections should work around the issue while we investigate.

@lewing

This comment has been minimized.

Copy link
Member

commented Feb 13, 2019

Simple test case for the linker

using System;
using System.Linq.Expressions;
using System.Linq;

namespace Mono.Linker.Tests.Cases.Expressions {
        public class QueryableUsedViaExpression {
                public static void Main ()
                {
                        var q = "Test".AsQueryable ();
                        var count = CallQueryableCount (q);
                        // q.Count ();
                        Console.WriteLine ($"count: {count}");
                }

                public static int CallQueryableCount (IQueryable source)
                {
                        return source.Provider.Execute<int> (
                                Expression.Call (
                                        typeof (Queryable), "Count",
                                        new Type [] { source.ElementType }, source.Expression));
                }
        }
}
@EdCharbeneau

This comment has been minimized.

Copy link

commented May 3, 2019

@marek-safar I can reproduce with the mono/linker master 6b87e44f

adding <type fullname="System.Linq.Queryable*" /> to Linker.xml in the System.Core sections should work around the issue while we investigate.

Would this "work around" work for us on our UI component library? We're still waiting on a fix.

Having our customers disable Linker makes Blazor quite bloated.

@lewing

This comment has been minimized.

Copy link
Member

commented May 4, 2019

@EdCharbeneau yes
see https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/blazor/configure-linker?view=aspnetcore-3.0 for instructions the relevant section to add to a linker.xml

<assembly fullname="System.Core">
     <type fullname="System.Linq.Queryable*" />
</assembly>

Or if you are distributing an assembly I believe you can embed that descriptor in the assembly by embedding a linker resource in your assemble 'My.Assembly` by adding something like

<ItemGroup>
    <EmbeddedResource Include="WebAssembly.xml">
      <LogicalName>My.Assembly.xml</LogicalName>
    </EmbeddedResource>
</ItemGroup>

to your .csprog

@tsmccartan

This comment has been minimized.

Copy link

commented May 7, 2019

I am wondering if the issue I am having is related to this.

I am using Blazor to query documents in Cosmos DB, and having issues with the return from the following method.

In My Razor Page

IDocumentQuery InstanceList = client.CreateDocumentQuery(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
.Where(o => o.Active == true).AsDocumentQuery();

Gives me this error:
WASM: System.TypeLoadException: Could not resolve type with token 0100003b from typeref (expected class 'System.Linq.IOrderedQueryable`1' in assembly 'Microsoft.AspNetCore.Blazor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60')

It seems that there is some issue with IOrderedQueryable. CreateDocumentQuery returns the type IOrderedQueryable, and I can't change that. Even converting to List, etc. still gives me this error above. Is there some workaround for this?

I have tried the above, but all added System.Linq.IOrderedQueryable as a Type in the Linker.XML, but that just caused a bunch of other errors....

@akorchev

This comment has been minimized.

Copy link

commented Jun 17, 2019

@lewing The suggested workaround with linker configuration doesn't seem to work when distributing an assembly. No matter the configuration file it just doesn't seem to get picked up by the linker. Are there any extra steps?

@marek-safar

This comment has been minimized.

Copy link
Member

commented Jun 18, 2019

We don't support this dynamic code in linker right now and the workaround with using reflection (methodinfo) won't work either as the method is generic with generic type argument which cannot be expressed using reflection APIs.

If this is quite common we could extend the linker to have detection support for simple Expression cases like

Expression.Call(typeof(Queryable) /* Declaring Type */, "Count" /* Method Name */, new Type[] /* arity */)
@danroth27

This comment has been minimized.

Copy link
Author

commented Jun 18, 2019

@marek-safar This usage is common for component vendors seeking to implement Grid components that support sorting, filtering, paging, etc. Right now, these component vendors have to tell their customers to disable the linker to work around this issue.

@lewing lewing referenced this issue Jun 21, 2019

Closed

[wasm] System.Linq.Expressions #15300

3 of 3 tasks complete
@marek-safar

This comment has been minimized.

Copy link
Member

commented Jun 21, 2019

@danroth27 ok, we will address that at the linker level

@marek-safar marek-safar self-assigned this Jun 21, 2019

@EdCharbeneau

This comment has been minimized.

Copy link

commented Jul 25, 2019

Any updates to this?

@marek-safar

This comment has been minimized.

Copy link
Member

commented Aug 7, 2019

Fixed in master with #16014

@marek-safar marek-safar closed this Aug 7, 2019

kjpou1 added a commit to kjpou1/mono that referenced this issue Aug 16, 2019

Add regression test for issue mono#12917
- IL Linker not working correctly with IQueryable extensions

lewing added a commit that referenced this issue Aug 17, 2019

Add regression test for issue #12917
- IL Linker not working correctly with IQueryable extensions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.