Announcements
Hello
I am making a call from my plugin to an Azure Function using a webhook. I am using the below code
IServiceEndpointNotificationService cloudService = (IServiceEndpointNotificationService)serviceProvider.GetService(typeof(IServiceEndpointNotificationService)); if ( cloudService == null ) throw new InvalidPluginExecutionException ( "Failed to retrieve the service bus service." ); string response = cloudService.Execute(new EntityReference("serviceendpoint", serviceEndpointId), context); if ( !String . IsNullOrEmpty ( response ) ) { tracingService . Trace ( "Response = {0}" , response ); }
Azure function returns a simple "Hello" message back. The Azure function is working fine when I test it from Azure Portal.
However, in the plugin the Execute method returns null as response. Anyone has an idea what could be wrong in the code?
Hi Wahaj,
Yes its all correct and registered in sandbox mode.
It looks like the http code returned is coming back correctly but the message is lost - it looks like MS has not implemented that correctly.
If I return a 200 then the plugin continues after execute but results string is empty.
If I put a try, catch around the Execute call to the endpoint, when an error code is returned like 500 then I can catch it and throw a pluginexception error back to display a friendly messsage. This is a workaround as I actually want the message sent back from the function instead of assuming a generic message will fit all errors that might have occured.
Thanks
Hi,
Did you verify:
Hi,
Plugin code:
try
{
QueryExpression endpointQuery = new QueryExpression("serviceendpoint");
endpointQuery.ColumnSet = new ColumnSet("url");
ConditionExpression urlCond = new ConditionExpression("url", ConditionOperator.EndsWith, "MyWebHook");
endpointQuery.Criteria.AddCondition(urlCond);
var endpointColl = Service.RetrieveMultiple(endpointQuery);
if (endpointColl.Entities.Count == 0)
{
throw new InvalidPluginExecutionException("Failed to retrieve the endpoint");
}
var endpoint = endpointColl[0];
Tracing.Trace($"Taking Enpoint URL: {endpoint["url"]}");
Tracing.Trace($"Endpoint: {endpoint.Id}");
Tracing.Trace("Posting the execution context.");
string response = cloudService.Execute(new EntityReference("serviceendpoint", endpoint.Id), Context);
//**ISSUE IS HERE STRING IS ALWAYS NULL
if (!String.IsNullOrEmpty(response))
{
Tracing.Trace("Response = {0}", response);
}
Tracing.Trace("Done.");
}
Web hook:
[FunctionName("MyWebHook")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", WebHookType = "genericJson", Route = null)]HttpRequestMessage req, TraceWriter log)
{
IOrganizationService service = null;
log.Info("In CheckAttachment");
var contextJson = await req.Content.ReadAsStringAsync();
var context = contextJson.GetContext();
return req.CreateResponse(HttpStatusCode.OK, "Created");
}
In postman I can see "Created" is returned but not in the plugin trace:
Posting the execution context.
Done.
Completed plugin execution
It goes straight to null instead of tracing the string that should be in the response.
Hi,
If possible, can we see snapshots of your azure function and plugin code?
Hi, it works fine in postman its just coming back into Dynamics the string is empty. From all paths a http status is returned.
Hi,
Can you please try this outside of Dynamics first? Like a postman call.
If it works, I would recommend you to look at your function definition, are sending any parameters from Dynamics? Please make sure, your function returns HTTP status from all paths.
Hi,
Thanks for the links. I have just updated my post, there was an error in the endpoint I was calling, so the actual issues is that a null string is being returned now instead of what I'm returning with the httpstatus.OK
Any ideas?
Thanks
Thank you for you query.
Please se following:
Here is a similar thread with verified answer:
Hi,
Did you resolve this? I'm trying the same thing, trying to invoke a web hook from a plugin
web hook:
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", WebHookType = "genericJson", Route = null)]HttpRequestMessage req, TraceWriter log)
just returning:
var contextJson = await req.Content.ReadAsStringAsync();
var context = contextJson.GetContext();
return req.CreateResponse(HttpStatusCode.OK, "Created");
The string is null when it is returned back from the function.
The plugin is running synchonously, tried on pre-operation and post-operation but no difference.
Any ideas?
Thanks
André Arnaud de Cal...
294,079
Super User 2025 Season 1
Martin Dráb
232,860
Most Valuable Professional
nmaenpaa
101,158
Moderator