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 webassembly] library not working with linker disabled #358

Closed
julienGrd opened this issue Mar 17, 2020 · 20 comments
Closed

[Blazor webassembly] library not working with linker disabled #358

julienGrd opened this issue Mar 17, 2020 · 20 comments
Labels

Comments

@julienGrd
Copy link
Contributor

Hello and thanks for your work ! I use your library in one of my blazor webassembly project, but it seem not working when the linkler is disabled

You will find a reproduction project here : https://github.com/julienGrd/blazorWebassembly3.2Preview1

  • run the app
  • go to fetch data
  • open the chrome console, you will see this exception
System.TypeInitializationException: The type initializer for 'System.Linq.Dynamic.Core.DynamicQueryableExtensions' threw an exception. ---> System.Exception: Specific method not found: All ---> System.InvalidOperationException: Sequence contains no matching element

It seem this issue is related to this one on mono, if it can help : mono/mono#12917

if you disable the linker(in the webassembly csproj) there is no longer problem, but it's not a good thing and make the app bigger

 <PropertyGroup>
    <BlazorWebAssemblyEnableLinking>false</BlazorWebAssemblyEnableLinking>
  </PropertyGroup>

thanks for your help !

@StefH
Copy link
Collaborator

StefH commented Mar 17, 2020

Is this something which can be fixed by this library, or just as notification?

@julienGrd
Copy link
Contributor Author

I'm not really sure... at the end of the issue (this one mono/mono#12917) they speak about a PreserveDependencyAttribute which can be add to solve the problem but we have a lack of information. So if you understand what they speak about in the issue maybe you can make something but otherwise i will wait for more information.

@StefH
Copy link
Collaborator

StefH commented Mar 18, 2020

If that property solves the issue, that means that property should be defined in the calling application, cannot be defined in this project.

I think...

@julienGrd
Copy link
Contributor Author

apparently the guys of mono back to us in the month to explain, we will see at this moment.
Im not the only who use this library in blazor webassembly app so anyway it can be nice to put the solution here, even if it impact only the calling app and not the library itselve

@danroth27
Copy link

Hi @julienGrd. You should be able to configure the linker for your library by embedding an XML linker config file into the library. I've put together a sample that shows how to do this here: https://github.com/danroth27/ComponentLibWithXmlLinkerConfig. Could you please try this out and see if it works for your scenario?

@julienGrd
Copy link
Contributor Author

Hi @danroth27 , thanks it seem work perfectly ! just an important thing to make it works, i have to add the file at the project but also edit the csproj to add this directive (like your project)

<ItemGroup>
        <EmbeddedResource Include="LinkerConfig.xml">
            <LogicalName>$(MSBuildProjectName).xml</LogicalName>
        </EmbeddedResource>
    </ItemGroup>

Is this file not impact other .NET standard type of project? because its not a blazor specific library !

@StefH : OK to add this to your project ? otherwise it seem i can make it directly on my client project

@danroth27
Copy link

danroth27 commented Mar 19, 2020

Is this file not impact other .NET standard type of project? because its not a blazor specific library !

All this is doing is embedded the XML linker config file into the assembly. This file will get picked up appropriately by the .NET IL linker wherever it gets used, including Xamarin apps and standalone .NET Core desktop apps. For apps that aren't using the linker this file is completely benign.

In short, this file is not Blazor specific and is safe to add to any .NET Standard library.

@julienGrd
Copy link
Contributor Author

@danroth27 finally It seem not enough with this library. I fall into another problems when i try to use dynamic group by.
To reproduce :

  • open your ComponentLibWithXmlLinkerConfigproject
  • put this piece of code on the Initialized of your component
    var test = people.AsQueryable().GroupBy("Name").ToDynamicList<IGrouping<dynamic, Person>>();

you will have this error :

blazor.webassembly.js:1 WASM: System.TypeInitializationException: The type initializer for 'System.Linq.Dynamic.Core.DynamicEnumerableExtensions' threw an exception. ---> System.InvalidOperationException: Sequence contains no matching element
blazor.webassembly.js:1 WASM:   at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) <0x21b7380 + 0x00022> in <filename unknown>:0 
at System.Linq.Dynamic.Core.DynamicEnumerableExtensions..cctor () <0x21b6f98 + 0x0002a> in <filename unknown>:0 

What is strange, is im unable to reproduce this error when i reference the csproj of this project, its only when i reference the nuget package.

@StefH : some idea on this ?

@danroth27
Copy link

@julienGrd Makes sense - the type initializer for DynamicEnumerableExtensions uses reflection: https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/src/System.Linq.Dynamic.Core/DynamicEnumerableExtensions.cs#L16-L20. You could address this by updating the xml linker config to preserve the DynamicEnumerableExtensions type.

@julienGrd
Copy link
Contributor Author

Yes i was thinking was something like that, i still don't really understand how to use the linker :-/. i try both these configuration without make it work

  <assembly fullname="System.Reflection">
    <type fullname="System.Linq.Dynamic.Core.DynamicEnumerableExtensions" preserve="all" />
  </assembly>
<assembly fullname="System.Runtime">
    <type fullname="System.Linq.Dynamic.Core.DynamicEnumerableExtensions" preserve="all" />
  </assembly>

@danroth27
Copy link

@julienGrd DynamicEnumerableExtensions lives in the System.Linq.Dynamics.Core assembly, not System.Runtime.

@julienGrd
Copy link
Contributor Author

ok it works perfect ! i will be happy to have some documentations about the linker when it will be possible !
Thanks for your help i will close this issue.

@StefH if you still want include by default this configuration to make it works in mono projects without client configuration, i can suggest a PR. just reopen this issue and say me.

@danroth27
Copy link

@julienGrd Docs on how to configure the linker for libraries are in the works: dotnet/AspNetCore.Docs#17369

@julienGrd
Copy link
Contributor Author

the final solution to make it works, on the library or on the client project
Linker.xml

<?xml version="1.0" encoding="utf-8" ?>
<!-- IL linker format: https://github.com/mono/linker#link-xml-file-examples -->
<linker>
  <assembly fullname="System.Core">
    <type fullname="System.Linq.Queryable" preserve="all" />
  </assembly>
  <assembly fullname="System.Linq.Dynamic.Core">
    <type fullname="System.Linq.Dynamic.Core.DynamicEnumerableExtensions" preserve="all" />
  </assembly>
</linker>

csproj

<ItemGroup>
    <None Remove="LinkerConfig.xml" />
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="LinkerConfig.xml">
      <LogicalName>$(MSBuildProjectName).xml</LogicalName>
    </EmbeddedResource>
  </ItemGroup>

@StefH
Copy link
Collaborator

StefH commented Mar 23, 2020

Please make a PR.

@julienGrd
Copy link
Contributor Author

Please make a PR.

perfect ! i will do this these next days !

@julienGrd julienGrd reopened this Mar 23, 2020
@StefH
Copy link
Collaborator

StefH commented Mar 23, 2020

And when this is done; I need to update the Wiki.

Also: can you include a full working example blazor project? Place this in a new src-blazor folder.

@julienGrd
Copy link
Contributor Author

yes it's a good idea !Do you want some specific test in the blazor project ? or a simple dynamic order by on a table is OK for you ?

@StefH
Copy link
Collaborator

StefH commented Mar 23, 2020

Keep it simple.

@StefH
Copy link
Collaborator

StefH commented Mar 25, 2020

closing issue because PR is merged to master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants