Partager via


How to manage metered network cost constraints (XAML)

This topic details best practices for maintaining awareness of network connection cost or data plan status changes and enable your app to use this information to avoid incurring additional costs for roaming or exceeding a specified data transfer limit.

Prerequisites

The following examples use C# or C++ and are based on the Network information sample. For general help creating a Windows Runtime app using C# or Visual Basic, see Create your first Windows Runtime app using C# or Visual Basic. For general help creating a Windows Runtime app using C++, see Create your first Windows Runtime app using C++.

Knowing what a ConnectionProfile is and how to access the information that it represents is important; for more information see How to retrieve network connection information. For additional code examples, download the Network Information sample.

What are metered networks?

A metered network is any network that imposes a cost to the user for a specified amount of data usage or while roaming. Most mobile network providers have placed caps on the amount of data transferred per month, per user, and exceeding the data cap (or transferring data while roaming) can result in significant charges to the user. With the growing popularity of mobile broadband technologies, increasing demand for data consumption will increase the impact of this issue, leading to a more variable set of data plans. As a result, Windows Store apps need to be aware of such network limitations and adapt its behavior as conditions change.

Windows 8 categorizes metered networks as allowing unrestricted, fixed, or variable cost limits; NetworkCostType defines these values. If a user goes over the cap specified by the data plan or are roaming, most networks implement a steep charge either financially, or by blocking/throttling access.

Most mobile network users will move between various networks, metered and otherwise, throughout the course of a day. A 3G/4G network might be used for network connectivity while away from home or the office. When at home or the office, one or more Wi-Fi networks are often available; similarly, those same Wi-Fi networks will eventually be unavailable when the user leaves home. There may also be times when no network is available. Given the proliferation of Wi-Fi and mobile broadband networks, such network change scenarios are increasingly common.

Who is impacted?

Improper use of metered network bandwidth can influence the reputation of your app and organization. In general, all Windows Store apps (especially those that transfer large amounts of data) need to follow the guidance below.

General guidance

Your app can be made aware of connection changes, in this case cost or data plan changes, by registering for connection change events. Once notified of a change, your app can access the indicated ConnectionProfile for current information before a decision is made regarding app behavior. For instructions on this process, see How to manage network connection events and changes in availability.

Specific to metered network scenarios, the following ConnectionCost properties can be used to gather information and identify current data plan settings for a specific connection:

Property Description

ApproachingDataLimit

Gets a value that indicates if a connection is approaching the data usage allowance specified by the data plan, which is defined by the carrier. This limitation is most commonly referred to as a data cap.

NetworkCostType

Gets a value that indicates the current network cost for a connection.

  • Unrestricted: Use of this network connection is unlimited. It has no usage charges and capacity constraints.
  • Fixed: Use of this network connection is unrestricted up to a data limit set by the carrier.
  • Variable: Use of this network connection is metered on a per-byte basis.
  • Unknown: Cost information is not available for this network connection.

OverDataLimit

Gets a value that indicates if the connection has exceeded the data usage allowance specified by the data plan, which is defined by the carrier.

Roaming

Gets a value that indicates whether the connection is established with a network outside of the home provider.

 

Apps that use metered networks to connect to destinations on the Internet can obtain cost information directly by calling GetInternetConnectionProfile and accessing the associated cost properties. By using this information, an app can follow these guidelines to decide how best to use network resources:

Behavior App Guidelines Example

Normal

If the NetworkCostType is Unrestricted or Unknown and the ConnectionCost is not Roaming, then your app should implement Normal behavior.

In Normal scenarios, your app does not implement restrictions. Apps treat the connection as Unlimited in cost, and Unrestricted by usage charges and capacity constraints.

Examples:

  • Play an entire HD movie.
  • Download a large file without restrictions or UI prompts.
  • Media player app plays an entire HD movie.
  • App downloads a large file without any restrictions or prompts.

Conservative

If the NetworkCostType is Fixed or Variable, and the ConnectionCost is not Roaming or OverDataLimit, then the app should implement Conservative behavior.

In conservative scenarios, the app should implement restrictions for optimizing network usage to handle transfers over metered networks.

Examples:

  • Play movies in lower resolutions.
  • Delay non-critical downloads.
  • Avoid pre-fetching of information over a network.
  • Switch to a header-only mode when retrieving email messages.
  • Media player app plays movies in lower resolutions.
  • App delays non-critical downloads.

Opt-In

If the ConnectionCost is Roaming or OverDataLimit, your app should implement Opt-In behavior.

For opt-in scenarios, your app should handle cases where the network access cost is significantly higher than the plan cost. For example, when a user is roaming, a mobile carrier may charge a higher rate data usage.

Examples:

  • Prompt the user before accessing the network.
  • Suspend all background data network activities.
  • App prompts user to access the network.
  • App suspends all background data network activities.

 

This code example checks the connection cost and returns suggestions for appropriate app behavior.

public enum NetworkCost { Normal, Conservative, OptIn };

public class CostGuidance
{
    public CostGuidance()
    {
        var connectionCost = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();
        Init(connectionCost);
    }
    public NetworkCost Cost { get; private set; }
    public String Reason { get; private set; }

    public void Init(ConnectionCost connectionCost)
    {
        if (connectionCost == null) return;
        if (connectionCost.Roaming || connectionCost.OverDataLimit)
        {
            Cost = NetworkCost.OptIn;
            Reason = connectionCost.Roaming 
                ? "Connection is roaming; using the connection may result in additional charge."
                : "Connection has exceeded the usage cap limit.";
        }
        else if (connectionCost.NetworkCostType == NetworkCostType.Fixed 
            || connectionCost.NetworkCostType == NetworkCostType.Variable)
        {
            Cost = NetworkCost.Conservative;
            Reason = connectionCost.NetworkCostType == NetworkCostType.Fixed
                ? "Connection has limited allowed usage."
                : "Connection is charged based on usage. ";
        }
        else
        {
            Cost = NetworkCost.Normal;
            Reason = connectionCost.NetworkCostType == NetworkCostType.Unknown
                ? "Connection is unknown"
                : "Connection cost is unrestricted";
        }
    }
}

Behavior differences between Windows Store apps and Windows Phone Store apps

Some mobile broadband providers do not set the cost information for a user's plan in the provisioned profile for mobile broadband. The user is responsible for entering this cost information on Windows and Windows Phone.

Cost information is reported differently for mobile broadband networks. On Windows 8.1, if the cost information is not set by the user for mobile broadband, NetworkCostType will be reported as Fixed cost. On Windows Phone 8.1, if the cost information for the mobile broadband (cellular data) is not set, it will reported as Unknown. This behavior difference affects the general guidance above for how to check the connection cost and returns suggestions for appropriate app behavior. Using the above guidance Windows 8.1 would suggest Conservative behavior and Windows Phone 8.1 would suggest Normal behavior. Both platforms should suggest Conservative behavior. As a result, the general guidance for appropriate behavior should be revised on Windows Phone for mobile broadband profiles.

Handling maximum transfer size

Transferring large files over a metered network can result in significant, unexpected costs for the user. Apps that synchronize, upload or download files when on a metered network can do so without user permission only if the file size is less than the Max Transfer size specified by the carrier.

To transfer large amounts of data, apps should obtain user permission or wait for access to an unrestricted network. Optionally, the application can allow the user to bypass future user permission messages.

The following example code demonstrates how to retrieve the maximum transfer size for a connection:

var dataPlanStatus = InternetProfile.GetDataPlanStatus();

if (dataPlanStatus.maxTransferSizeInMegabytes !== null) {
  
    mySample.displayStatus("Maximum Transfer Size in Megabytes: " + dataPlanStatus.maxTransferSizeInMegabytes);
}
else{

     mySample.displayError("Maximum Transfer Size in Megabytes: " + "Not Defined");
}

Summary

In this topic we reviewed metered network scenarios and how best to prevent your connected app from incurring additional charges on behalf of the end-user.

While this covers metered network scenarios, a NetworkStatusChanged event can also represent changes to the availability of a connection. For more information and guidance on how best to engage these scenarios, see How to manage network connection events and changes in availability.

Other

Create your first Windows Runtime app using C# or Visual Basic

Create your first Windows Runtime app using C++

How to handle exceptions in network apps

How to manage network connection events and changes in availability

How to retrieve network adapter and locality information

How to retrieve network connection information

How to retrieve network connection usage data

Reference

ConnectionCost

ConnectionProfile

NetworkCostType

Windows.Networking.Connectivity

Samples

Network information sample

Network status background sample