And this was the complaint from one my customer. Why CRM assembly references in Azure function apps are throwing error? To add to my woes, that was some pretty important functionality running inside those small little functions and alas, the last few runs have failed. It was few months back and at that point of time, I fixed it.
But in a recent training on WebHooks, it seemed to me participants are not aware of this scenario and hence penning this blog for the benefit of my readers.
If you are absolutely new to Azure functions and how to add assembly references in Azure functions, I guess a bit of basic understanding before reading this blog would help.
If you would have developed Azure functions and referenced CRM SDK assemblies, you would done using project.json file. You would have created the file and then added the below JSON in the file to reference CRM SDK’s
With azure functions 1.0, this would have worked just fine. At runtime, the framework would read your project.json file and restore the assemblies from Nuget. A detailed description on this by friend and fellow MVP Nishant Rana in this blog.
But the issue starts once Azure functions are upgraded to V2.0. And why? Because project.json is no longer supported in V2.0. Instead a new file – function.proj needs to created and referenced assemblies need to be defined in specific format.
All we need is to add a file with name “function.proj” and put the below contents.
<Project Sdk="Microsoft.NET.Sdk"> < PropertyGroup> < TargetFramework>net461</TargetFramework> </PropertyGroup> < ItemGroup> < PackageReference Include="Microsoft.CrmSdk.CoreAssemblies" Version="22.214.171.124" /> </ItemGroup> < /Project>
Your function code remains unchanged. But for beginners, you would need to reference with #r directive before using it.
Now once you run, you will see that the packages are now successfully restored and the application is working fine.
P.S – I tried at that time with .NET framework 4.6.2 and using <TargetFramework>net462</TargetFramework>. Unfortunately with this version it threw an error. May be I am missing something silly and I would be grateful if readers point this out.
Ok, so my CRM Sdk assemblies are in Nu-get and I can refer them just fine in the new function.proj file. But what about custom assemblies which are not in Nuget. How do I refer them? Well, if that’s the case, you should not be worried. There is an easy way to do this.
For this example, let’s assume Microsoft.Xrm.Sdk.dll is my custom assembly.
In your functions app, navigate to Platform Features –> Advanced Tools (Kudu)
Once inside Kudu, open Powershell prompt
Navigate to Site –> wwwroot. Once inside the wwwroot folder, create a new folder called bin
Go inside bin folder and drag and drop your assembly. In my case it is Microsoft.Xrm.Sdk.dll
All set and done. Now we just need to refer this assembly in our code file. As shown in the below screenshot, we can refer it by using a relative path.
Now you can start using your assembly. Simple isn’t it?
Honestly this part is not something related to specifically to D365 but with Microsoft’s vision of removing technology barriers, a CRM architect/ developer would frequently find themselves working on Azure functions and this might be some information which saves some time.
Hope this helps!
Our product offerings:
Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)
CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)
Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)
Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)