1

I am using VS Pro Update 3

I have been following the guide here: https://www.hanselman.com/blog/HowToReferenceAnExistingNETFrameworkProjectInAnASPNETCore10WebApp.aspx

I have 3 existing .NET 4.5.1 Class Libraries, written in VB.NET but I don't think that should matter as it's all on CLR.
My ASP.NET Core project is C#, targeting .NET 4.5.1.
Here is the Framework section of my project.json:

  "frameworks": {
    "net451": {
      "dependencies": {
        "WebApp.Api.Services": {
          "target": "project"
        }
      }
    }
  },

If I add all my VB.NET class libraries using Add Existing Project to my solution and try to add them as project reference, I get the following error:

Project XXX type is unsupported by current project and cannot be added as a reference

I also tried adding a C# class Library (WebApp.Api.Services), add VB.NET references to that, then add the C# Class Library as a reference to the ASP Core project
Doing this shows the references in Solution Explorer okay, it seems as though I can use types from the C# Class library but I can't use the VB.NET library's types in my code.

I also created a NuGet package, but no luck
I have read that you can create a 'wrapper' project somehow, or Visual Studio can do this automatically.

Has anyone ever tried adding VB.Net projects to ASP Core solution?

Luke T O'Brien
  • 2,565
  • 3
  • 26
  • 38

2 Answers2

2

A project built for .NET Core cannot use class libraries built for .NET Framework. They are not directly compatible with each other. In fact I'm confused when you say:

My ASP.NET Core project is C#, targeting .NET 4.5.1.

If it's targeting .NET 4.5.1, then it's not actually a .NET Core project.

You should port your class libraries to .NET Standard; that way they can be used by a wide range of compatible frameworks. It is also possible to have a class library target multiple frameworks.

Community
  • 1
  • 1
Gigi
  • 28,163
  • 29
  • 106
  • 188
  • It is actually possible to use ASP.NET Core on .NET 4.5.1: http://stackoverflow.com/a/38166777/3191599 – Nate Barbettini Apr 03 '17 at 18:55
  • 1
    @NateBarbettini: Gigi is probably referring to the question being tagged as [tag:.net-core], so under this premise: the short answer is: It's not possible to use a Class library with it, since a class library always implies full .NET Framework target. Only new-styled or old-styled PCL can be used with .NET Core, if they target a specific profile (i.e. `portable-net45+win8` or similar) – Tseng Apr 03 '17 at 19:02
  • The long answer is: It **may be possible**, to use it if you a) use vs 2017 (or 2015 with nuget) **and** b) are 110% certain that it doesn't use any types not available in .NET Core **and** c) you use `import` or `PackageTargetFallback` to force nuget to restore this package despite it targeting says its not compatible. This is **very dangerous** though as you will also allow any net45 package to be used in .NET Core even if its not compatible, so one **should never use** that route and rather convert the library to a PCL targeting `netstandard1.x` – Tseng Apr 03 '17 at 19:06
  • Yes sorry, I meant using the VS Template `ASP.NET Core Web App (.NET Framework)`, so not actually .NET Core, I will update my question..... I do find all these different Frameworks slightly confusing. Unfortunately it may take years to port all of our code to .NET Standard, we might need to start from scratch. Surly there must be a way? Are there any porting tool available? – Luke T O'Brien Apr 04 '17 at 08:28
  • After more investigating I have found a `System.BadImageFormatException` in the log, so I thought it might be x86 vs x64 issue rather then what I originally thought - I will do more testing – Luke T O'Brien Apr 04 '17 at 09:47
1

I managed to solve my issue.

The guide here is correct, I had another different issue that I didn't realize.
I found a System.BadImageFormatException error which lead me to the following question Start an asp.net core project as x86 under visual studio 2015
I uninstalled the x94 version of .NET Core and installed the x86 one.

I still could not add references to the VB.NET libraries to the ASP Core project, so my C# services 'Bridge' library was vital.

In my 'Bridge' library I have Models that I can return from my ASP Controllers:

Public BridgeModelA : VB.NETLibrary.ActualModelA
{
…
}

Then extensions

Public static VB.NETLibrary.ActualModelA ToModel(this BridgeModelA)
{
…
}

Although the base members of these models cannot be accessed in the controllers, due to ASP proj not referencing the VB projs, they are returned by the controller with JSON result containing all base members.
This is actually great as it fits in with the design of thin controllers.

Community
  • 1
  • 1
Luke T O'Brien
  • 2,565
  • 3
  • 26
  • 38