We have a dynamics applications where we have integrated a Asp.net Web API to accomplish our custom business logic for Order and Quote entity. Currently the application uses user account and password credentials for authentication with Dataverse. But recently this authentication has been deprecated and that's why getting error from the Web API.
To resolve this, we have started to move our authentication procedure to OAuth. But we are getting a Object reference not set to an instance of an object.
Previous Code:
public sealed class ConnectionManager
{
readonly OrganizationServiceProxy proxy;
readonly OrganizationContext context;
public ConnectionManager(ICrmConfiguration c)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var crmCredentials = new ClientCredentials();
crmCredentials.UserName.UserName = c.UserName;
crmCredentials.UserName.Password = c.UserPassword;
proxy = new OrganizationServiceProxy(new Uri(c.ServiceUri), null, crmCredentials, null)
{
Timeout = new TimeSpan(hours: 0, minutes: 5, seconds: 0)
};
proxy.EnableProxyTypes();
context = new OrganizationContext(proxy)
{
MergeOption = MergeOption.PreserveChanges
};
}
}
OrganizationContext is generated from early bound generator tool where the code snippets is like below:
[System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "9.0.0.9154")]
public partial class OrganizationContext : Microsoft.Xrm.Sdk.Client.OrganizationServiceContext
{
/// <summary>
/// Constructor.
/// </summary>
public OrganizationContext(Microsoft.Xrm.Sdk.IOrganizationService service) : base(service)
{
}
/// <summary>
/// Gets a binding to the set of all <see cref="CRMProxy.Model.Account"/> entities.
/// </summary>
public System.Linq.IQueryable<CRMProxy.Model.Account> AccountSet
{
get
{
return this.CreateQuery<CRMProxy.Model.Account>();
}
}
..
...
}
Current Code: To resolve the issue, we have changed ConnectionManager class like the following.
public sealed class ConnectionManager
{
readonly OrganizationContext context;
public ConnectionManager(ICrmConfiguration c)
{
var connectionString = "AuthType=OAuth;Username=xxx@xxxx.onmicrosoft.com;Password=xxxxx;Url=">xxxxx.crm.dynamics.com/.../58145B91-0C36-4500-8554-080854F2AC97;LoginPrompt=Auto";
CrmServiceClient crmConn = new CrmServiceClient(connectionString);
var context= new OrganizationContext(crmConn);
var p = context.SalesOrderSet.ToList(); // Getting Object reference not set to an instance of an object.
}
}
AppID and RedirectUri are default one from the documentation. After changing the connection procedure, when we are retrieving data it is giving a Object reference not set to an instance of an object error. Maybe I am missing something. So we need your kind help.
Note: If am I posting it in the wrong forum, then please guide me to the exact one.
References:
1. brianilland.me/.../