在C#插件中调用Dynamics Web API [英] Call Dynamics Web API in C# Plugin

查看:151
本文介绍了在C#插件中调用Dynamics Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Microsoft Dynamics中有一个业务流程来处理新客户端的创建.该过程完成后,我将附加一个工作流,该工作流将执行一个操作,该操作将调用插件来执行一些自定义处理.我正在关注文章以设置此过程.

I have a Business Process Flow in Microsoft Dynamics to handle creation of a new client. 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.

在我的插件中,我调用了Dynamics Web API(请参见下面的代码).当我点击responseMessage = client.GetAsync(url).Result;行时,我的插件退出而没有返回错误.当我在插件注册工具中调试时,插件注册工具崩溃,必须重新启动.当我查看网络流量时,此呼叫似乎失败,并显示401 - Unauthorized错误.

In my plugin, I have a call to the Dynamics Web API (see code below). When I hit the responseMessage = client.GetAsync(url).Result; line, my plugin exits without returning an error. 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.

当我从控制台应用程序尝试相同的HttpClient调用时,该调用成功.我尝试了几种不同的身份验证凭据,但均未成功.我还尝试了几种不同的方式调用GetAsync函数.此错误与异步或身份验证方法有关吗?我在这里想念什么?

When I try this same HttpClient call from a console app, the call succeeds. I've tried a few different credentials for authentication without success. I've also tried calling the GetAsync function a few different ways. 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;


更新

我们正在使用Dynamics 2016内部部署.


Update

We are using Dynamics 2016 On-Premise.

我们使用Web API的原因是此调用发生在我们开发的库中,该库将在多个系统之间进行接口.该库在插件内使用,但也可以在插件外用于其他应用程序.我们提供的代码示例是库中失败的代码块.我们直接将代码块提取到插件中,以查看错误是否与库或插件有关.

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. 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.

我们注意到client.GetAsync(url).Result调用实际上返回了正确的值.我们错过了在网络流量中没有看到两次呼叫失败而第三次呼叫成功并返回正确值的信息(请参见下面的屏幕截图).奇怪的是,当我们调试该行代码时,越过该行代码,导致插件注册工具崩溃,而网络流量显示我们返回了正确的值.如果我们在此代码行之后设置断点,则插件注册工具不会崩溃,并且响应中的值正确.当我们回到库中而不是直接在插件中进行调试时,此行为会更改.在该库中,无论网络流量仍然显示成功响应如何,无论我们在何处设置断点,只要碰到此行,插件注册工具就会始终崩溃.

We noticed that the client.GetAsync(url).Result call was actually returning a correct value. 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.

看来我的库正在成功地对Web API进行多个不同的调用,所以我认为问题更多是插件注册工具不能处理Web API调用的程度超过了无法实际使用的调用的程度.

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.

推荐答案

我认为问题出在插件注册工具"对插件执行进行性能分析.我的后续问题是此处

I believe the issue was with the Plugin Registration Tool profiling a plugin execution. My follow up question is here.

这篇关于在C#插件中调用Dynamics Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆