4

I am trying to get my component to be resolved in my Web API project. I am using this code snippet for registering which works fine in my UnitTest project.

Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
IWindsorContainer container = new WindsorContainer(new XmlInterpreter("Windsor.config"));
container.Resolve<IDataProvider>();

But in my .Net Core Web API project I get this error:

Castle.MicroKernel.SubSystems.Conversion.ConverterException
  HResult=0x80131500
  Message=Could not convert string 'MyProject.DataAccess.DataProviders.PDataProvider, MyProject.DataAccess, Version=1.0.0.0, Culture=neutral' to a type. Assembly was not found. Make sure it was deployed and the name was not mistyped.
  Source=Castle.Windsor
  StackTrace:
   at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.GetType(String name)
   at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType)
   at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion(String value, Type targetType)
   at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion[TTarget](String value)
   at Castle.Windsor.Installer.DefaultComponentInstaller.SetUpComponents(IConfiguration[] configurations, IWindsorContainer container, IConversionManager converter)
   at Castle.Windsor.Installer.DefaultComponentInstaller.SetUp(IWindsorContainer container, IConfigurationStore store)
   at Program.<Main>$(String[] args) in C:\source\repos\F2\MyProject.F2\Program.cs:line 8

My Windsor.config file:

<?xml version="1.0" encoding="utf-8" ?>
<castle>
    <components>
        <component id="DataProvider" 
                   service="MyProject.Interfaces.DataProviders.IDataProvider, MyProject.Interfaces" 
                   type="MyProject.DataAccess.DataProviders.PDataProvider, MyProject.DataAccess, Version=1.0.0.0, Culture=neutral"></component>
    </components>
</castle>

My 'MyProject.DataAccess.dll' is in the '/bin' folder for both debug and release, which I am copying manually. Either the 'MyProject.DataAccess.dll' should be copied elsewhere where the Web API app can 'see' or locate it or the version of 'MyProject.DataAccess.dll' (.Net framework 4.8) is a problem.

Can anyone shed some light on what I am missing, please?

codingjoe
  • 707
  • 5
  • 15
  • 32
  • You can check the official document for [`dotnet compatibility Pack for .NET Core`](https://learn.microsoft.com/en-us/archive/msdn-magazine/2017/connect/net-introducing-the-windows-compatibility-pack-for-net-core)guideline here. However, its better to migrate your program for better performance. – Md Farid Uddin Kiron Dec 26 '22 at 06:14
  • Migration is not an option in my setup. At most I have to downgrade my .Net Core web api to .Net 4.8 web api. – codingjoe Dec 26 '22 at 08:53
  • I need to add that - I am supposing the difference between the two target framework is causing it. Although by the sound of the error message, it seems like my .Net Core Web API simply cannot see my *.dll C# library in which my implementation of IDataProvider resides, even though I manually copied the 'MyProject.DataAccess.dll' in the bin folder. – codingjoe Dec 26 '22 at 13:03
  • This is a duplicate of https://stackoverflow.com/questions/55907693/how-to-call-a-method-in-a-net-core-assembly-from-net-framework . Short answer is you cannot have cross-framework communication. The assembly loading infrastructure has no way to identify which framework target assembly belongs to - it assumes all code is in same framework. – Amogh Sarpotdar Jan 02 '23 at 13:08
  • I have converted my 4.8 project to .Net 6.0, so there shouldnt be any framework problems. Although I am still getting the exact same error message. Is there something I need to be aware of regarding the *.dll files my web api uses when it spins up an instance of my web app, when I press the play button? I feel like the debug/release folder with all the referenced assemblies are not being used in the web app in debug mode. – codingjoe Jan 05 '23 at 17:36
  • The problem seems to be the exact same as this: https://stackoverflow.com/questions/73143341/how-to-return-the-location-of-the-referenced-assemblies-in-a-referenced-class-li – codingjoe Jan 05 '23 at 18:23

1 Answers1

2

Use .NET Standard TFM instead of NET Framework. This way the assembly would be compatible between runtimes.

I was under the impression that .net core was backward compatible

The main idea behind Net Core is to allow moving forward and breaking back-compat. They (Core team) somewhat trying to be source-level compatible to easier mitigation from NETFx, but it's not a goal in the long term run.


UPD:

Starting form Net Core 2.0 can load NetFx assemblies (but not the other way). Make sure you correctly typed AssemblyQualifiedName of type (including PublicKeyToken and exact assembly version (it's not the same as file version)). Assembly should be placed near your application assembly (verify with AppContext.BaseDirectory from what directory your exe is running). Winsdor is using Type.GetType to find your type, you can debug it.

OwnageIsMagic
  • 1,949
  • 1
  • 16
  • 31
  • This. It becomes very annoying with the limited amount of supported features/libraries when targeting net standard 2.0, but it is the only way that works. Yay. If there needs to be a fix just for net standard 2.0, I recommend using the #if pragma with the target framework to have different sources for different target frameworks (e.g. 4.8 and standard 2.0) – Mafii Jan 03 '23 at 10:17