Bridge.net .Net Framework 4.5 - You must add a reference to assembly mscorlib, Version=4.0.0.0

Alexandre Caila 21 Reputation points
2021-12-20T10:10:59.17+00:00

I'm trying to use Bridge.net, it's targeting .NET Framework 4.5 and I added a reference on the project to a custom library (.NET Framework 4.5) but when I try to inherit from a class within this library I get the error about mscorlib. I tried the only answer in Statckoverflow and I keep getting it.
To create the Bridge.net project, I used the visual studio template.

My packages.config:

<packages>
  <package id="Bridge" version="17.10.1" targetFramework="net45" />
  <package id="Bridge.Core" version="17.10.1" targetFramework="net45" />
  <package id="Bridge.Min" version="17.10.1" targetFramework="net45" />
  <package id="Bridge.Newtonsoft.Json" version="1.17.0" targetFramework="net45" />
  <package id="Microsoft.NETCore.Portable.Compatibility" version="1.0.1" targetFramework="net45" />
</packages>

My test class in the custom library:

namespace TestLib {
    public class Class1 {
        public void Hello() {
            Console.WriteLine("Hello world!");
        }
    }
}

My class in the Bridge.net class:

using TestLib;
namespace BridgeUnity {
    class Hello : Class1 {
    }
}
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2021-12-21T02:30:17.747+00:00

    mscorlib is a library that contains common types used by the runtime for .NET Framework apps, but that runtime isn't used for Bridge.NET apps - it just functions as a C# -> JS/TS transpiler, so it'll just use the JS runtime ultimately.

    As such any project/nuget/dll reference in your Bridge project that contains references that Bridge can't resolve will cause the Bridge compiler to throw this error you're seeing. We hit this snag a few years ago and ended up just moving any shared dependencies to Shared Projects:
    https://learn.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/shared-projects?tabs=windows

    Shared Projects code is compiled as part of each target project and so any dependencies will need to be resolvable to both your Bridge.NET project and your .NET project, although for simple POCO types or code that references the parts of the .NET Framework that were implemented by the Bridge team should be fine. You'll see a compiler error against the type if one of the referencing projects can resolve a dependency.

    One note about serialisation (as I see you're referencing Bridge.Newtonsoft): if your goal is to create simple serialisable types that you can transmit between the client & a server app, and if you're including type metadata in your JSON (serialised as $type properties) you'll need to make sure that the assembly part of that type matches between your client and server.

    What we ended up doing was referencing the shared project from our Bridge project, then creating a new .NET class library called {SHARED_PROJECT_NAME}.NET that also referenced the Shared Project, then our ASP.NET web app referenced that {SHARED_PROJECT_NAME}.NET instead of the shared project itself.

    This allowed us to change the assembly name of the .NET wrapper {SHARED_PROJECT_NAME}.NET to match the Bridge project, but without having to change the assembly name of our main ASP.NET app.

    Disclaimer: Bridge.NET was abandoned by the maintainers a few years ago (Object.NET) so unless this is a programming exercise I'd recommend perhaps looking around for an alternative transpiler to avoid the pains that my company went through over the last few years.

    3 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.