Create a function app
First log in to the Azure Application Service Center
https://portal.azure.com/
Click Function Application,
write the corresponding information, and create the application.
The created function application can be understood as a cloud service site that provides webapi. Multiple functions can be added to the function application.
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.
Create an Azure Function project in VS
First open VS, create an Azure Function project,
add Azure Functions to the project
and select http trigger
The following picture shows the test code I added
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
After downloading the release configuration file, go back to VS, click Publish, and select Import Configuration File, the next step
Select the downloaded "Get Publish Configuration File" and click Finish.
The configured release is as follows, just click Publish.
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)
Register the written function through the plug-in registration tool
Open the newly registered function and click "Get function url"
Copy the url, the content contained in the url has two parts, as shown in the figure below, the red and yellow boxes
Open the plug-in registration tool, link the dynamics 365 environment, click "Register" and select "Register New Web Hook"
Fill in the two parts of the above url to the positions shown in the figure below, and click Save.
At this time, we can add corresponding steps like a plug-in, as shown below
Simple test in the system
Create a customer record at will
Then go back to the Azure Function App Center and monitor the function triggered by the record you just created.
Click in and you can see the log added in the specific code.
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.
GET
POST
C# backend receive
Debug the function published to Azure, and obtain the calling function as shown in the figure below
postman is called as follows
GET
POST