C# Integration
This guide provides instructions for integrating the FleetShare SDK into a C# application. The SDK uses asynchronous programming to run concurrently with your main application logic. C# is a versatile, object-oriented programming language developed by Microsoft as part of the .NET framework.
Requirements
Before you begin, ensure you have the following dependencies in your project:
- System
- System.Collections.Concurrent
- System.Collections.Generic
- System.Net.WebSockets
- System.Net.Sockets
- System.Text
- System.Threading
- System.Threading.Tasks
- Newtonsoft.Json
Add these dependencies to your project file or install them via NuGet package manager.
Integration Steps
1. Import Required Namespaces
Ensure you import the required FleetShare SDK C# namespaces into your project. The FleetShare team will provide you with the necessary files.
2. Define Your Main Method
Define your Main
method where you will start the FleetShare SDK and run your main program logic.
3. Start the FleetShare SDK
Invoke the FleetShare SDK asynchronously. Use the provided apiKey
and, if necessary, additional parameters like silent
and deviceName
.
Parameters:
- apiKey: Your API key from the FleetShare SDK program.
- silent: (Optional) If set to
true
, the SDK runs silently without outputting logs. - deviceName: (Optional) A unique name for the device running the SDK.
4. Implement Main Program Logic
Include your main program logic within the Main
method. The FleetShare SDK will continue to share bandwidth in the background while your program runs.
Example Implementation
Here’s an example of integrating the FleetShare SDK into a C# application:
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string apiKey = "YOUR_API_KEY";
bool silent = false;
string deviceName = "MyDevice";
Console.WriteLine("Starting FleetShareSDK...");
try
{
await FleetShareSDK.StartFleetShareSDK(apiKey, silent, deviceName);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
// Your main program logic goes here
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"Main program is running... ({i + 1})");
await Task.Delay(1000); // Wait for 1 second
}
}
}
Best Practices
- Error Handling: Implement robust error handling within your asynchronous methods to manage potential issues.
- User Consent: Make sure to obtain user consent before initiating the bandwidth sharing process.
- Updating the SDK: Stay updated with new SDK versions and implement mechanisms to handle SDK updates gracefully, as older versions may become deprecated.
Ensure you follow these best practices to maintain a secure and user-friendly application.
Troubleshooting
If you encounter any issues while integrating or using the FleetShare SDK, consider the following:
- Ensure you’re using the latest version of the SDK.
- Check that all required dependencies are correctly added to your project.
- Verify that your API key is correct and active.
- If you’re experiencing connection issues, check your network connectivity.
For further assistance, please contact the FleetShare support team.