尝试在连接到CRM的控制台应用程序上保存ServiceContext的更改时,为什么会出现NullReferenceException?

时间:2017-03-06 14:55:04

标签: dynamics-crm-online dynamics-crm-2016

我正在关注Build a console app that connects to CRM guide,当我尝试运行它时,会在NullReferenceException上抛出SaveChanges()。 (参见帖子底部的编辑内容)

以下是代码:

using System;
using System.Linq;
using Xrm;
using Microsoft.Xrm.Client;

namespace CRM_Console_Application
{
    class Program
    {
        static void Main(string[] args)
        {

            var xrm = new XrmServiceContext(new Microsoft.Xrm.Client.Services.OrganizationService(new CrmConnection("Xrm")));

            var allisonBrown = new Contact
            {
                FirstName = "Allison",
                LastName = "Brown",
                Address1_Line1 = "23 Market St.",
                Address1_City = "Sammamish",
                Address1_StateOrProvince = "MT",
                Address1_PostalCode = "99999",
                Telephone1 = "12345678",
                EMailAddress1 = "allison.brown@example.com"
            };

            xrm.AddObject(allisonBrown);
            xrm.SaveChanges(); //Here is where it throws an Exception
            WriteExampleContacts(xrm);
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }

        private static void WriteExampleContacts(XrmServiceContext xrm)
        {
            var exampleContacts = xrm.ContactSet.Where(contact => contact.EMailAddress1.EndsWith("@example.com"));
            foreach (var contact in exampleContacts)
                Console.WriteLine(contact.FullName);
        }
    }
}

另外,我使用的是由CRM SDK中的CrmSvcUtil工具生成的Xrm.cs文件和App.config,如下所示。

<!--App.config-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
  </configSections>
  <connectionStrings>
    <add name="Xrm" connectionString="Server=http://[MyCompanyName].crm.dynamics.com; Username=[MyUsername]; Password=[MyPassword]"/>
  </connectionStrings>
  <microsoft.xrm.client>
    <contexts default="Xrm">
      <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" connectionStringName="Xrm"/>
    </contexts>
  </microsoft.xrm.client>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

但我得到了这个错误,似乎无法弄明白为什么。此外,我尝试使用Microsoft的源代码,它返回相同的错误:

  

处理此请求时出错。   System.NullReferenceException:未将对象引用设置为对象的实例。      在Microsoft.Xrm.Sdk.Client.ProxyTypesBehavior.System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint,ClientRuntime behavior)      在Microsoft.Xrm.Client.Services.OrganizationService.CreateServiceConfiguration(CrmConnection连接)      在Microsoft.Xrm.Client.Services.OrganizationService.GetServiceConfiguration(CrmConnection连接)      在Microsoft.Xrm.Client.Services.OrganizationService.ToOrganizationServiceProxy(CrmConnection连接)      在Microsoft.Xrm.Client.Services.OrganizationService.ToOrganizationService(CrmConnection连接)      在Microsoft.Xrm.Client.Services.OrganizationService。&lt;&gt; c__DisplayClass2。&lt; .ctor&gt; b__0()      在System.Lazy 1.CreateValue() at System.Lazy 1.LazyInitValue()      在System.Lazy 1.get_Value() at Microsoft.Xrm.Client.Services.OrganizationService.InnerOrganizationService.UsingService[TResult](Func 2行动)      在Microsoft.Xrm.Client.Services.OrganizationService.Execute(OrganizationRequest request)      在Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request)      在Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest请求,IList`1结果)

我错过了什么?有什么我做错了吗?

提前致谢。

编辑:不,“重复”链接没有帮助,因为我无法调试XrmServiceContext.SaveChanges()为了获取缺失/空对象所做的事情。

EDIT2:

我修改了我的控制台应用程序(用于连接测试),现在它返回必须填充凭据,但登录信息已经存在。 如果我去,也会发生同样的事情

IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationSe‌​rvice>(new Uri(url)); 
Sdk.Client.AuthenticationCredentials credentials = new Sdk.Client.AuthenticationCredentials(); 
credentials.ClientCredentials.UserName.UserName = "MyUsername"; 
credentials.ClientCredentials.UserName.Password = "MyPassword"; 
orgServiceManagement.Authenticate(credentials);

0 个答案:

没有答案