Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

CrmServiceClient is null Value

Posted on by Microsoft Employee

Hi

Connections using CrmServiceClient sometimes result in null values.

It happens intermittently if you use it continuously, and most of the time, it seems that the connection is not made once an hour.

<add name="ConnectionString"
connectionString="AuthType=ClientSecret; url={myorg}.crm5.dynamics.com; clientid={clientid}; clientsecret={mysecret};"/>

private static IOrganizationService service;

private string _conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

public IOrganizationService CreateOrganizationWebProxyClient()
{
    CrmServiceClient client;
    client = new CrmServiceClient(_conStr);

    service = (IOrganizationService)client.OrganizationWebProxyClient != null ? (IOrganizationService)client.OrganizationWebProxyClient : (IOrganizationService)client.OrganizationServiceProxy;

    return service;
}

The code used is as above.

Sometimes I want to know why the connection doesn't work. 

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CrmServiceClient is null Value

    using (OrganizationWebProxyClient sdkService = new OrganizationWebProxyClient(serviceURL, false))

    Changing false to true enabled it.

    using (OrganizationWebProxyClient sdkService = new OrganizationWebProxyClient(serviceURL, true))

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CrmServiceClient is null Value

    When using the above code

    In Linq of EarlyBound type, an exception that Microsoft.Xrm.Sdk.Entity cannot be cast as a Model of ServiceContext occurs.

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: CrmServiceClient is null Value

    Hi,

    Hope this code helped you. If yes, please mark it as verified so others can use.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CrmServiceClient is null Value

    Let's try that code. Thank you for answer.

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: CrmServiceClient is null Value

    Hi,

    I think this is most common issue and I really don't like this code as I always used to get null value I think This is due to .net Framework version. If you have Tenant Id as well then I will suggest below code to get the service.

    private static IOrganizationService ConnectionService(string url, string clientId, string clientSecret, string tenantId)
    {
    tenantId = "htps://login.microsoftonline.com/" + tenantId;
    Task<string> callTask = Task.Run(() => AccessTokenGenerator(url, clientId, clientSecret, tenantId));
    callTask.Wait();

    string token = callTask.Result;
    Uri serviceURL = new Uri(url + @"/xrmservices/2011/organization.svc/web?SdkClientVersion=9.1");
    using (OrganizationWebProxyClient sdkService = new OrganizationWebProxyClient(serviceURL, false))
    {
    sdkService.HeaderToken = token;
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
    sdkService.InnerChannel.OperationTimeout = new TimeSpan(2, 30, 0);
    IOrganizationService organizationService = (IOrganizationService)sdkService != null ? (IOrganizationService)sdkService : null;
    return organizationService;
    }

    }

    private static async Task<string> AccessTokenGenerator(string url, string ClientId, string clientSecret, string tenantId)
    {
    var credentials = new ClientCredential(ClientId, clientSecret);
    var authContaxt = new AuthenticationContext(tenantId);
    var result = await authContaxt.AcquireTokenAsync(url, credentials);
    return result.AccessToken;
    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CrmServiceClient is null Value

    If the connection doesn't work, do I have to keep trying to connect using IsReady?

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,989 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans