简体   繁体   中英

Kentico Kontent DeliveryClient.Build in Kentico Portal could not load file or assembly Microsoft.Extensions.DependencyInjection

I am attempting to call Kentico Kontent using their DeliveryClient in a Kentico Portal project that doesn't have dependency injection. This project would like to migrate to Kontent but would like a new feature in the extisting project to be implemented with Kontent before the transition.

After installing the Kontent Delivery SDK, here's my code

var clientTest = DeliveryClientBuilder.WithProjectId("MyProjectId").Build();

I get a runtime error

System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.Extensions.DependencyInjection, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.'

Obviously, this project doesn't have dependency injection and I hear setting it up in a portal project is difficult if not impossible. So I decide to new up my own delivery client as outlined in Kentico's blog post

var test = new DeliveryClient(options);

But DeliveryClient is marked internal: 'DeliveryClient' is inaccessible due to its protection level

How do I proceed?

Details

  • Kentico.Kontent.Delivery 12.3.0
  • Kentico version 12.0.22
  • .Net Framework 4.6.1

Although the Kentico.Kontent.Delivery 12.* targets netstandard2.0 which means it should be compatible with .NET Framework 4.6.1 , there is evidence that this setup can cause issues. There are a few things that I would recommend before trying anything else:

  1. Upgrade to .NET 4.8 (or at least to 4.7.2)
  2. Enable Automatic Binding Redirection in your project by adding:
<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

No matter how you instantiate/register the DeliveryClient ( DeliveryClientBuilder services.AddDeliveryClient() ), the SDK always uses Microsoft.Extensions.DependencyInjection internally, so there is no workaround. You will need to address the assembly binding problem.

The actual issue was much dumber. I had installed the Kontent Delivery Client nuget sdk in another project and only referenced it in this one. Installing it for this project solved the problem. I am able to call the builder without any errors now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM