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!

Remy van Duijkeren

Remy van Duijkeren

Power Platform Advisor

Microsoft Power Platform Advisor with over 25 years of experience in IT with a focus on (marketing) automation and integration.

Helping organizations with scaling their business by automating processes on the Power Platform (Dynamics 365).

Expert in Power Platform, Dynamics 365 (Marketing & Sales) and Azure Integration Services. Giving advice and strategy consultancy.

Services:
– Strategy and tactics advise
– Automating and integrating

Subscribe to
The Daily Friction

A daily newsletter on automation and eliminating friction

Related Content

External authentication with Dataverse ServiceClient

For a while now we can use the new Dataverse ServiceClient that replaced the old CrmServiceClient. It has three big improvements: Works for .NET 5.0 and up (.NET Core) Uses the newer MSAL.NET instead of ADAL.NET (which is out of support) for authentication Support for...

read more

Everyone got ALM wrong in Dynamics 365 / Dataverse

For ages, we've been ferociously encouraging the integration of developer practices, such as source control and ALM, into the Dynamics 365/Dataverse realm. The ultimate truth The revered 'Master Branch' in source control, has always been the sole fountainhead from...

read more
WordPress Blocks Post using the REST API

WordPress Blocks Post using the REST API

Sending email to my subscribers is done by an automation flow. In the last couple of days paragraphs in my emails started to disappear. It changed into one large blob of text which is very annoying to read. Sorry about this! The automation depends on the RSS-feed of...

read more