แก้ไข

แชร์ผ่าน


Access external web services

Plug-ins and custom workflow activities can access the network through the HTTP and HTTPS protocols. This capability provides support for accessing popular web services like social sites, news feeds, web services, and more. The following web access restrictions apply to this sandbox capability.

Other methods of accessing web services include the use of Webhooks and the Azure Service Bus. Refer to the links provided below for more information on those topics.

How to access external web services

Today most people are familiar with the System.Net.Http.HttpClient Class. HttpClient was introduced with .NET 4.5 and provides significant capabilities over the System.Net.WebClient Class which is still available.

For new plug-ins you should use HttpClient because the .NET team doesn't recommend WebClient for new development. However, this doesn't mean you must replace any legacy code uses of WebClient that you find. Most of the advantages provided in HttpClient do not necessarily provide advantages within a plug-in. HttpClient is intended to be re-used and is asynchronous by default. Unless you are making multiple HTTP requests within your plug-in, WebClient is designed for a single request. Because HttpClient is asynchronous by default, you need to break away from typical use patterns and add code to force the operations to be performed synchronously, typically by removing the await keyword and appending .GetAwaiter().GetResult() to any asynchronous calls.

WebClient provides simple synchronous methods such as UploadData, DownloadFile which don't clearly expose the underlying HTTP method used, but they can be set using specific overrides such as UploadString(String, String, String) in case you want to use PATCH instead of POST.

In most cases outside of plug-ins, you will want to use HttpClient. Within plug-ins, you can also use WebClient if you prefer.

Best practices

As called out in the following best practices topics:

You should make sure to set an appropriate Timeout period for your external calls and disable KeepAlive. See the above links for more information.

See also

Plug-ins
Workflow extensions
Azure integration
Use Webhooks
Sample: Web access from a sandboxed plug-in