The error message you're encountering suggests that Dynamics 365 is unable to find or load the HtmlAgilityPack assembly when your plugin executes, even though it's present in the bin folder during local builds. This issue commonly occurs in Dynamics 365 plugins due to assembly loading restrictions and deployment nuances.
HtmlAgilityPack
bin
Issue: Even though the DLL is in your local bin folder, Dynamics 365 plugins run in a sandboxed environment, which does not have access to external libraries unless they are explicitly included during deployment.
Solution: Make sure that the HtmlAgilityPack.dll is included in the Plugin Assembly during registration. You can do this in one of two ways:
HtmlAgilityPack.dll
1. ILMerge or ILRepack (Recommended for sandboxed environments)
Merge the HtmlAgilityPack DLL with your plugin assembly to ensure it is embedded within your main DLL.
Steps:
ILRepack
Install-Package ILRepack
ilrepack /out:YourPluginMerged.dll YourPlugin.dll HtmlAgilityPack.dll
YourPluginMerged.dll
Instead of external DLL references, embed the HtmlAgilityPack within your plugin assembly by:
.csproj
<ItemGroup> <EmbeddedResource Include="HtmlAgilityPack.dll" /> </ItemGroup>
using System.Reflection; using System.IO; public static Assembly LoadEmbeddedAssembly() { var resourceName = "YourNamespace.HtmlAgilityPack.dll"; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { byte[] assemblyData = new byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } }
If your Dynamics 365 instance might be using an older version of the library, you can ensure compatibility using a binding redirect in the app.config:
app.config
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="HtmlAgilityPack" publicKeyToken="bd319b19eaf3b43a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.11.72.0" newVersion="1.11.72.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
The issue could be related to missing or improperly loaded dependencies during the plugin execution in your CRM environment. Here are some steps to troubleshoot and resolve the problem:
Ensure Proper Assembly Deployment: Verify that the HtmlAgilityPack.dll is correctly deployed to the environment where the plugin is executing. For CRM plugins, dependencies must be included in the deployment package if they are not part of the GAC (Global Assembly Cache).
Merge the Dependency (ILMerge or ILRepack): Since CRM plugins run in a sandboxed environment, you can use tools like ILMerge or ILRepack to merge the HtmlAgilityPack assembly into your plugin assembly. This ensures all dependencies are bundled into a single DLL.
Check the CRM Assembly Isolation: If your plugin is registered in "Isolation Mode" (Sandbox), ensure all referenced assemblies are compatible with sandboxed plugins. The sandbox environment restricts certain operations, and external dependencies can sometimes cause issues.
Confirm Assembly Version and Token: Double-check the version, culture, and public key token of the HtmlAgilityPack assembly. Ensure they match the version you've referenced in your project. Mismatched assembly versions can cause this error. Receiptify
Add the Assembly to the GAC (if applicable): If your CRM environment is not sandboxed and you have access to the server, consider adding HtmlAgilityPack.dll to the GAC. This ensures the assembly is available globally.
Enable Detailed Logging: Add detailed logging to your plugin to capture more information about the error. Log details like the full stack trace, current working directory, and loaded assemblies.
Test Locally: Run the same plugin code in a local environment outside the CRM sandbox to ensure the logic functions correctly.
Alternative Solution: If merging or deploying the dependency is not feasible, consider using built-in .NET libraries like System.Xml or System.Text.Json to parse the HTML if your requirement can be simplified.
System.Xml
System.Text.Json
If you decide to use ILMerge, here's a simple guide:
ILMerge /out:YourPluginMerged.dll YourPlugin.dll HtmlAgilityPack.dll
Let me know if you need further guidance or code examples for any of these steps!
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
We are honored to recognize Mansi Soni as our August 2025 Community…
A new season of Super Users has arrived, and we are so grateful for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.