Using Dynamics CRM SDK with .NET Core

Tamas Molnar picture Tamas Molnar · Oct 24, 2018 · Viewed 10.2k times · Source

Is there a way to connect to Dynamics CRM 365 from a .NET Core application via the Dynamics SDK? Or should I use the Web Api?

I've read it could be possible, but when I reference the SDK from my .NET Core Class Library and try to connect I get the error:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Web.Services..

It seems like this DLL is not supported in .NET Core: How to use soap web services in Asp.net Core?

My code is like this:

 new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);

I could succesfully make it work from a .NET Framework project.

Answer

Alan Mervitz picture Alan Mervitz · Oct 27, 2018

There is a distinction to be made between applications that use the .NET Core runtime vs. the .NET Core framework. As you've found out, the Dynamics 365 SDK does not currently work with the .NET Core runtime, however it does work with the .NET Core framework when the .NET Core project targets the .NET Framework runtime using the .NET Framework's target framework moniker (TFM) setting in the project file. For example with a .NET Core console application, the .csproj file would look like this (notice the TargetFramework):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CrmSdk.CoreAssemblies" Version="9.0.2.5" />
  </ItemGroup>

</Project>

Such applications will not be cross-platform and only be executable on Windows, but still allows for the use of other .NET Standard class libraries and frameworks such as ASP.NET Core that implement .NET Standard while executing on the .NET Framework runtime. Eventually if the Dynamics 365 SDK is ever updated to work on the .NET Core runtime, the project file's target framework monitor value can be changed to .NET Core and become cross-platform.