Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

How to figure out whether a piece of code is running in PROD environment

(0) ShareShare
ReportReport
Posted on by 65

I have a piece of code that needs to run an integration depending on whether the code is running in PROD environment. What is the best way of determining this? Microsoft changes DB Server name from time to time. I am thinking of using DB name since it seems to be stable. We don't want to check any field that is set manually since that is error prone. For eg. we can check if email parameter is set to blank or any particular value since we use different email for PROD and other environments but there is always a chance that someone might use PROD email for nonPROD environment.

Any suggestions?

  • MarkoArsovski Profile Picture
    MarkoArsovski 10 on at
    RE: How to figure out whether a piece of code is running in PROD environment

    Try using this code if you need it from a non interactive session (like batch or custom service):

    using Microsoft.Dynamics.ApplicationPlatform.Environment;

    ..................

    ..................

    IApplicationEnvironment env = EnvironmentFactory::GetApplicationEnvironment();

    str currentUrl = env.Infrastructure.HostUrl;

    System.Uri currentHost = new System.Uri(currentUrl);

  • Suggested answer
    HarryDesh Profile Picture
    HarryDesh 65 on at
    RE: How to figure out whether a piece of code is running in PROD environment

    Unfortunately URL returns blank in batch mode. We decided to use database name, there is no good solution here.

  • HarryDesh Profile Picture
    HarryDesh 65 on at
    RE: How to figure out whether a piece of code is running in PROD environment

    Yes, we discussed with auditors the problem with testing such kind of a solution.

  • HarryDesh Profile Picture
    HarryDesh 65 on at
    RE: How to figure out whether a piece of code is running in PROD environment

    I think we will go with the option to check the URL. Our auditors are not happy with any setting that can be changed by users.

  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,642 Super User 2024 Season 2 on at
    RE: How to figure out whether a piece of code is running in PROD environment

    Hi Harry,

    There are multiple places in the standard application where some settings might be production dependent. The best way is to ensure the configuration is different in each environment. E.g. stop the batches for email processing or remove mail settings. Or change the settings for a certain integration. Clear authentication fields. That is the way how it is managed for standard features..

  • nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to figure out whether a piece of code is running in PROD environment

    What is your business requirement? Are you sure that the technical solution that you are thinking is a good solution for the business requirement? This kind of code would be impossible to test (or troubleshoot) in non-prod system so it sounds like asking for trouble.

    Anyway, you can check the environment URL by URLUtility::getUrl(). If it has "sandbox" in it, it's a sandbox environment. If it has "onebox" or "devaos" in it, it's a dev system. Otherwise it's a prod system (unless I missed some other keyword, but anyway).

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans