Dynamics 365 triggers calls to Azure Function based on Webhook

Create a function app

First log in to the Azure Application Service Center
https://portal.azure.com/

Click Function Application,
Insert image description here
write the corresponding information, and create the application.
Insert image description here

The created function application can be understood as a cloud service site that provides webapi. Multiple functions can be added to the function application.

Insert image description here

The triggering of the function can be based on various modes, as shown in the figure below, it can be triggered based on an http request, or it can be triggered based on other methods, such as when a message is inserted into the queue, or it can be used as a timer for scheduled tasks.
Insert image description here

Create an Azure Function project in VS

First open VS, create an Azure Function project,
Insert image description here
add Azure Functions to the project
Insert image description here
and select http trigger
Insert image description here
The following picture shows the test code I added

Insert image description here

 public static class AccountTest
    {
    
    
        [FunctionName("AccountTest")]
        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
            ILogger log)
        {
    
    
            log.LogInformation("Start AccountTest.");
            string content = await req.Content.ReadAsStringAsync();
            log.LogInformation("jsonContent:"+ content);

            RemoteExecutionContext context = GetContext(content);
            Entity updatedAccount = (Entity)context.InputParameters["Target"];
            log.LogInformation(updatedAccount.LogicalName + ": " + updatedAccount.Id);
            log.LogInformation("End AccountTest.");
            return req.CreateResponse(HttpStatusCode.OK);
        }
        public static RemoteExecutionContext GetContext(string contextJSON)
        {
    
    
            RemoteExecutionContext rv = null;
            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(contextJSON)))
            {
    
    
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RemoteExecutionContext));
                rv = (RemoteExecutionContext)ser.ReadObject(ms);
            }
            return rv;
        }
    }

Register the written Azure Function to the function application

Go back to the Azure application portal, open the function app, and click "Get publishing configuration file" as shown below
Insert image description here

After downloading the release configuration file, go back to VS, click Publish, and select Import Configuration File, the next step

Insert image description here

Select the downloaded "Get Publish Configuration File" and click Finish.

Insert image description here

The configured release is as follows, just click Publish.

Insert image description here

After the release is complete, go back to the management center, find "Function", and you can see the newly released function (it may take a few seconds or minutes to see it in the management center after the release is successful)

Insert image description here

Register the written function through the plug-in registration tool

Open the newly registered function and click "Get function url"

Insert image description here

Copy the url, the content contained in the url has two parts, as shown in the figure below, the red and yellow boxes

Insert image description here

Open the plug-in registration tool, link the dynamics 365 environment, click "Register" and select "Register New Web Hook"
Insert image description here

Fill in the two parts of the above url to the positions shown in the figure below, and click Save.
Insert image description here

At this time, we can add corresponding steps like a plug-in, as shown below

Insert image description here

Simple test in the system

Create a customer record at will

Insert image description here

Then go back to the Azure Function App Center and monitor the function triggered by the record you just created.
Insert image description here

Click in and you can see the log added in the specific code.

Insert image description here

If you use VS to start and debug Azure Function, the error "Unable to start the Azure storage emulator. Please run it as an administrator" is reported, and you can kill the process corresponding to port 10000

1. Check port netstat -ano|findstr [port]
Example: netstat -ano|findstr 10000

2. Kill the process taskkill /pid [PID] /f
Example: taskkill /pid 19672 /f

If you need to call http trigger for VS debugging, after running, it will appear as shown in the figure below. After adding a breakpoint, you can directly call the local debugging through the web page or postman to enter the breakpoint.

Insert image description here

GET

Insert image description here

POST

Insert image description here
C# backend receive

Insert image description here

Debug the function published to Azure, and obtain the calling function as shown in the figure below

Insert image description here

postman is called as follows

GET

Insert image description here

POST

Insert image description here

Guess you like

Origin blog.csdn.net/tantu666/article/details/131810448