Mar*_*ski 0 c# dynamics-crm dynamics-crm-4
我正在编写应用程序,它使用CRM SDK 4连接到CRM.在第一个版本中,我一直在使用app.config文件("connectionStrings"部分)
<connectionStrings>
<add name="mycrm" connectionString="Authentication Type=Integrated; Server=http://ServerName/OrganizationName;"/>
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
和(在代码中):
DataContext ctx = new DataContext("mycrm");
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是现在,应用程序应该让用户选择身份验证类型,输入服务器名称,用户ID,密码等.我已经创建了连接字符串,但我不知道如何使用它.DataContext和CrmConnection对象AFAIK只有具有connectionString名称(来自app.config文件)的构造函数作为参数.当我尝试使用我的connectionString时,我得到异常:
System.ApplicationException was unhandled
Run Code Online (Sandbox Code Playgroud)
Message ="无法加载连接字符串名称'Authentication Type = Integrated; Server = http:// ServerName/OrganizationNamemsg)位于System.Windows.Application.RunInternal(窗口窗口)的System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架),位于C:\ Users\mrobaszynski\Desktop\PU\PaymentsUploader \中的PaymentsUploader.App.Main() obj\Release\App.g.cs:位于System.Threading.ExecutionContext.Run的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()的System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)中的第0行(ExecutionContext executionContext ,System.Threading.ThreadHelper.ThreadStart()InnerException:System.NullReferenceException Message ="对象引用未设置为对象的实例.",ContextCallback回调,对象状态).Source ="Microsoft.Xrm.Client"StackTrace:at Microsoft.Xrm.Client.CrmConnection.
小智 5
不,其他帖子是错误的 - 不要使用反射.执行此操作的正确方法是使用CrmConnection.Parse(customConnectionString)来构建crm连接.然后,您可以在构造函数中使用该连接构造数据上下文.
var crm = new XrmDataContext(CrmConnection.Parse(customConnectionString));
Shan McArthur
www.shanmcarthur.net