Skip to content

nanoframework/System.Net

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework System.Net Library repository

Build status

Component Build Status NuGet Package
System.Net Build Status NuGet

NetworkHelper usage

NetworkHelper provides two patterns for establishing a network connection: a blocking token-based approach for simple use-cases, and an event-based approach for background connection management.

Token-based (retryable)

Call SetupAndConnectNetwork with a CancellationToken timeout. This method can be called repeatedly β€” if the first attempt times out, call it again:

bool connected = false;
while (!connected)
{
    CancellationTokenSource cs = new(30000);
    connected = NetworkHelper.SetupAndConnectNetwork(requiresDateTime: true, token: cs.Token);
    if (!connected)
    {
        Debug.WriteLine($"Network not ready, status: {NetworkHelper.Status}");
        // wait before retrying
        Thread.Sleep(5000);
    }
}

Event-based

Call SetupNetworkHelper once at startup. The helper connects in the background. Wait on NetworkReady:

NetworkHelper.SetupNetworkHelper(requiresDateTime: true);

if (!NetworkHelper.NetworkReady.WaitOne(30000, true))
{
    Debug.WriteLine($"Failed to connect: {NetworkHelper.Status}");
}

Note: NetworkReady is reset when the connection is lost and re-signaled when it is restored, accurately reflecting live network state. Code that previously assumed NetworkReady would remain set after first connect should be updated to handle transient disconnects.

Reset and reconfigure

Call Reset() to fully reset the helper so it can be called again with different settings, or to restart after an error:

NetworkHelper.Reset();

// Now call SetupNetworkHelper or SetupAndConnectNetwork again
NetworkHelper.SetupNetworkHelper(requiresDateTime: true);

SetupNetworkHelper throws InvalidOperationException if called a second time without a prior Reset(). Token-based methods (SetupAndConnectNetwork) do not have this restriction and are always retryable.

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.