我正在尝试使用HttpClient.PostAsync将json发送到Web API.它适用于控制台应用程序,但不适用于我的CRM插件.做一些研究我注意到它可能与插件运行和线程化的上下文有关.无论如何这里是我的调用代码:
public async void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target"))
{
if (context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "new_product")
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
if (entity.Contains("new_begindate") && entity.Contains("new_expirationdate"))
{
await OnlineSignUp(entity, service);
}
}
catch (InvalidPluginExecutionException)
{
throw;
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(OperationStatus.Failed, "Error signing up: " + e.Message);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是发送json的相关代码:
private async Task<HttpResponseMessage> OnlineSignUp(Entity license, IOrganizationService service) …
Run Code Online (Sandbox Code Playgroud)