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] TypeDescriptor is not working #7784

Closed
stsrki opened this issue Feb 21, 2019 · 5 comments
Closed

[Blazor] TypeDescriptor is not working #7784

stsrki opened this issue Feb 21, 2019 · 5 comments
Labels
area-blazor Includes: Blazor, Razor Components External This is an issue in a component not contained in this repository. It is open for tracking purposes.

Comments

@stsrki
Copy link
Contributor

stsrki commented Feb 21, 2019

Describe the bug

I'm trying to use TypeDescriptor for conversion between value types but it is not working in Blazor project. As soon as I try to use it it will break and raise an exception. In the example bellow the second Console.WriteLine is never called.

To Reproduce

class ValueConverter<TValue>
{
    public TValue Convert( object value )
    {
        try
        {
            Console.WriteLine( "before" );
            TypeConverter conv = TypeDescriptor.GetConverter( typeof( TValue ) );
            Console.WriteLine( "after" );

            return (TValue)conv.ConvertFrom( value );
        }
        catch
        {
            Console.WriteLine( $"error: {value}" );
            Console.WriteLine( exc.ToString() );
            return default;
        }
    }
}

Exception Stack Trace

WASM: System.MissingMethodException: Constructor on type 'System.ComponentModel.Int32Converter' not found.
components.webassembly.js:1 WASM: at System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) <0x1de9a28 + 0x00348> in :0
components.webassembly.js:1 WASM: at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) <0x1ddf940 + 0x000de> in :0
components.webassembly.js:1 WASM: at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture) <0x22e6cd0 + 0x0002a> in :0
components.webassembly.js:1 WASM: at System.SecurityUtils.SecureCreateInstance (System.Type type, System.Object[] args, System.Boolean allowNonPublic) <0x22e6bf0 + 0x0009c> in <485de8e868c44158864a67df7e8a64c8>:0
components.webassembly.js:1 WASM: at System.SecurityUtils.SecureCreateInstance (System.Type type) <0x22e6b18 + 0x0000c> in <485de8e868c44158864a67df7e8a64c8>:0
components.webassembly.js:1 WASM: at System.ComponentModel.ReflectTypeDescriptionProvider.CreateInstance (System.Type objectType, System.Type callingType) <0x22e1b90 + 0x0003c> in <485de8e868c44158864a67df7e8a64c8>:0
components.webassembly.js:1 WASM: at System.ComponentModel.ReflectTypeDescriptionProvider.SearchIntrinsicTable (System.Collections.Hashtable table, System.Type callingType) <0x22e1608 + 0x002f0> in <485de8e868c44158864a67df7e8a64c8>:0
components.webassembly.js:1 WASM: at System.ComponentModel.ReflectTypeDescriptionProvider+ReflectedTypeData.GetConverter (System.Object instance) <0x22c3400 + 0x00188> in <485de8e868c44158864a67df7e8a64c8>:0
components.webassembly.js:1 WASM: at System.ComponentModel.ReflectTypeDescriptionProvider.GetConverter (System.Type type, System.Object instance) <0x22c2cc8 + 0x00022> in <485de8e868c44158864a67df7e8a64c8>:0
components.webassembly.js:1 WASM: at System.ComponentModel.TypeDescriptor+TypeDescriptionNode+DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter () <0x22c2a00 + 0x00042> in <485de8e868c44158864a67df7e8a64c8>:0
components.webassembly.js:1 WASM: at System.ComponentModel.TypeDescriptor.GetConverter (System.Type type) <0x22c7e18 + 0x00014> in <485de8e868c44158864a67df7e8a64c8>:0
components.webassembly.js:1 WASM: at Blazorise.JSRunner+<>c__DisplayClass10_0`1[TValue].b__0 (System.String value) [0x0000d] in D:\Projects\Other\Blazorise\Source\Blazorise\JSRunner.cs:96

@rynowak
Copy link
Member

rynowak commented Feb 21, 2019

I think it's definitely the case that the linker is trimming this out. The specific type Int32Converter is getting trimmed by the linker because it's not used directly by any code.

One workaround you could use it to set <BlazorLinkOnBuild>false</BlazorLinkOnBuild> in the project file. This will turn off linking, which will increase the size of the app. A more desirable thing would be to change the settings to include the type converters.

@javiercn any ideas?

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Feb 21, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Mar 25, 2019
@mkArtakMSFT mkArtakMSFT added the External This is an issue in a component not contained in this repository. It is open for tracking purposes. label Apr 3, 2019
@miroslavp
Copy link

I declared static variables like this to prevent IL stripping:

    private static readonly Int32Converter i32c = new Int32Converter();
    private static readonly Int64Converter i64c = new Int64Converter();
    private static readonly StringConverter sc = new StringConverter();
    private static readonly NullableConverter nc = new NullableConverter(typeof(bool?));
    private static readonly BooleanConverter bc = new BooleanConverter();

@rynowak @javiercn
Any better solution for this?

A more desirable thing would be to change the settings to include the type converters.

Where can I find those settings?

@javiercn
Copy link
Member

@SteveSandersonMS Can users still add a custom descriptor file to tweak the linking process?

@SteveSandersonMS
Copy link
Member

Yes, but weirdly the docs for that feature seem to have been deleted. I found an old copy in Git history here: https://github.com/aspnet/Blazor.Docs/blob/7ced0029b97b3b8afdda4e2a235bd6cecebfc2fd/docs/host-and-deploy/configure-linker.md

@danroth27 Do you know why that was removed? Also on https://blazor.net/, the "docs" link in the header goes to a 404, and there's still the "experimental" tag in the top right.

@danroth27
Copy link
Member

weirdly the docs for that feature seem to have been deleted. @danroth27 Do you know why that was removed?

The IL linker doc is here: https://docs.microsoft.com/aspnet/core/host-and-deploy/blazor/configure-linker

Also on https://blazor.net/, the "docs" link in the header goes to a 404

I'm not seeing this. When I click on Docs I get redirected to https://docs.microsoft.com/aspnet/core/blazor/

there's still the "experimental" tag in the top right.

That's expected for now

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components External This is an issue in a component not contained in this repository. It is open for tracking purposes.
Projects
None yet
Development

No branches or pull requests

8 participants