'Unable to cast - 'CDSRuntime.SandboxWorker.SandboxOrganizationServiceWrapper' to type ..WebProxyClient<Microsoft.Xrm.Sdk.IOrganizationService

I am trying to get access token from IOrganizationService inside custom workflow.

Below is the logic I'm using to retrieve IOrganisationService in custom workflow:

IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
_organizationService = serviceFactory.CreateOrganizationService(context.UserId);

I have tried getting Token through OrganizationServiceProxy by below code:

var proxy = (OrganizationServiceProxy) _organizationService;
var token = proxy.SecurityTokenResponse.Token;

which is also resulting in the same error.

What is the possible way here to get access token in this custom workflow?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source