Early-Bound Classes for .NET 4.6.2 and 6.0

by Jun 26, 2023

You like to use strong types in .NET when working with Dataverse / Dynamics 365? Are you into Early-Bound Classes? Generating entity classes? You can use CrmSvcUtil for this, but I personally like to use XrmContext from Delegate to do this, because it creates smaller files.

Using Early-Bound Classes in .NET 6.0 and up

So now I have a project Entities that have my generated classes. I want to use these generated classes inside Dataverse, like Plugins. But I also want to use them outside Dataverse, when making API calls, like from Azure Functions.

Early-Bound Classes

Within Dataverse we are still stuck with .NET 4.6.2 and need to use the Microsoft.CrmSdk.CoreAssemblies library from the SDK.

But outside Dataverse we can use the new NuGet package Microsoft.PowerPlatform.Dataverse.Client. This new library supports .NET 6.0 and up, which is great! It is also possible to do async calls with the new interface IOrganizationServiceAsync.

Whenever possible, I want to run with the shiny new stuff and use the new library in .NET 6.0 and even .NET 7.0.

I also want to use my generated classes in my Entities project with the shiny new stuff. For this I need to update my Entities project to .NET 6.0 and need to swap Microsoft.CrmSdk.CoreAssemblies for Microsoft.PowerPlatform.Dataverse.Client.

But when I do this, I can’t use the Entities project anymore inside plugins in Dataverse 😞.

Targeting .NET 4.6.2 and .NET 6.0

Luckly MSBuild supports multitargeting framework and platforms. At least if you use the new lean .NET projects files, which were introduced when .NET core made it entrance (if you still didn’t upgrade, do it now!).

By editing the project file we can build it for .NET 4.6.2 and .NET 6.0.

  <PropertyGroup>
    <AssemblyName>AV.SpotlerAutomate.Entities</AssemblyName>
    <RootNamespace>AV.SpotlerAutomate.Entities</RootNamespace>
    ...
    <TargetFrameworks>net462;net7.0</TargetFrameworks>
  </PropertyGroup>

But this is not enough to get it working. We also need to swap the library we use from the SDK, depending on the target framework we are using.

We can use MSBuild conditions for this. When adding reference packages to the project file we specify them with a condition. See below:

  <ItemGroup>
    <PackageReference Condition="'$(TargetFramework)' == 'net462'" Include="Microsoft.CrmSdk.CoreAssemblies" Version="9.0.2.49" />
    <PackageReference Condition="'$(TargetFramework)' == 'net6.0'" Include="Microsoft.PowerPlatform.Dataverse.Client" Version="1.1.9" />
  </ItemGroup>

Now my Entities project is building correctly into two versions, one for .NET 4.6.2 and the other for .NET 6.0. I can now use my generated classes in plugins and in .NET 6.0 code!

Hopefully, this will help you out until Dataverse / Dynamics 365 itself is being upgraded to .NET 6.0 or up!

Subscribe to
The Daily Friction

A daily newsletter on automation and eliminating friction

Related Content

Form Data OnLoad

Form Data OnLoad

You want to call Form OnLoad multiple times? Don't! I was reading about a blogpost to trigger the Form OnLoad multiple times. This should happen after the data was saved and/or refreshed. Why would you want this? Usually, the Form OnLoad contains logic to change the...

read more
Who is the target audience for Power Pages

Who is the target audience for Power Pages

Power Pages is slowly getting better and better product. But then I look at the pricing page: https://powerpages.microsoft.com/en-us/pricing/ Who is the target audience for this product? If you need something 'quick and dirty' it is nice to have a website up and...

read more
Power Pages Secure?

Power Pages Secure?

Power Pages! They tell me it's easy and is very secure, it's built in! I am not fully convinced when I hear this. Why? Simple said: "Easy and Security don't mix". Making something more secure will make it less easy to use. Data separation The thing is, Dataverse is...

read more