问题描述
我正在尝试使用以下代码从Dynamics 365 Online插件访问外部第三方API:
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in plug-in debugging.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
tracingService.Trace("Downloading the target URI: " + webAddress);
try
{
//<snippetWebClientPlugin2>
// Download the target URI using a Web client. Any .NET class that uses the
// HTTP or HTTPS protocols and a DNS lookup should work.
using (WebClient client = new WebClient())
{
byte[] responseBytes = client.DownloadData(webAddress);
string response = Encoding.UTF8.GetString(responseBytes);
//</snippetWebClientPlugin2>
tracingService.Trace(response);
// For demonstration purposes,throw an exception so that the response
// is shown in the trace dialog of the Microsoft Dynamics CRM user interface.
throw new InvalidpluginExecutionException("WebClientPlugin completed successfully.");
}
}
catch (WebException exception)
{
string str = string.Empty;
if (exception.Response != null)
{
using (StreamReader reader =
new StreamReader(exception.Response.GetResponseStream()))
{
str = reader.ReadToEnd();
}
exception.Response.Close();
}
if (exception.Status == WebExceptionStatus.Timeout)
{
throw new InvalidpluginExecutionException(
"The timeout elapsed while attempting to issue the request.",exception);
}
throw new InvalidpluginExecutionException(String.Format(CultureInfo.InvariantCulture,"A Web exception occurred while attempting to issue the request. {0}: {1}",exception.Message,str),exception);
}
}
catch (Exception e)
{
tracingService.Trace("Exception: {0}",e.ToString());
throw;
}
}
}
但是我得到了错误:
Request for the permission of type 'System.Security.Permissions.SecurityPermission,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089' Failed.'
我已经检查了MS文档,但没有任何内容说明为什么我无法做到这一点。我了解沙盒插件,但根据微软的说法,我应该能够使用其自己的示例代码来做到这一点。
解决方法
这在CRM Online中是预期的,因为这是SaaS,并且您在云中的共享租户中。您可以执行webhook或Azure服务中心来触发带有CRM上下文的外部终结点进行处理。 Read more
如果您已经有了CRM Online,那么通常的解决方案是将处理工作转移到您可以更好地控制的环境中。最常见的选择是使用Azure Service Bus or Azure Event Hub将处理工作卸载到Azure。 CRM 9的新功能是将数据发送到WebHook,该数据可以托管在任何位置。