web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

CrmServiceClient is null Value

(0) ShareShare
ReportReport
Posted on by

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. 

I have the same question (0)
  • Community Member Profile Picture
    on at

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

  • Suggested answer
    shivaram Profile Picture
    3,315 on at

    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
    on at

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

  • shivaram Profile Picture
    3,315 on at

    Hi,

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

  • Community Member Profile Picture
    on at

    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.

  • Community Member Profile Picture
    on at

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

    Changing false to true enabled it.

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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans