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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Resolving labels using the ILabelResolver

(0) ShareShare
ReportReport
Posted on by

Hi,

I'm writing some code in C# to talk to the AOS and get some information about AOT objects. I’ve been using Peter Villadsen’s examples that he uploaded to GitHub for reference and he uses the following method to get labels:

 

        /// <summary>

        /// Resolve the given label to the text it represents in the default language. If a non

        /// label is passed, that text is returned unchanged.

        /// </summary>

        /// <param name="label">The label to look up.</param>

        /// <returns>The text for the label resolved to the current language.</returns>

        private string ResolveLabel(string label)

        {

            var labelResolver = CoreUtility.ServiceProvider.GetService(typeof(Microsoft.Dynamics.Framework.Tools.Integration.Interfaces.ILabelResolver)) as Microsoft.Dynamics.Framework.Tools.Integration.Interfaces.ILabelResolver;

 

            if (labelResolver != null)

            {

                return labelResolver.GetLabelText(label);

            }

            return label;

        }

 

I am trying to do the same and my code is similar:

 

        

private String getLabel(string _labelID)

        {

            String      ret;

 

            ret = "";

 

          

            if (_labelID != "")

            {

                var labelResolver = CoreUtility.ServiceProvider.GetService(typeof(Microsoft.Dynamics.Framework.Tools.Integration.Interfaces.ILabelResolver)) as Microsoft.Dynamics.Framework.Tools.Integration.Interfaces.ILabelResolver;

 

                if (labelResolver != null)

                {

                    ret = labelResolver.GetLabelText(_labelID);

                }

            }

 

            return ret;

 

In my test project I’ve been able to pick up a label ID from a table but when I try to resolve I get the an NullReferenceException error on the line where I create my variable.

 

So clearly its not getting the object from the AOS, although I can talk to the AOS and I can get AX classes and tables etc. Now I have seen some changes in the references in C# that are not in his code so I’m not sure if its because we are using different version.

*This post is locked for comments

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

    Which object is null? labelResolver? In which context are you calling this code?

    You can also consider the metadata service.

  • Community Member Profile Picture
    on at

    Yes the label resolver. When you say the metadata service, is this not what this is using? Do you have any examples of resolving a label in to text?

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Therefore the service isn't available to the service locator in the given context (which I have no information about).

    An example of using the metadata service is [AX URL]/metadata/Labels(Id='@Budget:RecordDoesNotExist',Language='en-us').

  • Community Member Profile Picture
    on at

    I'm not sure what more you need as far as context. The Method is called with the label ID - e.g. "@SYS123" but other than that its a standalone method which could be static if I needed as all it does is call the service.

    Here is the cs file (renamed as .txt) its a bit messy as I've been trying a few things but you should be able to run  it

    http://wp.me/a41MkV-4l

    As I'm using this as a learning exercise I'd like to use the new stuff if possible.

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    For instance, I didn't know in which kind of application you're running your code, how did you set up services and so on. I just saw a piece of C# code, which could be used in any kind of application in infinite number of ways.

    Only when looking at the complete code, I can now see that you're using a separate console application. When I have time, I'll remove unrelated code from it and try to run it. I don't know without investigation how the ILabelResolver service can be initialized.

    In the meantime, please tell us on which computer you're running your console application.

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Where did you get the information that this service can be used outside Visual Studio? I suspect you're trying to take code intended for a VS add-in and put it into a complete separate application.

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    I've simplified your application to this:

    using System;
    using Microsoft.Dynamics.Framework.Tools.MetaModel.Core;
    using Microsoft.Dynamics.Framework.Tools.Integration.Interfaces;
    
    namespace ConsoleApplication1
    {
        class Program
        {       
            static void Main(string[] args)
            {
                var program = new Program();
                program.getLabel("@SYS123");
            }
    
            private String getLabel(string _labelID)
            {
                var labelResolver = CoreUtility.ServiceProvider.GetService(typeof(ILabelResolver)) as ILabelResolver;
    
                if (labelResolver != null)
                {
                    return labelResolver.GetLabelText(_labelID);
                }
    
                return "";
            }
        }
    }

    And when I tried to run it, I found it doesn't fail because labelResolver is null. It fails even before is can get there, because there is no service provider at all. Your application doesn't contain any code initializing the service provider (and registering services in it).

  • Community Member Profile Picture
    on at

    "In the meantime, please tell us on which computer you're running your console application."

    Its the 365 update 3 VM

    "Where did you get the information that this service can be used outside Visual Studio"

    Its the C# code from a VS project so its all run within VS.

    "It fails even before is can get there, because there is no service provider at all. Your application doesn't contain any code initializing the service provider (and registering services in it)."

    When I run it I can talk to the AOS and pull information about the objects I have listed in the switch (It'll get expanded once I get this issue fixed). I've only uploaded the C# code as the whole project zipped is just shy of 40MB.

    As an aside - is there a SYS123? I just used that as an example of the label format.

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    This is completely wrong: "Its the C# code from a VS project so its all run within VS". Your console application is a separate process; that you develop the application in Visual Studio and not some other editor is irrelevant and that you use Visual Studio to start the application doesn't mean it's executed inside the VS process.

    That some other code in your application works doesn't say anything about this problem. You can't just ignore the facts I showed you.

    What does it matter whether SYS123 exists or not? Your code will never get to using it, because it fails much sooner.

  • Community Member Profile Picture
    on at

    "Your console application is a separate process; that you develop the application in Visual Studio and not some other editor is irrelevant and that you use Visual Studio to start the application doesn't mean it's executed inside the VS process"

    Then I'm not sure what you mean ....... its C# code, just like Peter Villadsen’s. Which type of C# project should I use?

    "That some other code in your application works doesn't say anything about this problem. You can't just ignore the facts I showed you."

    Nobody is ignoring anything and look your coming off as aggressive, I'm only after some help here not an argument.

    My point was that the other code uses the metadata interfaces fine, which indicates at least some of this works in a console application. Maybe the ILabelResolver does not, but I'd ask why would that not when items such as the Microsoft.Dynamics.Framework.Tools.MetaModel.Core works and if it does not work that way, then what is the correct way to use it?

    "What does it matter whether SYS123 exists or not? Your code will never get to using it, because it fails much sooner."

    Because your code attempts to use 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
Henrik Nordlöf Profile Picture

Henrik Nordlöf 2 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans