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 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...
Hi,
Can you please try without using statement to see if it works.
Here is the connection string:
Url=crmtest.***************;AuthType=AD;Domain=*****;UserName=*****;Password=*****;RequireNewInstance=False;
And yes I am using latest nuget:
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.
Hi,
Could you please share the connection string you used in your code.
Also I hope you are using latest nuget packages
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156