Skip to main content
To install the C# SDK:
1
Confirm that you meet the minimum requirements. (Prerequisites)
2
Install the Ditto SDK. (Installing Dependencies)
3
Set up your app permissions. (Configuring Permissions)
4
Add Ditto to your app. (Initializing Ditto)
5
(Optional) Declare a foreground service (Android only). (Declaring a Foreground Service)

Prerequisites

Following are the minimum requirements that must be met before attempting to install Ditto:

Installing Dependencies

To use the Ditto SDK in your project, you’ll need to install the appropriate NuGet package.
If using a package manager other than NuGet, the official package manager for .NET, the command syntax to install Ditto may differ from the following snippet. For more information, see the official Nuget documentation > Ditto .NET
Install-Package Ditto -Version 5.0.1

.NET Framework 4.8+ setup

Ditto supports .NET Framework 4.8 and 4.8.1 via the netstandard2.0 asset shipped in Ditto 5.0.1 and later. Version 5.0.0 does not include this support; use 5.0.1 or higher.
Install the Ditto NuGet package and you are done. No additional steps are required.
Multi-project solutions: if Ditto is used by a class library referenced by your .exe, install the Ditto package directly on the .exe (startup) project as well. Native DLLs do not propagate through <ProjectReference> chains in .NET Framework. Architecture: the Ditto native DLL is Windows x64 only. Ensure your project has <PlatformTarget>x64</PlatformTarget> and <Prefer32Bit>false</Prefer32Bit>. Older WinForms templates default to “AnyCPU + Prefer 32-bit,” which loads as a 32-bit process and fails to load the x64 native DLL. For a working reference, see the dotnet-winforms-net48 sample on GitHub.

Troubleshooting

  • System.DllNotFoundException: Unable to load DLL 'dittoffi' (HRESULT 0x8007007E): The native DLL is not in the runtime output directory of your .exe. For packages.config projects, confirm the <Content Include> entry shown above is present in the .exe project’s csproj (not just a referenced library) and that the path resolves to a real file on disk. For PackageReference projects, ensure <RuntimeIdentifier>win-x64</RuntimeIdentifier> is set on the .exe project.
  • System.BadImageFormatException (HRESULT 0x8007000B): The native DLL is being found, but its architecture does not match the process. Confirm your .exe project has <PlatformTarget>x64</PlatformTarget> and <Prefer32Bit>false</Prefer32Bit>. The dotnet-winforms-net48 sample shows the correct settings.

Configuring Permissions

To set up your environment for your target platform:
For apps targeting iOS; for example, MAUI, once you’ve added the Ditto package dependency, configure permissions and ensure your app continues syncing while running in the background, including when the end-user device is locked, by enabling background modes.
1
Open the Info.Plist file for your project.
2
Add the following key-value pairs, and then, if desired, modify each string value:
XML
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Uses Bluetooth to connect and sync with nearby devices</string>
<key>NSLocalNetworkUsageDescription</key>
<string>Uses WiFi to connect and sync with nearby devices</string>
<key>NSBonjourServices</key>
<array>
<string>_http-alt._tcp.</string>
</array>
3
Enable Bluetooth Background Modes:
XML
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
</array>

Requesting Permissions at Runtime

In addition to being declared, some permissions need to be requested from the user at runtime. To ensure your app has the required permissions for sync, use the DittoSyncPermissions helper class from the Ditto SDK. This helper class simplifies the process of requesting end-user permissions at runtime with a single method call for all necessary permissions. Ensure Sync.Start() is invoked after permissions have been requested.
C#
public async Task CheckPermissions()
{
    await DittoSyncPermissions.RequestPermissionsAsync();
    ditto.Sync.Start();
}

Initializing Ditto

Once you’ve installed the Ditto SDK, set the debug level you want Ditto to log messages for, create a new Ditto identity, and set up Ditto exception handling for data sync operations:
C#
using DittoSDK;

// Copy the "URL" and "Database ID" from the Ditto Portal (Connect via SDK).
var databaseId = "REPLACE_ME_WITH_YOUR_DATABASE_ID";
var config = new DittoConfig(
    databaseId: databaseId,
    connect: new DittoConfigConnect.Server(new Uri("REPLACE_ME_WITH_YOUR_URL"))
);

var ditto = await Ditto.OpenAsync(config);

ditto.Auth.ExpirationHandler = async (ditto, secondsRemaining) => {
    try
    {
        var result = await ditto.Auth.LoginAsync("YOUR_PLAYGROUND_TOKEN", DittoAuthenticationProvider.Development);
    }
    catch (Exception e)
    {
        Console.WriteLine($"Authentication failed: {e}");
    }
};

ditto.Sync.Start();

Declaring a Foreground Service (Android Only)

A foreground service allows your app to continue syncing data with Ditto while running in the background. A foreground service is not required to use Ditto, but without it your app will stop syncing whenever it is in the background. To declare the foreground service, add the following to your AndroidManifest.xml:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" tools:targetApi="upside_down_cake" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" tools:targetApi="tiramisu" />

<application ...>
    <service
        android:name="com.ditto.internal.transports.foregroundservice.InternalDefaultDittoForegroundService"
        android:exported="false"
        android:foregroundServiceType="connectedDevice"
        android:stopWithTask="true">
        <meta-data android:name="notificationChannelName" android:value="Background Data Sync" />
        <meta-data android:name="notificationChannelId" android:value="BackgroundDataSyncId" />
        <meta-data android:name="smallIcon" android:resource="@drawable/small_app_icon" />
        <meta-data android:name="notificationTitle" android:value="Syncing Data" />
        <meta-data android:name="notificationText" android:value="Syncing data across connected devices" />
        <meta-data android:name="notificationId" android:value="1001" />
    </service>
</application>
Make sure you customize the service metadata—it will be displayed to the user in the foreground service notification. Note that your small_app_icon drawable must be monochrome white. The foreground service will be automatically started and stopped on ditto.Sync.Start() and ditto.Sync.Stop(), respectively.

Next Steps

Learn how to build a task list app built on .NET and integrated with Ditto by going to the .NET MAUI or .NET TUI quickstart apps.