Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Microsoft.Xrm.Tooling.Connector.CrmServiceClient

Posted on by 20

Hi,

I am writing a code to connect to CRM and using Microsoft.Xrm.Tooling.Connector.CrmServiceClient. I am trying to connect to OnPremise CRM with this piece of code:

public CrmServiceClient GetOrganizationService()
{
    CrmServiceClient.MaxConnectionTimeout = _timeout;

    CrmServiceClient svc = new CrmServiceClient(_connectionString);

    if (svc.IsReady)
    {
        return svc;
    }
    else
    {
        throw new Exception(svc.LastCrmError);
    }
}

I am using this method inside using statement like so:

using (var service = _crmEngine.GetOrganizationService())
{
    var query = ...

    var result = service.RetrieveMultiple(query);

    return result;
}

This code is used in ASP.NET web application and class with first method is registered as Singleton.

When I run code I get this error:

Cannot access a disposed object.
Object name: 'System.ServiceModel.Security.TransportSecurityProtocol'.

Stack trace:

at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.RetrieveMultiple(QueryBase query)
at AbsenceCalendar.Security.AbsenceSecurityPermissionRepository.GetSecurityPermissionsForUser(Guid Id) in C:\MyProjects\AbsenceCalendar\AbsenceCalendar.Security\AbsenceSecurityPermissionRepository.cs:line 53
at AbsenceCalendar.Security.AbsenceSecurityService.GetSecurityPermissionsForUser(Guid userId) in C:\MyProjects\AbsenceCalendar\AbsenceCalendar.Security\AbsenceSecurityService.cs:line 21
at RealEstateInCloud.Security.SecurityService`2.<>c__DisplayClass7_0.<GetUserPermission>b__0(TKey p)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at RealEstateInCloud.Security.SecurityService`2.GetUserPermission(TKey userId, PermissionAction permissionAction, String entityName)
at RealEstateInCloud.Security.SecurityEngine`2.GetPermissionForEntity[TEntity(PermissionAction permissionAction)
at AbsenceCalendar.Common.ListController`2.OwnList(PagingMetadata pagingMetadata) in C:\MyProjects\AbsenceCalendar\AbsenceCalendar\Common\ListController.cs:line 31
at AbsenceCalendar.Controllers.AbsenceItemController.OwnList(PagingMetadata pagingMetadata) in C:\MyProjects\AbsenceCalendar\AbsenceCalendar\Controllers\AbsenceItemController.cs:line 47
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[ arguments)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()

I searched this on web but I didn't find solution how to use Microsoft.Xrm.Tooling.Connector.CrmServiceClient in Web applications in proper way.

Please help!.

  • DKozul Profile Picture
    DKozul 20 on at
    RE: Microsoft.Xrm.Tooling.Connector.CrmServiceClient

    I tried and it works, but I needed to refactor a lot of my previous code.

    We will see long term if removing using statements was smart...

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Microsoft.Xrm.Tooling.Connector.CrmServiceClient

    Hi,

    Can you please try without using statement to see if it works.

  • DKozul Profile Picture
    DKozul 20 on at
    RE: Microsoft.Xrm.Tooling.Connector.CrmServiceClient

    Here is the connection string:

    Url=crmtest.***************;AuthType=AD;Domain=*****;UserName=*****;Password=*****;RequireNewInstance=False;

    And yes I am using latest nuget:

    pastedimage1661841176212v1.png

    Maybe instead of this piece of code:

     if (svc.IsReady)
    {
        return svc;
    }

    I should put:

    if (svc.IsReady)
    {
        return svc.OrganizationWebProxyClient ?? svc.OrganizationServiceProxy as IOrganizationService;
    }

    But then i can't use using statement since IOrganizationService is not disposable.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Microsoft.Xrm.Tooling.Connector.CrmServiceClient

    Hi,

    Could you please share the connection string you used in your code.

    Also I hope you are using latest nuget packages

    www.nuget.org/.../

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans