0
votes

I have created a windows form application in C# (.NET 4.6.2). I am using the below piece of code to connect to Dynamics 365 online:

ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "[email protected]";
clientCredentials.UserName.Password = "XXXXXXXX";

Uri crmURL = new Uri("https://XXXXX.api.crm4.dynamics.com/XRMServices/2011/Organization.svc");

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (OrganizationServiceProxy orgaSvcProxy = new OrganizationServiceProxy(crmURL, null, clientCredentials, null))
{
    IOrganizationService orgaService = (IOrganizationService)orgaSvcProxy;
    if (orgaService != null)
    {
        Guid userid = ((WhoAmIResponse)orgaService.Execute(new WhoAmIRequest())).UserId;
        if (userid != Guid.Empty)
        {
            MessageBox.Show("Connection established successfully");
        }
    }
}

I have referenced following 2 dlls in my VS 2015 (.NET 4.6.2) project:

  1. Microsoft.Xrm.Sdk.dll
  2. Microsoft.Crm.Sdk.Proxy.dll

Everything works fine if I launch the windows form from VS IDE or launch the windows form using exe file. But if I merge the exe and above mentioned two dlls in a single exe file using ILMERGE and launch the windows form using merged exe then following line of code starts failing:

OrganizationServiceProxy orgaSvcProxy = new OrganizationServiceProxy(crmURL, null, clientCredentials, null)

Above line of code throws following error:

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Xrm.Sdk.Client.ServiceMetadataUtility.GetSDKVersionNumberFromAssembly() at Microsoft.Xrm.Sdk.Client.ServiceMetadataUtility.RetrieveServiceEndpointMetadata(Type contractType, Uri serviceUri, Boolean checkForSecondary) at Microsoft.Xrm.Sdk.Client.ServiceConfiguration1..ctor(Uri serviceUri, Boolean checkForSecondary) at Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration..ctor(Uri serviceUri, Boolean enableProxyTypes, Assembly assembly) at Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateConfiguration[TService](Uri serviceUri, Boolean enableProxyTypes, Assembly assembly) at Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateConfiguration[TService](Uri serviceUri) at Microsoft.Xrm.Sdk.Client.ServiceProxy1..ctor(Uri uri, Uri homeRealmUri, ClientCredentials clientCredentials, ClientCredentials deviceCredentials) at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy..ctor(Uri uri, Uri homeRealmUri, ClientCredentials clientCredentials, ClientCredentials deviceCredentials) at ElevateCRMAccess.fmElevateCRMAcces.btnElevate_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I have used following line to merge assemblies using ILMERGE:

"$(SolutionDir)ILMERGE/IlMerge.exe" /target:winexe /targetplatform:"v4,C:\Windows\Microsoft.NET\Framework\v4.0.30319" /out:"$(SolutionDir)..\Output\ConnectToCRM.exe" "$(TargetDir)ConnectToCRM.exe" "$(TargetDir)Microsoft.Crm.Sdk.Proxy.dll" "$(TargetDir)Microsoft.Xrm.Sdk.dll"

Could someone please help identify the issue here. What is causing the merged exe to be failed.

2

2 Answers

0
votes

The state of the art for using the SDK to connect to Dynamics 365 (a.k.a. CDS) from an external app is CrmServiceClient, which is available in the Xrm Tooling NuGet package.

Once you add that NuGet package to your project, you're ready to go.

using Microsoft.Xrm.Tooling.Connector;
var svc = new CrmServiceClient("Url=https://dev26.crm.dynamics.com; [email protected]; Password=Pass; AuthType=Office365");
svc.Retrieve("account", new Guid("7D9AAF71-5845-E811-A950-000D3A1D7326", new ColumnSet(true)) ;

Please see this answer for additional connection strings.

I've been using it with .NET Framework 4.7.1+ to connect C# apps to Dynamics CRM 2013 through Dynamics 365 v9.

Whether you need to do the ILMerge or not, you might want to consider switching to XrmTooling. And if you can avoid ILMerge, all the better. For the record, ILMerge is unsupported for plugins.

0
votes

I know it is an old question, but I just saw this question and thought maybe an answer could help people with same problem.

According to the error, the problem is that SDK is looking at the DLL to determine the SDK version (GetSDKVersionNumberFromAssembly), and when you use ILMerge to merge dlls, you are moving assembly content into a new assembly, and so SDK can not find out the version since it doesn't know your dll.

To solve the problem, you can store SDK dll as a resource in your assembly file and extract it on startup, or use a tool like Fody Custora to automate this process. The Custora output is very similar to ILMerge, but Custora don't merge ILs, it simply add external dlls as resources.