Sample: Web access from a plug-in
This sample shows how to write a plug-in that can access network (web) resources like a web service or feed. It also demonstrates how to limit the time duration allowed for this call.
How to run this sample
- Download or clone the Samples repo so that you have a local copy. This sample is located under PowerApps-Samples-master\dataverse\orgsvc\C#\WebAccessPlugin.
- Open the sample solution in Visual Studio, navigate to the project's properties, and verify the assembly is signed during the build. Press F6 to build the sample's assembly (WebAccessPlugin.dll).
- Run the Plug-in Registration tool and register the assembly in the Microsoft Dataverse server's sandbox and database.
- When registering a step, specify a web URI string (that is,
https://www.microsoft.com
) in the unsecure configuration column.- The default value
https://www.bing.com
is used if none is provided.
- The default value
- Using an app or write code to perform the appropriate operation to invoke the message and table request that you registered the plug-in on.
- When the plug-in runs, if the duration of the call exceeds the 15-second limit, it throw's an error. Otherwise it should succeed.
- When you're done testing, unregister the assembly and step using the Plug-in Registrations tool.
What this sample does
When executed, the plug-in downloads web page data from the specified web service address (or the default address). If the request exceeds the 15-second limit, it throws an InvalidPluginExecutionException and write details to the Plug-in Trace Log.
If the
HttpClientPlugin
plug-in fails, it writes something like the following to the Plug-in Trace log:Downloading the target URI: https://www.bing.com Inner Exceptions: Exception: System.Threading.Tasks.TaskCanceledException: A task was canceled. Exception: Microsoft.Xrm.Sdk.InvalidPluginExecutionException: An exception occurred while attempting to issue the request. at PowerApps.Samples.HttpClientPlugin.Execute(IServiceProvider serviceProvider)
The TaskCanceledException is ambiguous about the cause of the task being canceled. For a more complete solution showing how to explicitly detect errors due to time-outs, see this blog post: Better time out handling with HttpClient.
How this sample works
In order to simulate the scenario described in What this sample does, the sample does the following:
Setup
- Checks the constructor's unsecure configuration parameter for a web address value; otherwise, the default value is used.
- The HttpClient Class class is used by the plug-in's
Execute
method to download web page data. - If the call exceeds the 15-second duration specified, an InvalidPluginExecutionException is thrown and details about the error is written to the Plug-in Trace Log.
Demonstrate
HttpClientPlugin plugin
- Uses the HttpClient Class and sets the Timeout Property to limit the allowed time for the operation to complete.
- Catches the expected AggregateException Class and examines the inner exceptions for the expected TaskCanceledException