Search Unity

Bug Not adding NET_STANDARD_2_0 define on UWP platform

Discussion in 'Windows' started by bdovaz, May 7, 2021.

  1. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,053
    @Tautvydas-Zilys

    When switching to UWP it doesn't add NET_STANDARD_2_0 define, it adds NET_4_6 instead and I have .NET Standard 2.0 scripting backend set. According to this page it should be added:

    https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    You can see it in *.csproj files no NET_STANDARD_2_0 define added and in *.dll files inspector if you add a define constraint with NET_STANDARD_2_0 (you will see a red icon saying is not compatible).

    Case 1334636
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Thanks for the bug, we'll take a look.
     
  3. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,053
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    I'll request backports. The person that fixed it is on vacation right now.
     
    bdovaz likes this.
  5. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,053
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    It's assigned to the scripting team for backporting but unfortunately I have no visibility into how they are prioritizing it. I'll ask them.
     
    bdovaz likes this.
  7. holo-krzysztof

    holo-krzysztof

    Joined:
    Apr 5, 2017
    Posts:
    77
    I just ran into this as well.

    There seems to be more that's going wrong; my test code is:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         string queryString = "?test=&query=";
    4.         NameValueCollection qscoll = HttpUtility.ParseQueryString(queryString);
    5.  
    6.         foreach (var item in qscoll)
    7.         {
    8.             Debug.Log(item.ToString());
    9.         }
    10.     }
    It works fine in Editor when selected platform is Standalone Windows, but merely switching to UWP build target gives me compiler errors when HttpUtility is used unconditionally, even though it is part of the .NET Standard 2.0 API and it is also the active profile in ProjectSettings. It looks like what we're getting is not .NET Standard when building for UWP.

    FWIW, I made another bug report with version 2020.3.31, #1417085. I really hope this gets backported to 2020 LTS since upgrading Unity for XR projects is super brittle and even bumping minor versions has been hit and miss the last few times I did it (most recently with 2020.3.32).
    The same bug also happens with 2020.3.32, but even if it didn't we wouldn't be able to switch to it because of the MRTK/Unity deadlock on startup bug.
     
    Last edited: Apr 4, 2022
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Well that is bizarre. Thanks for the bug report, we'll investigate.

    It seems for whatever reason the project is building against .NET 4.7.1 instead of .NET 2.0. If you adding a file named "csc.rsp" with this contents in the project, the error will go away:

    Code (csharp):
    1. -r:System.Web.dll
    That should be a valid workaround until we fix it.
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    I found another, perhaps a better workaround. It seems that for whatever reason, the UI for API compatibility settings doesn't work. Running this editor script in the project once will fix that issue:

    Code (csharp):
    1. using UnityEditor;
    2.  
    3. public class EditorScript
    4. {
    5.     [MenuItem("Fix UWP scripting compilation/Do Fix")]
    6.     public static void FixUWPCompilation()
    7.     {
    8.         PlayerSettings.SetApiCompatibilityLevel(BuildTargetGroup.WSA, ApiCompatibilityLevel.NET_Standard_2_0);
    9.     }
    10. }
     
    holo-krzysztof likes this.
  10. holo-krzysztof

    holo-krzysztof

    Joined:
    Apr 5, 2017
    Posts:
    77
    This is nice. It seems to stick around even after deleting Library/reimporting the project, which is good.

    This I tried in our project where Standalone variant uses 4.x API while UWP is set to .NET Standard 2.0 and it didn't work for some reason.
    I've just checked and we do build against 4.x because Odin Serializer is in the project and that does not support .NET Standard it seems. Maybe I should change the project settings to 4.x API to avoid confusion.

    Edit: Oh, I think I know why it didn't work. I put the csc.rsp file into the Assets folder instead of beside the asmdef that actually uses System.Web.

    Adding a reference to System.Web with csc.rsp works in editor (.NET 4.x), but when making a UWP build I get a bunch of these (which fail the build):
    Reference rewriter: Error: type `System.Web.HttpUtility` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void Test::Start().

    I've reported a separate bug for this, #1417498.
     
    Last edited: Apr 5, 2022