Показать сообщение отдельно
Старый 01.05.2021, 14:15   #1  
Blog bot is offline
Blog bot
Участник
 
24,420 / 822 (76) +++++++
Регистрация: 28.10.2006
sertandev: X++: How to get the D365FO URL correctly inside backend services
Источник: https://devblog.sertanyaman.com/2021...kend-services/
==============

You may need to retrieve the URL of the D365 FO instance for logging or integration purposes, or for generating deep links. D365 FO foundation package provides you the URLUtility::getUrl() method for this. However this FO standard method does not work if you execute it from a batch or inside a custom API service. When we step into the code we see that it calls this method in Global class :

/// /// Gets the current request uri /// /// the current request uri internal static System.Uri GetCurrentUri() { Microsoft.Dynamics.Client.ServerForm.Contexts.SessionContext sessionContext; System.Uri currentUrl = null; sessionContext = Microsoft.Dynamics.Client.ServerForm.Contexts.SessionContext::get_Current(); if (sessionContext) { currentUrl = sessionContext.get_RequestUrl(); } return currentUrl; }Here is uses the SessionContext of ASP.NET to get the current URL. However if there is no browser session open, like in the case of services or batch runs, there is also no SessionContext and this code fails.

In that case we have other ASP.NET methods to get the URL, such as “HttpContext.Current.Request.Url.AbsoluteUri”, but the HttpContext is also not available inside X++ and I could not find any standard FO code that uses it (if you know a place that uses it, please feel free to share it in the comments). Upon further investigation, I have found this piece of code which works perfectly inside backend services as well :

using Microsoft.Dynamics.ApplicationPlatform.Environment;....................................IApplicationEnvironment env = EnvironmentFactory::GetApplicationEnvironment();str currentUrl = env.Infrastructure.HostUrl;System.Uri currentHost = new System.Uri(currentUrl);Then to get the root path of the URL you can use:

str hostUrl = currentHostUrl.GetLeftPart(System.UriPartial::Authority);To generate deeplinks using this code, put it inside a method in your own utility class and use it with the code in this blog post replacing the “URLUtility::getUrl()” call with your own method.



Источник: https://devblog.sertanyaman.com/2021...kend-services/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.