Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
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?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans