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 :
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!.

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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/.../

  • DKozul Profile Picture
    20 on at

    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,983 Moderator on at

    Hi,

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

  • DKozul Profile Picture
    20 on at

    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...

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 46 Most Valuable Professional

#2
Pallavi Phade Profile Picture

Pallavi Phade 33

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 28 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans