使用ILMERGE合并dll后无法初始化OrganizationServiceProxy

 收藏

我已经在C#(.NET 4.6.2)中创建了一个Windows窗体应用程序。我正在使用下面的代码在线连接到Dynamics 365:

ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "XXXXX@XXX.onmicrosoft.com";
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");
        }
    }
}

我在我的VS 2015(.NET 4.6.2)项目中引用了以下2个dll:

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

如果我从VS IDE启动Windows窗体或使用exe文件启动Windows窗体,则一切正常。但是,如果我使用ILMERGE将exe和上面提到的两个dll合并到一个exe文件中,并使用合并的exe启动Windows窗体,则以下代码行将失败:

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

上面的代码行引发以下错误:

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)

我已使用以下行使用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"

有人可以在这里帮助您确定问题。是什么导致合并的exe失败。

回复
  • 云淡,风清 回复

    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.

    一旦将该NuGet包添加到项目中,就可以开始使用了。

    using Microsoft.Xrm.Tooling.Connector;
    var svc = new CrmServiceClient("Url=https://dev26.crm.dynamics.com; Username=user@dev26.onmicrosoft.com; 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.

    我一直在将它与.NET Framework 4.7.1+一起使用,以通过Dynamics 365 v9将C#应用程序连接到Dynamics CRM 2013。

    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.

  • Jared 回复

    我知道这是一个古老的问题,但我只是看到了这个问题,并认为也许答案可以对遇到同样问题的人有所帮助。

    根据该错误,问题在于SDK正在查看DLL以确定SDK版本(GetSDKVersionNumberFromAssembly),并且当您使用ILMerge合并dll时,您正在将程序集内容移动到新程序集中,因此SDK无法找到版本,因为它不知道您的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.