简体   繁体   English

在C#插件中调用Dynamics Web API

[英]Call Dynamics Web API in C# Plugin

I have a Business Process Flow in Microsoft Dynamics to handle creation of a new client. 我在Microsoft Dynamics中有一个业务流程来处理新客户端的创建。 When the process finishes, I am attaching a workflow that kicks of an action which calls a plugin to do some custom processing. 当该过程完成时,我附加了一个工作流,该工作流可以调用插件来执行一些自定义处理。 I am following this article to set up this process. 我正在按照这篇文章来设置这个过程。

In my plugin, I have a call to the Dynamics Web API (see code below). 在我的插件中,我调用了Dynamics Web API(请参阅下面的代码)。 When I hit the responseMessage = client.GetAsync(url).Result; 当我点击responseMessage = client.GetAsync(url).Result; line, my plugin exits without returning an error. line,我的插件退出而不返回错误。 When I am debugging in the Plugin Registration Tool, the Plugin Registration Tool crashes and has to be restarted. 当我在插件注册工具中调试时,插件注册工具崩溃并且必须重新启动。 When I look at the network traffic, this call appears to be failing with a 401 - Unauthorized error. 当我查看网络流量时,此呼叫似乎失败了401 - Unauthorized错误。

When I try this same HttpClient call from a console app, the call succeeds. 当我从控制台应用程序尝试同样的HttpClient调用时,调用成功。 I've tried a few different credentials for authentication without success. 我已经尝试了一些不同的凭据进行身份验证而没有成功。 I've also tried calling the GetAsync function a few different ways. 我也尝试过几种不同的方式调用GetAsync函数。 Is this error related to the async or authentication methods? 此错误与异步或身份验证方法有关吗? What am I missing here? 我在这里错过了什么?

HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential("admin", "password", "DOMAIN") });
client.BaseAddress = new Uri(Helpers.GetSystemUrl(COHEN.APIConnector.Application.Dynamics));
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
client.DefaultRequestHeaders.Add("OData-Version", "4.0");
HttpResponseMessage responseMessage;
string url = "ccseq_clients";

responseMessage = client.GetAsync(url).Result;

Update 更新

We are using Dynamics 2016 On-Premise. 我们正在使用Dynamics 2016内部部署。

The reason we are using the Web API is that this call occurs in a library we have developed that will interface between multiple systems. 我们使用Web API的原因是这个调用发生在我们开发的库中,它将在多个系统之间进行接口。 The library is used within a plugin, but can also be used outside of a plugin for other applications. 该库在插件中使用,但也可以在插件之外用于其他应用程序。 The code sample we provided is the chunk of code that is failing in the library. 我们提供的代码示例是库中失败的代码块。 We pulled the chunk of code directly into the plugin to see if the error was related more to the library or the plugin. 我们将大量代码直接插入到插件中,以查看错误是否与库或插件有关。

We noticed that the client.GetAsync(url).Result call was actually returning a correct value. 我们注意到client.GetAsync(url).Result调用实际上返回了一个正确的值。 We missed seeing in our network traffic that two calls failed before a third succeeded and returned the correct value (See screenshot below). 我们错过了在我们的网络流量中看到两个呼叫在第三个成功之前失败并返回正确的值(参见下面的屏幕截图)。 The odd thing is that when we debug this line of code, stepping over the line of code causing the Plugin Registration Tool to crash while the network traffic shows we returned the correct values. 奇怪的是,当我们调试这行代码时,踩过代码行导致插件注册工具崩溃,而网络流量显示我们返回了正确的值。 If we set a breakpoint after this line of code, then the Plugin Registration Tool does not crash and we have a correct value in our response. 如果我们在这行代码之后设置一个断点,那么插件注册工具不会崩溃,我们的响应中有一个正确的值。 This behavior changes when we went back to debugging in the library rather than directly in the plugin. 当我们回到库中的调试而不是直接在插件中调试时,这种行为会发生变化。 In the library, the Plugin Registration Tool always crashes when hitting this line regardless of where we set our breakpoints even though the network traffic still shows a successful response. 在库中,即使网络流量仍然显示成功响应,无论我们在何处设置断点,插入此行时插件注册工具始终会崩溃。

网络流量


Update 2 更新2

It would appear that my library is successfully making multiple different calls to the Web API so I'm thinking the issue is more that the Plugin Registration Tool can't handle the Web API call more than the call not actually working. 看来我的库成功地对Web API进行了多次不同的调用,所以我认为问题更多的是插件注册工具不能处理Web API调用而不是实际工作的调用。

网络流量

I believe the issue was with the Plugin Registration Tool profiling a plugin execution. 我相信问题在于插件注册工具分析插件执行。 My follow up question is here . 我的后续问题就在这里

This blog states that we can make Web Api work in CRM on-premise (non-IFD) plugin by using WebClient with DefaultCredentials. 这篇博客指出,我们可以通过使用带有DefaultCredentials的WebClient使Web Api在CRM内部部署(非IFD)插件中工作。

using (WebClient client = new WebClient()) 
{            
    client.UseDefaultCredentials = true;
    byte[] responseBytes = client.DownloadData(new Uri("<your web api url>/accounts(3C2D2712-E43F-E411-9402-005056AB452C)")); 
    string response = Encoding.UTF8.GetString(responseBytes);
    // parse the json response 
}

Make sure you specify the UseDefaultCredentials as true to make the web api call under the context of the user the plugin is running. 确保将UseDefaultCredentials指定为true,以便在插件运行的用户的上下文中进行web api调用。

We may struggle more with Online plugins because of Sandbox + Adal library + AAD tokens combo as we are trying in a non-interactive plugin code unlike other interface where we can challenge the user by prompt. 由于Sandbox + Adal库+ AAD令牌组合,我们可能会更多地使用在线插件,因为我们尝试使用非交互式插件代码,而不像其他界面我们可以通过提示来挑战用户。

Moreover Web api is more useful for cross platform, outside CRM context integration. 此外,Web api对于跨平台,外部CRM上下文集成更有用。 Inside the platform execution you can use Org service to achieve more. 在平台执行中,您可以使用Org服务来实现更多功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM