我创建了一个调用 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
构造函数 - 它应该接受 Binding
和 EndpointAddress
,以便您的代码看起来像这样:
//...
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/