I am following the Build a console app that connects to CRM guide, and when I try to run it, it throws a NullReferenceException
on SaveChanges()
. (See bottom of the post for edits)
Here's the code:
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);
}
}
}
Also, I am using a Xrm.cs file generated by the CrmSvcUtil tool from the CRM SDK, and an App.config, as follows.
<!--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>
But I get this error and can't seem to figure out why. Also, I tried it out with Microsoft's source code and it returns the same error:
An error occured while processing this request. System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Xrm.Sdk.Client.ProxyTypesBehavior.System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior) at Microsoft.Xrm.Client.Services.OrganizationService.CreateServiceConfiguration(CrmConnection connection) at Microsoft.Xrm.Client.Services.OrganizationService.GetServiceConfiguration(CrmConnection connection) at Microsoft.Xrm.Client.Services.OrganizationService.ToOrganizationServiceProxy(CrmConnection connection) at Microsoft.Xrm.Client.Services.OrganizationService.ToOrganizationService(CrmConnection connection) at Microsoft.Xrm.Client.Services.OrganizationService.<>c__DisplayClass2.<.ctor>b__0() at System.Lazy
1.CreateValue() at System.Lazy
1.LazyInitValue() at System.Lazy1.get_Value() at Microsoft.Xrm.Client.Services.OrganizationService.InnerOrganizationService.UsingService[TResult](Func
2 action) at Microsoft.Xrm.Client.Services.OrganizationService.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest request, IList`1 results)
What am I missing? Is there something I'm doing wrong?
Thanks in advance.
EDIT: No, the "Duplicate" link does not help, since I cannot debug what XrmServiceContext.SaveChanges()
does in order to get to the missing/null object.
EDIT2:
I modified my console app (for connnection testing) and now it returns that the credentials must be populated, but the login info is already there. Same happens if I go
IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(url));
Sdk.Client.AuthenticationCredentials credentials = new Sdk.Client.AuthenticationCredentials();
credentials.ClientCredentials.UserName.UserName = "MyUsername";
credentials.ClientCredentials.UserName.Password = "MyPassword";
orgServiceManagement.Authenticate(credentials);