Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Microsoft.Xrm.Tooling.Connector.CrmServiceClient

(0) ShareShare
ReportReport
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
    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
    28,977 Moderator on at
    RE: Microsoft.Xrm.Tooling.Connector.CrmServiceClient

    Hi,

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

  • DKozul Profile Picture
    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
    28,977 Moderator 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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,033 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,854 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans