Skip to content

GenerateResource task does not support non-string resources on .NET core #2221

Closed
dotnet/corefx
#36906

Description

@nguerrera

From @tarekgh on June 14, 2017 15:54

@Spongman commented on Tue Jun 13 2017

a (.csproj) project a with an embedded text file, added using the VS2017.2 project properties UI.

ConsoleApp2.zip

on windows, it works fine.

on linux, it just shows:

$ dotnet --version
1.0.4
$ uname -a
Linux mybox 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2 (2017-04-30) x86_64 GNU/Linux
$ dotnet run
..\Resources\New Text Document.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252

@danmosemsft commented on Tue Jun 13 2017

@tarekgh are there any known issues here?


@tarekgh commented on Wed Jun 14 2017

Please take a look at the issue https://github.com/dotnet/cli/issues/3695 this was CLI issue and I believe it is already fixed but I am not sure which release include the fix. I'll move this issue to CLI repo.

Copied from original issue: dotnet/cli#6866

Activity

  1. nguerrera commented on Jun 15, 2017

    @nguerrera
    ContributorAuthor

    MSBuild does not support currently support non-string resources on .NET Core. However, it is not checking the resx data's type attribute, but instead blindly using the value as a string. This should either work or have a clear "not supported" error at build time.

    Relevant snippet from repro project resx:

      <data name="New Text Document" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>..\Resources\New Text Document.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
      </data>

    Code that is not checking the type attribute:
    https://github.com/Microsoft/msbuild/blob/cc727f715651f305639ad8635fefc40165299f86/src/Tasks/GenerateResource.cs#L2909-L2920

    It might be easy enough to actually handle this ResxFileRef-to-string case correctly. If we do that, we still need a clear error in other non-string data cases.

  2. eltiare commented on Sep 3, 2017

    @eltiare

    I am eagerly awaiting the embedding of all resources via the commandline as it will make my life easier with the watch command.

  3. ThatRendle commented on Sep 4, 2017

    @ThatRendle

    As a stop-gap solution while we wait for resource embedding to become a properly-supported, cross-platform, I created a dotnet tool which does something vaguely similar: https://github.com/RendleLabs/Embedder/blob/master/README.md

    (I forgot to git push, will do that soon)

  4. jetersen commented on Nov 8, 2017

    @jetersen

    This also applies to images even on Windows using dotnet build, msbuild 😭

    .NET Command Line Tools (2.0.2)
    
    Product Information:
     Version:            2.0.2
     Commit SHA-1 hash:  a04b4bf512
    
    Runtime Environment:
     OS Name:     Windows
     OS Version:  10.0.16299
     OS Platform: Windows
     RID:         win10-x64
     Base Path:   C:\Program Files\dotnet\sdk\2.0.2\
    
    Microsoft .NET Core Shared Framework Host
    
      Version  : 2.0.0
      Build    : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d
    
  5. viktors-telle commented on Dec 6, 2017

    @viktors-telle

    Any ETA on fixing this?

  6. auxym commented on Dec 11, 2017

    @auxym

    I'm also getting with issue with dotnet build / dotnet msbuild, but not with the msbuild.exe packaged with VS2017, which seems to correctly embed string resources.

  7. AlgorithmsAreCool commented on Jan 17, 2018

    @AlgorithmsAreCool

    This explains a lot. I had a WinForms application that would fail to launch at runtime with dotnet build but worked fine in VS2017. I was getting an InvalidCastException when loading an image resource.

    EDIT
    As auxym says, dotnet msbuild does not work as of cli version 2.1.2. You need to call the msbuild visual studio installs. For me this is located at:

    "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe"
    

    For some reason this still does not work in release configuration. I have to use debug to get the resources embedded correctly. Hope this helps until a proper fix is available.

  8. mikebridge commented on Feb 1, 2018

    @mikebridge

    I have a ResourceManager.GetObject call on an embedded resource which succeeds on windows but fails with the following error on using dotnetcore 2.0.3 (from the dotnet-sdk-2.0.3 docker image) on Linux:

    System.InvalidCastException : Unable to cast object of type 'System.String' to type 'System.Byte[]'.
    

    Is this a related issue?

    I have an embedded text file "grades.json" which is accessed via the autogenerated Resources.Designer.cs file. That error message occurs here:

    internal static byte[] grades {
        get {
            object obj = ResourceManager.GetObject("grades", resourceCulture);
            return ((byte[])(obj));
        }
    }
  9. danmoseley commented on Feb 1, 2018

    @danmoseley

    ResourceManager will support binary blobs in resources with https://github.com/dotnet/corefx/issues/26745. I am guessing this is also "Unable to cast object of type 'System.String' to type 'System.Byte[]'" .. @ViktorHofer can verify.

    I had a WinForms application that would fail to launch at runtime with dotnet build

    Bear in mind that Winforms is not supported on .NET Core so if it's actually a Winforms app you will hit more errors after this when you try to load a Winforms type.

    One other Winforms caveat: the set of binary-serializable types is smaller in .NET Core than in .NET Framework (list) and it does not include for example Image, Icon and Font - types that the Winforms designer likes to binary serialize into resources.

  10. mikebridge commented on Feb 1, 2018

    @mikebridge

    @danmosemsft this particular instance is a c# library called from a webapp.

  11. danmoseley commented on Feb 1, 2018

    @danmoseley

    @mikebridge what are the type / mimetype you see in the resx?
    eg

      <data name="pictureBox1.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
    
  12. mikebridge commented on Feb 1, 2018

    @mikebridge

    @danmosemsft The resx file shows this:

    <data name="grades" type="System.Resources.ResXFileRef, System.Windows.Forms">
      <value>..\grades.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
    </data>
    

    I didn't realize it was specific to winforms---I can embed this file another way if necessary....

  13. danmoseley commented on Feb 1, 2018

    @danmoseley

    Yeah, ResxFileRef is a Winforms type for whatever reason (although presumably it doesn't need Winforms proper so it could be ported.) You'll want to embed as a string or a binary formatted string directly into the resx.

  14. 87 remaining items

  15. voltcode commented on Jul 30, 2019

    @voltcode

    This fix should be backported to MSBuild 15 at least and .NET Core 2.1/2.2, resource files are a very old feature, lack of support for them prevents people on .NET FX from migrating onto .NET Core.

  16. nguerrera commented on Jul 30, 2019

    @nguerrera
    ContributorAuthor

    @voltcode I'm afraid adding support for something that never worked in that combination would not be allowed for a servicing change. We keep a very high bar for that. Taking big features like this backwards adds too much risk. This was a very large change across three repositories. We have fixed this for .NET Core 3.0+ and MSBuild 16.3+, and that is all we can do without a time machine.

  17. voltcode commented on Jul 30, 2019

    @voltcode

    What do you mean it never worked? It worked in Visual Studio. This is a big inconsistency, resource files are from year 2003 and you're forcing people to move to VS 2019 (paid upgrade) to fix it.
    Unless NET Core 3.0 will be supported in VS 2017?

  18. nguerrera commented on Jul 30, 2019

    @nguerrera
    ContributorAuthor

    It is not supported in VS 2017. There has been a lot of work in msbuild, NuGet, the project system, and other VS components for .NET Core 3.

  19. jnm2 commented on Jul 30, 2019

    @jnm2

    @voltcode What I've been doing is using Visual Studio's .NET Framework msbuild which has always supported non-string resources rather than .NET Core's msbuild which had never supported them. I used this starting with VS2017:

    $visualStudioInstallation = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
    $msbuild = Join-Path $visualStudioInstallation 'MSBuild\15.0\Bin\MSBuild.exe'
    & $msbuild path\to\project\folder /restore /p:Configuration=Release /v:minimal /nologo
  20. nguerrera commented on Jul 30, 2019

    @nguerrera
    ContributorAuthor

    What do you mean it never worked?

    I mean .NET Core 2.x projects with .NET Core SDK 2.x behave as they always have. It is regrettable that they did not have the result of this work, but it is simply too late to change that.

    .NET Core 2.x filled many gaps with .NET Framework vs. .NET Core 1.x, but not this one. We didn't backport all of those gap closings to 1.x either. We literally cannot do that. We have to defend every backport rigorously, and the change will be rejected if it is closing a gap that was always there.

    We will get tenfold more passionate feedback when we break something in a servicing update. We are holding this bar with the best of intentions. The risk is simply too high.

  21. voltcode commented on Jul 30, 2019

    @voltcode

    thanks @jnm2 . Just to clarify by .NET FX you mean the real, old school MSBuild.exe, and by .NET Core msbuild you mean dotnet build command?

    When writing build scripts, I would like exactly the same behavior between script and VS. To achieve this goal - what is better to use for scripts - MSBuild.exe or dotnet build ?

    We hit the resources problem when using dotnet test in build scripts - as far as I understand - if we use MSBuild.exe to build entire solution and dotnet test with --no-build flag - we should be fine? We're using .NET Core 2.2.

  22. rainersigwald commented on Jul 30, 2019

    @rainersigwald
    Member

    Visual Studio uses MSBuild.exe to build. Using that will be closer to the VS build.

    Yes, using MSBuild.exe followed by dotnet test --no-build sounds reasonable in your use case.

  23. voltcode commented on Jul 31, 2019

    @voltcode

    Thanks @rainersigwald, we've tested your suggestion and it we're definitely going to stay on MSBuild.exe and avoid dotnet * tools as much as possible to replicate VS behavior.

  24. jzabroski commented on Nov 21, 2019

    @jzabroski

    EDIT: *** IF YOU ARE RUNNING INTO THIS ERROR WHEN THEN READ THIS - YOUR BUILD SERVER'S "BUILD TOOLS FOR VISUAL STUDIO 2019" MAY NOT BE UP TO DATE ***

    I am able to successfully run my app from my command line on my local dev machine, but cannot run it after it's being built in TeamCity.

    Local Dev Machine

     & "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\M
    sBuild.exe" --version
    Microsoft (R) Build Engine version 16.2.37902+b5aaefc9f for .NET Framework
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    MSBUILD : error MSB1001: Unknown switch.
    Switch: --version
    
    For switch syntax, type "MSBuild -help"

    TeamCity Build Server

    Visual Studio Build Tools 16.1.9
    image

    When I click "Launch", I get:

    **********************************************************************
    ** Visual Studio 2019 Developer Command Prompt v16.0
    ** Copyright (c) 2019 Microsoft Corporation
    **********************************************************************

    Attempt to fix

    1. Go to https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019
    2. Download "Build Tools for Visual Studio 2019" (vs_buildtools__1704816089.1574373004.exe)
    3. Get file SHA256 hash with PowerShell: 7DA4388648C92544C97407C6F052FD6BC0317DB407CADAB9FDCB328A34D3E317
    PS C:\Users\john.zabroski> Get-FileHash "C:\Users\john.zabroski\Downloads\vs_buildtools__1704816089.1574373004.exe"
    
    Algorithm       Hash                                                                   Path
    ---------       ----                                                                   ----
    SHA256          7DA4388648C92544C97407C6F052FD6BC0317DB407CADAB9FDCB328A34D3E317       C:\Users\john.zabroski\Downlo...
    1. Install the exe
    2. Running the installer indicates it is 16.3.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions