Tuesday, September 11, 2012

Unable to construct an object from the class in the batch framework -


 

Hi There!

I hope your week is going ok and with lots of challenges.

As we all know, when we want to run a custom class in the server as a batch job we need to extend the RunBaseBatch class and schedule a job for it.

Today I was working on a class that needed to be run in the server in AX 2012. However, before I scheduled the process, I made the choice to test my class from the client to make sure I did not have any bugs. When the test was successful, I scheduled the job and waited for the batch job status to go from Waiting to Ended, and to my surprise I got an error.

I wasn't sure if I did have another bug in my class that I did not catch, so I just copied the sample batch class Microsoft has here and schedule another job to see if the problem indeed in my code. I got the same error:

"Unable to construct an object from the class Sample Class in the batch framework. Make sure that the X++ code has been compiled to Microsoft .NET Framework CIL, and that the constructor does not require any parameters"

Looking at some documentation, I read that in some cases we need to generate a full CIL compile, but this takes forever. Then a good friend of mine mentioned that a an Incremental CIL will most likely fix the error, an it did.

So, just to be thorough I would like to show the steps on how to do this:

Open a new development environment and click Build and then choose Generate Incremental CIL.



It is going to take about 2-3 minutes (it depends from environment to environment).



Then, to double check if the job run well, go to System Administration > Inquiries > Batch Jobs and look for your job. It status should now be Ended.



That's all for now folks. Until the next time.


 

5 comments:

  1. I had the same error, but the Generate Incremental CIL did not work, But Generate full CIL did work, it took about 20mins.

    ReplyDelete
    Replies
    1. Hi Xier,

      Yeah, the full CIL will definitely fix the error. The incremental worked for me. Thanks for sharing your experience though.

      Delete
  2. Hi Eduardo,

    I had a similar problem, but in my case the RunBaseBatch extended class consumed some .NET objects located in VS Projects inside AX. If I run it in client or even in server mode everything is executed successfully, but if I schedule it in the batch framework it cracks returning to me errors related to object instancing. I generated incremental e full CIL and in both cases I restarted AX service and I got the same error. So I decided to rewrite most of the code to X++ classes leaving in C# only the complex processes, but at this time I got some type conversion errors like "Could not cast type Dynamics.AX.Application.TypeX to MyApp.TypeX" where TypeX is a X++ class imported to my C# project inside AX. Finally I decided to move all code to X++. I am using the SysDictIndex class in my solution and after this refactoring I was still receiving a type conversion error telling me that I could not convert Dynamics.Ax.Application.SysDictIndex to Dynamics.Ax.Application.SysDictIndex. Impressive !! To solve my problem I had to replace all occurrences of SysDictIndex by DictIndex. Do you have any ideia about happened in my case ?

    ReplyDelete
    Replies
    1. Hi Humberto,

      Is your application version and the kernel version the same? It sounds really weird. Is your batch server an AOS server as well? If so, do you have two different accounts for each?

      Excellent post though and let me know.

      Delete
    2. Hi Eduardo,

      No, my app version (6.0.947.862) and kernel version (6.0.947.280) are different. I'm still working with a deployment of a dynamics ax 2012 R1. I live in Brazil and here we are still waiting our localization to be released by regional Microsoft. In regards to batch server I can tell you that I'm working in a stand alone server, so, AOS and batch server resides on same machine with same execution credentials. Thanks for your interest in my case.

      Delete

Thank you for your thoughts. Your comment will appear in my blog shortly after review.

Have a great day!