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:
- Microsoft.Xrm.Sdk.dll
- 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.ServiceConfiguration
1..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.ServiceProxy
1..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.