web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

The expected type was str, but the encountered type was class.

(0) ShareShare
ReportReport
Posted on by 535

I'm unsure of why it thinks a list is of the type class. I have a windows service in which I am trying to get back 3 pieces of information that were packed in a list.

Error:, "The expected type was str, but the encountered type was class.

Windows Code:

ServiceTestApp.LoftwareLabelService;
            List<String>[] returnMessage = new List<String>[3];

            DataSet dataSet;
            //Paging paging = null;

            AifSos.LoftwareLabelInfoServiceClient proxy = new AifSos.LoftwareLabelInfoServiceClient();

            proxy.ClientCredentials.Windows.ClientCredential.UserName = "svc_axaos";   //kmundt   //svc_axbcproxy
            proxy.ClientCredentials.Windows.ClientCredential.Password = "mc@C0nn3ct";   //sears

            AifSos.CallContext LabelContext2;

            LabelContext2 = new AifSos.CallContext();

            LabelContext2.Company= "sd";


            try
            {
                returnMessage = proxy.GetLabelData(LabelContext2);

                previewTxtBx.Text = previewTxtBx.Text + returnMessage.ToString();

            }
            catch (Exception f)
            {

                previewTxtBx.Text = "Error getLabelInfo - (LabelService) did not run correctly: " + f.Message + " - " + System.DateTime.UtcNow + "\n Inner Exception: " + f.InnerException + f.Source.ToString() + "\nStack Strace: " + f.StackTrace.ToString() + " \nData: " + f.Data;
            }


AX Code:

#define.Description("Windows Label Record Retrievable")

    LLContract                  labelInfo;
    SMCLabelsLoftware           labelsloftware;
    SMCDefsLoftwareLabelsDtl    LabelsDtl;
    SysDictTable                sysDictTable;
    str                         fieldname;
    int                         i;
    Struct                      finalstruct, labelsStruct = new Struct(Types::String, 'FieldID', Types::String,'Value', Types::String, 'labelname' );
    List                        finallist, labelsList = new List(Types::String);
    ListEnumerator              listEnum;

    try
    {
        ttsbegin;

                while select labelsloftware where labelsloftware.PrintedStatus == ''
                {
                    sysDictTable = new SysDictTable(tableNum(SMClabelsLoftware));

                    for(i=1;i<= sysDictTable.fieldCnt();i++)
                     {
                         fieldname = SysDictTable.fieldName(sysDictTable.fieldCnt2Id(i));
                         if(SMCDefsLoftwareLabelsDtl::find(labelsloftware.LabelName,SysDictTable.fieldName(sysDictTable.fieldCnt2Id(i))))
                         {
                             labelsStruct.value('FieldID',fieldName);
                             labelsStruct.value('Value',labelsloftware.(sysDictTable.fieldCnt2Id(i)));
                             labelsStruct.value('labelname',labelsloftware.LabelName);

                             //labelInfo.fieldID(fieldName);
                             //labelInfo.Value(labelsloftware.(sysDictTable.fieldCnt2Id(i)));
                             //labelInfo.labelname(labelsloftware.LabelName);

                             //print finalstruct.value('FieldID'),  finalstruct.value('Value'), finalstruct.value('labelname');
                             //pause;

                             labelsStruct.pack();
                             labelslist.addEnd(labelsStruct);
                         }
                     }
                }

        }
        ttscommit;
        return labelsList;
    }
    catch
    {
        error("Error!");
        return labelsList;
    }

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,874 Most Valuable Professional on at

    First of all, please tell us which line is throwing the error.

  • KeithM Profile Picture
    535 on at

    It is giving me a very generic error(below) as in before on the call when I run the service. I enabled error logging on the service and found it giving me the error: The expected type was str, but the encountered type was class.

    line in the service: returnMessage = proxy.GetLabelData(LabelContext2);

     

     

    Windows Error: 

     

    Error getLabelInfo - (LabelService) did not run correctly: Request Failed. See the Exception Log for details. - 8/15/2016 3:19:24 PM
    Inner Exception: mscorlib
    Stack Strace:
    Server stack trace:
    at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

    Exception rethrown at [0]:
    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
    at ServiceTestApp.LoftwareLabelService.LoftwareLabelInfoService.GetLabelData(LoftwareLabelInfoServiceGetLabelDataRequest request)
    at ServiceTestApp.LoftwareLabelService.LoftwareLabelInfoServiceClient.ServiceTestApp.LoftwareLabelService.LoftwareLabelInfoService.GetLabelData(LoftwareLabelInfoServiceGetLabelDataRequest request) in c:\Keith_Documents\SolutionFilesDotNet2013\ServiceTestApp\ServiceTestApp\Service References\LoftwareLabelService\Reference.cs:line 3256
    at ServiceTestApp.LoftwareLabelService.LoftwareLabelInfoServiceClient.GetLabelData(CallContext CallContext) in c:\Keith_Documents\SolutionFilesDotNet2013\ServiceTestApp\ServiceTestApp\Service References\LoftwareLabelService\Reference.cs:line 3262
    at ServiceTestApp.Form1.button1_Click(Object sender, EventArgs e) in c:\Keith_Documents\SolutionFilesDotNet2013\ServiceTestApp\ServiceTestApp\Form1.cs:line 78
    Data: System.Collections.ListDictionaryInternal

     

  • Suggested answer
    Martin Dráb Profile Picture
    237,874 Most Valuable Professional on at

    It means that you have a bug in your X++ code, therefore you should used the debugger to see what's wrong there.

    Nevertheless I think I see it even without debugger. You're trying to assign a structure to a list of strings (labelslist.addEnd(labelsStruct)), which is clearly wrong. The types aren't compatible.

    By the way, what's the point of using the structure? It looks suspicious. Aren't you trying to return a list of class instances? AX can do that; you just have to use a data contract class.

  • KeithM Profile Picture
    535 on at

    I took this over from another developer. She wrote the AX code, so I am trying to dicipher as to why she wrote it this way.

    I wrote a data contract that I thought would be alright. I haven't implemented it yet. I'm unsure of how to go about changing it.

    [DataContractAttribute]
    Public class LLContract
    {
       str FieldID;
       str Value;
       str labelname;
    
    }


    Do I just need to change my return type to Class? And do I not pack the information any more?
    Struct                      finalstruct, labelsStruct = new Struct(Types::String, 'FieldID', Types::String,'Value', Types::String, 'labelname' );
    List                        finallist, labelsList = new List(Types::Class);
    ListEnumerator              listEnum;
    


    [SysEntryPointAttribute(true),
    AifCollectionTypeAttribute('return',Types::Class)]
    public List GetLabelData()
    {
    
    labelsStruct.pack();
    labelslist.addEnd(labelsStruct);
    
  • Suggested answer
    Alex Kwitny Profile Picture
    395 on at

    I would say yes. What your code is showing is a common pattern.

    labelsStruct.pack(); doesn't do anything the way it's written as it is designed to return a "packed" container object.

    container c = labelsStruct.pack(); // Then you can pass/store "c" if you wanted

    So you can probably remove that line.

    labelsStruct is a class, so labelsList.addEnd([string]) is expecting a string, which is most likely incorrect and you should change to Class.

    Look at \Classes\TrvExpenseLineCustomService\find as an example.

    Here is the MSDN about it: https://technet.microsoft.com/en-us/library/hh509052.aspx

  • KeithM Profile Picture
    535 on at

    Yeah, I went that route, but unsure of of how to receive that on the .NET side.  It's asking for a array of the class. when I code it.  It returns me basic text: Field ID: ServiceTestApp.LoftwareLabelService.LLContract[]

    AifSos.LLContract[] returnMessage = new AifSos.LLContract[0];

    try
                {
    
                    returnMessage = proxy.GetLabelData(LabelContext2);
                    previewTxtBx.Text = "Field ID: " + returnMessage + "\n";
    
                }
  • Martin Dráb Profile Picture
    237,874 Most Valuable Professional on at

    That's the expected behavior (the default implementation of ToString()). If you want something else, such as iterating all elements of the array and reading their FieldID property, change your code to do it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans