Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
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,960 Super User 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,960 Super User 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/.../

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,768 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,985 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans