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.
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="220.127.116.11" /> <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!