Issue while running web service using batch job in AX 2012?

I have consumed a web service using visual studio and used managed code to call that in AX 2012.

Now if I running the code in a simple job as:

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("">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()));

}

}

Above job is working fine and producing results in a infolog.

Now, if a same piece of code is written under a class for batchjob(extending Runbasebatch class) as we normally do for any batch job, it is throwing an error as:

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)

Other batch jobs except which used web services are working properly.

I have already tried many things such as : RunOn property of a class is set as “server” etc.

This is the case with each web service we have consumed.

Does anybody have a proper solution for this??

If you can’t find the error message in any other way, debug the batch to get it. Obviously, it’s important to know what failed.

Does your code work if you execute it on server in an interactive session? If not, the problem is not related to batch but to the machine where the code executes. The obvious suspect is the address - http://localhost/HelloWorld may exist on client and not on server. Other issues may be caused by permissions for the AOS service account.

Thanks for replying @martin, the line in above code “http://localhost/HelloWorld” is actually not in use and yes the code works perfectly on client on the system where AOS is installed. The issue is with other client systems.

The same issue occurs for every webService.

Same code is working perfectly when not executing from batch jobs.

How to check permissions for AOS Service account??

How to debug batch job??

What “the issue is with other client systems” exactly means?

Debugging in Microsoft Dynamics AX 2012 - Batch Jobs

I mean on the other systems in domain which are using AOS installed on server system…

I mean on the systems in domain which are using AOS installed on server system…

In other words, the problem is not related to batch, because it occurs even in interactive sessions. The problem is that the functionality works only on the machine with AOS. Correct?

I am not sure.

But functionality works for both AOS and non AOS machines if batch process is not involved to execute code.

One thing more, I have created batch process on non AOS machine. Is this related to ma issue??

I’m completely lost. I don’t know what you do and where and what results you get in different cases.

I am sorry for that. Let me try to explain it again step by step.

Objective: To consume a webservice in AX 2012 to get Conversion rate of a currency.

Machine: AOS is installed on separate server and i am using a system in a domain where only AX 2012 client is installed using a AOS instance available on server.

I am using a machine where only client is installed. I have full admin rights available.

Steps:

  1. As mentioned on following link:(I have a different services)

http://mukesh-ax.blogspot.in/2011/10/consuming-web-services-in-ax-2012.html

  1. Above code is working both on server machine and on my system.

  2. Now, my task is to create a batch job to run code mentioned in above code at regular time interval.

  3. I have created a class extending “RunbaseBatch” class and set runOn property of a class as “Server”.

  4. Put above code under Run method of a class.

  5. Now, I have created a new batch job using batch job form under system Administration.

  6. Under tasks of newly created job , added class created above.

  7. Set Recurrence property of a job and changed the status of a job to “Waiting”.

  8. When job runs it throws an error(diferent errors everytime with different exceptions) as:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.InvalidOperationException: Could not find default endpoint element that references contract ‘Currency_Convert.ICurrencyService’ 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.

at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)

at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)

at System.ServiceModel.ChannelFactory`1…ctor(String endpointConfigurationName, EndpointAddress remoteAddress)

at System.ServiceModel.EndpointTrait`1.CreateSimplexFactory()

at System.ServiceModel.ClientBase1.CreateChannelFactoryRef(EndpointTrait1 endpointTrait)

at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef()

at CurrencyConvert.Currency_Convert.CurrencyServiceClient…ctor()

at Dynamics.Ax.Application.dev_CurrencyRateCheck.Run() in dev_CurrencyRateCheck.run.xpp:line 9

— End of inner exception stack trace —

  1. The piece of code in a class is as follows:

try

{

type = CLRInterop::getType(‘CurrencyConvert.Currency_Convert.CurrencyServiceClient’);

convertcurrency = AifUtil::createServiceClient(type);

// convertcurrency = new CurrencyConvert.Currency_Convert.CurrencyServiceClient();

endPoint = convertcurrency.get_Endpoint();

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()));

}

  1. Same code is working perfectly if running in a job.

I hope, this will make you understand my actual issue.

I am sure the issue is not with the web service. Since the same issue occurs for every web service that we are having.

May be i am missing something that stops service to be working from batch jobs.

Great! We finally know the error message: “Could not find default endpoint element that references contract ‘Currency_Convert.ICurrencyService’ 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.”

Now tell me whether the code works if you execute it on server and not just in a job on client.

I have resolve this issue. I just end session all online user and stop/start AOS after doing ful cil. Maybe deleting XPPIL and Appl files helps before start the AOS service.