c# - 从 CRM 2011 插件调用 Web 服务

标签 c# wcf web-services dynamics-crm-2011

我创建了一个调用 AX 自定义 Web 服务的插件。 Web 服务应返回给定产品和客户的价格。

我能够在 CRM 之外毫无问题地调用 Web 服务,但将其包含在插件中后,它停止工作。

我收到的错误消息是:

Could not find default endpoint element that references contract 'AxIntegrationServices.PriceDiscService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

这是代码:

//retrieve the entity product as the input Entity
var entity = (Entity)context.InputParameters["Target"];

//Early bound entity
var oppProduct = new opportunityproduct(entity);
var quantity = (Decimal)oppProduct.quantity;

tracingService.Trace("Retrieving Opp with opp ID = {0}", oppProduct.opportunityid.Id.ToString());

//get the early bound opportunity containing the opportunity product
var opp = new opportunity(Helper.ActualEntity(oppProduct.opportunityid, service));
//get the early bound account entity that is the customer for the opportunity
tracingService.Trace("Retrieved, type = {0}", opp.name);
tracingService.Trace("Retrieving Account with accountID={0}", opp.customerid.Id.ToString());
Entity acc = Helper.ActualEntity(opp.customerid, service);
tracingService.Trace("Account retrieved");
var account = new account(acc);
//get the ax integration key for the account
tracingService.Trace("Retrieving Account AX key");
var accountAxKey = account.custom_axrecordid;
tracingService.Trace("Retrieving Product");
//get the early bound account entity that is the customer for the opportunity
var product = new product(Helper.ActualEntity(oppProduct.productid, service, new string[]{ "custom_axrecordid" }));
//get the integration key for the product
tracingService.Trace("Retrieving Product AX key");
var productAxKey = product.custom_axrecordid;

tracingService.Trace("Invoking web service");
PriceDiscServiceClient priceDiscServiceClient = new PriceDiscServiceClient();

CallContext callContext = new CallContext();

priceDiscServiceClient.ClientCredentials.Windows.ClientCredential.UserName = "xxx";

priceDiscServiceClient.ClientCredentials.Windows.ClientCredential.Password = "yyyy!";

priceDiscServiceClient.ClientCredentials.Windows.ClientCredential.Domain = "aaa"; 


PriceDiscServiceContract priceDiscServiceContract = priceDiscServiceClient.getPriceDiscSales(callContext, productAxKey, accountAxKey, quantity);

tracingService.Trace("Price :{0}",priceDiscServiceContract.Price);
tracingService.Trace("Markup :{0}", priceDiscServiceContract.Markup);
tracingService.Trace("PriceUnit :{0}", priceDiscServiceContract.PriceUnit);
tracingService.Trace("DiscAmount :{0}", priceDiscServiceContract.DiscAmount);
tracingService.Trace("DiscPct :{0}", priceDiscServiceContract.DiscPct);

oppProduct.priceperunit = priceDiscServiceContract.PriceUnit;



oppProduct.isproductoverridden = false;
oppProduct.ispriceoverridden = true;

Web 服务位于 CRM 环境的同一网络中,我正在通过 VPN 连接到它们。

有什么想法吗?

最佳答案

您应该检查您的 PriceDiscServiceClient 构造函数 - 它应该接受 BindingEndpointAddress,以便您的代码看起来像这样:

 //...
 BasicHttpBinding binding = new BasicHttpBinding();

 // configure Binding as needed (Timeout, etc.) ...

 EndpointAddress endpoint = new EndpointAddress(endpointUri);
 PriceDiscServiceClient client = new PriceDiscServiceClient(binding, endpoint);
 //...

正如 James Wood 已经指出的那样,下一个问题将是使用可配置值填充 endpointUri,而不是将其硬编码到插件中。

我倾向于使用插件不安全配置,而不是每次插件执行时往返 crm 设置记录。

link James Wood refers to这正是我选择配置端点地址 Uri 的解决方案。

关于c# - 从 CRM 2011 插件调用 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21260770/

相关文章:

ajax - 如何将基于 Spring + Hibernate 的应用程序后端与纯 HTML 和基于 AJAX 的客户端连接?

c# - 在着色器中使用来自 c# 脚本的函数

c# - 如何在mvc中从另一个 Controller 调用一个 Controller 中编写的函数

WCF RIA 服务授权

c# - WCF服务将数据集与数据类作为返回类型

java - 从 JAX-WS 异步 Web 服务获取 HTTP 响应代码

c# - ClickOnce 发送参数不起作用

c# - 使用 Unity/Prism/MVVM 的 ObjectContext 构造函数注入(inject)

c# - 使用 Ajax 请求使用 Wcf 服务时配置文件错误

java - 如何在本地主机上启动 cxf 服务但在 wsdl 中返回外部地址?