最新消息:ww12345678 的部落格重装上线,希望大家继续支持。

在Dynamics 365 Finance / SCM中从X ++调用Azure函数 /Call an Azure Function from X++ in Dynamics 365 Finance / SCM

网络文摘 William 237浏览 0评论

Create an Azure Function

Azure Functions are simple way to pack and provide business logic as web service without worrying about hosting a web server. Azure Functions can be implemented in different programming languages like C#, JavaScript, PHP, Java, etc. and can be hosted on Linux and Windows with different runtime environments that feed your need. In the Azure Portal click + Create a resource and search for Function App:
Create a Azure Function App
In the next screen choose a subscription and create a resource group (or use an existing one if you like). Provide a useful name and choose code as Publish method. Select .NET Core 3.1 as runtime stack and a region that is near your location:
Configure the Azure Function App to use .NET Core 3.1
Click Review + Create to create the Azure Function. It takes a view minutes to provision all the required elements:
Deploy the Azure Function App
Click on Go to Resource. Next to the Functions group click + to create a new function and select In-Portal to edit the function code direct in the browser:
Create a new HTTP trigger
Choose the webhook + API to create a demo function that can be called via HTTP POST.
Use webhook for the Azure Function
This will create a function that takes a name as parameter and returns “Hello ” + the parameter name.
C# Azure Function code
You can test the function by using Test tab on the right. The function takes a JSON string with a name parameter and returns a simple string.
Test the Azure Function with a JSON string

Call the function from X++

In the azure portal get the function URL with a function key. Copy the URL with the key:
Copy the Azure Function URL with function key
In Visual Studio create an X++ class with a main method for testing. Use the System.Net.Http.HttpClient class to call the service. The content is a JSON string encoded in UTF-8 with a name parameter and value. In this example the name is Dynamics:
System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpContent content = new System.Net.Http.StringContent(
        "{\"name\":\"Dynamics\"}",
        System.Text.Encoding::UTF8,
        "application/json");
At the moment X++ does not support the await keyword for asynchronouse calls. The workaround is to use the Task.Wait() method. Call the service with your function URL async and get the content of the call:
var task = httpClient.PostAsync("https://<YOUR_FUNCTION_URL>",content);
task.Wait();
System.Net.Http.HttpResponseMessage msg = task.Result;

System.Net.Http.HttpContent ct = msg.Content;
var result = ct.ReadAsStringAsync();
result.Wait();
System.String s = result.Result;

info(s);
Start the class from Visual Studio. The result should look like this:
Call the Azure Function from Dynamics 365 Finance
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址