我使用 Visual Studio 使用了 Web 服务,并使用托管代码在 AX 2012 中调用该服务。 现在,如果我在一个简单的工作中运行代码:
static void CurrencyService(Args _args)
{
CurrencyConvert.Currency_Convert.CurrencyServiceClient convertcurrency;
CurrencyConvert.Currency_Convert.Currency currency;
System.ServiceModel.Description.ServiceEndpoint endPoint;
System.Type type;
System.Exception ex;
str s1;
try
{
type = CLRInterop::getType('CurrencyConvert.Currency_Convert.CurrencyServiceClient');
convertcurrency = AifUtil::createServiceClient(type);
endPoint = convertcurrency.get_Endpoint();
// endPoint.set_Address(new System.ServiceModel.EndpointAddress("http://localhost/HelloWorld"));
currency = convertcurrency.GetConversionRate(CurrencyConvert.Currency_Convert.CurrencyCode::AUD,CurrencyConvert.Currency_Convert.CurrencyCode::INR );
info(strFmt('%1', CLRInterop::getAnyTypeForObject(currency.get_Rate())));
}
catch(Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(CLRInterop::getAnyTypeForObject(ex.ToString()));
}
}
上述工作运行良好,并在信息日志中产生结果。
现在,如果像我们通常为任何批处理作业所做的那样,在批处理作业的类(扩展 Runbasebatch 类)下编写相同的代码,则会抛出错误:
Microsoft.Dynamics.Ax.Xpp.ErrorException: Exception of type 'Microsoft.Dynamics.Ax.Xpp.ErrorException' was thrown.
at Dynamics.Ax.Application.BatchRun.runJobStatic(Int64 batchId) in BatchRun.runJobStatic.xpp:line 38
at BatchRun::runJobStatic(Object[] )
at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters)
at BatchIL.taskThreadEntry(Object threadArg)
除所使用的 Web 服务之外的其他批处理作业均运行正常。 我已经尝试过很多事情,例如:将类的 RunOn 属性设置为“服务器”等。 我们使用的每个 Web 服务都是如此。 有人对此有适当的解决方案吗?
最佳答案
我假设这与 Dynamics Axe 社区网站帖子中的内容相同。因此,阅读此处,错误与批处理无关,而是与以下内容相关:“在 ServiceModel 客户端配置部分中找不到引用合约“Currency_Convert.ICurrencyService”的默认端点元素。
这是因为正在 AX32.exe.config 文件中搜索端点,而这不是您需要的端点。您需要从与 DLL 关联的配置文件中获取它。
为此,您需要在 AX 中以不同的方式构建客户端。您需要使用 AIF 实用程序,因为这样才能使用正确的配置。示例:
type= CLRInterop::getType('DynamicsAxServices.WebServices.ZipCode.USAZipCodeServiceRef.PostalCodeServiceClient');
postalServiceClient = AifUtil::createServiceClient(type);
除此之外,还有一个额外的目的。不同的环境需要不同的 URL,这可以通过手动指定端点地址并让它使用系统参数来解决。 (这样你就可以为DEV/TEST/PROD指定不同的配置)(注意:下面的端点地址是硬编码的,应该是一个参数)
static void Consume_GetZipCodePlaceNameWithEndPoint(Args _args)
{
DynamicsAxServices.WebServices.ZipCode.USAZipCodeServiceRef.PostalCodeServiceClient postalServiceClient;
DynamicsAxServices.WebServices.ZipCode.USAZipCodeServiceRef.PostalCodepostalCode;
System.ServiceModel.Description.ServiceEndpointendPoint;
System.ServiceModel.EndpointAddressendPointAddress;
System.Exceptionexception;
System.Typetype;
;
try
{
// Get the .NET type of the client proxy
type = CLRInterop::getType('DynamicsAxServices.WebServices.ZipCode.USAZipCodeServiceRef.PostalCodeServiceClient');
// Let AifUtil create the proxy client because it uses the VSAssemblies path for the config file
postalServiceClient = AifUtil::createServiceClient(type);
// Create and endpoint address, This should be a parameter stored in the system
endPointAddress = new System.ServiceModel.EndpointAddress ("http://www.restfulwebservices.net/wcf/USAZipCodeService.svc");
// Get the WCF endpoint
endPoint = postalServiceClient.get_Endpoint();
// Set the endpoint address.
endPoint.set_Address(endPointAddress);
// Use the zipcode to find a place name
postalCode = postalServiceClient. GetPostCodeDetailByPostCode("10001"); // 10001 is New York
// Use the getAnyTypeForObject to marshal the System.String to an Ax anyType
// so that it can be used with info()
info(strFmt('%1', CLRInterop::getAnyTypeForObject(postalCode.get_ PlaceName())));
}
catch(Exception::CLRError)
{
// Get the .NET Type Exception
exception = CLRInterop::getLastException();
// Go through the inner exceptions
while(exception)
{
// Print the exception to the infolog
info(CLRInterop::getAnyTypeForObject(exception.ToString()));
// Get the inner exception for more details
exception = exception.get_InnerException();
}
}
}
关于batch-processing - 使用批处理作业运行 Web 服务时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20472503/