Installing .NET SDK for Data Storage and Instant Messaging
The .NET SDK is based on .NET Standard 2.0. It supports the following frameworks:
- Unity 2018.1+
- .NET Core 2.0+
- .NET Framework 4.6.1+
- Mono 5.4+
See .NET Standard for more frameworks supported.
Installing SDK
First of all, download the latest SDK from our GitHub Releases.
The table below shows the modules included in the SDK as well as their dependencies:
Name | Description |
---|---|
LeanCloud-SDK-Storage | For the Data Storage service. |
LeanCloud-SDK-Realtime | For Instant Messaging and LiveQuery. Depends on the Data Storage service. |
LeanCloud-SDK-Engine | For Cloud Engine. Depends on the Data Storage service and works on the server side environment of Cloud Engine. |
You can download only what you need to reduce the size of your app.
For Unity Projects
Direct import: Download LeanCloud-SDK-XXX-Unity.zip, unzip it as a
Plugins
folder, and drag and drop the folder into Unity.UPM: Add the following dependencies into
Packages/manifest.json
in your project:"dependencies": {
"com.leancloud.storage": "https://github.com/leancloud/csharp-sdk-upm.git#storage-2.3.0",
"com.leancloud.realtime": "https://github.com/leancloud/csharp-sdk-upm.git#realtime-2.3.0"
}
Note: Only Unity 2018+ is supported (Unity Api Compatibility Level being .NET Standard 2.0).
For Other Projects
For .NET Core and other projects supporting .NET Standard 2.0, please download LeanCloud-SDK-XXX-Standard.zip, unzip it, and set dependencies accordingly (XXX means the cloud services you are using, like Storage, Realtime (which includes LiveQuery), and Engine).
Initializing Your Project
Import the modules:
// The basics
using LeanCloud;
// Data Storage
using LeanCloud.Storage;
// Instant Messaging (optional)
using LeanCloud.Realtime;
// LiveQuery (optional)
using LeanCloud.LiveQuery;
Before using the service, initialize the SDK with the following code:
LCApplication.Initialize("your-client-id", "your-client-token", "https://your_server_url");
Credentials
You can view the credentials of your app by going to Developer Center > Your game > Game Services > Configuration:
- Client ID, also called
App ID
, will be used when you initialize the SDK. - Client Token, also called
App Key
, will be used when you initialize the SDK on the client side. - Server Secret, also called
Master Key
, will be used when you interact with admin interfaces on trusted environments including your own server and Cloud Engine. When you use it, all the permission checks will be skipped. Make sure to keep it private and never use it in the code for the client.
See Domain for setting up the domain.
Domain
To use TDS cloud services, you must set server_url
with the API domain when initializing the SDK. You can obtain a shared domain provided by TDS by going to Developer Center > Your Game > Game Services > Configuration > Domain.
Enabling Debug Logs
You can easily trace the problems in your project by turning debug logs on during the development phase. Once enabled, details of every request made by the SDK along with errors will be output to your IDE, your browser console, or your Cloud Engine instances’ logs.
- Unity
- .NET
LCLogger.LogDelegate = (LCLogLevel level, string info) => {
switch (level) {
case LCLogLevel.Debug:
Debug.Log($"[DEBUG] {DateTime.Now} {info}\n");
break;
case LCLogLevel.Warn:
Debug.Log($"[WARNING] {DateTime.Now} {info}\n");
break;
case LCLogLevel.Error:
Debug.Log($"[ERROR] {DateTime.Now} {info}\n");
break;
default:
Debug.Log(info);
break;
}
}
LCLogger.LogDelegate = (LCLogLevel level, string info) => {
switch (level) {
case LCLogLevel.Debug:
TestContext.Out.WriteLine($"[DEBUG] {DateTime.Now} {info}\n");
break;
case LCLogLevel.Warn:
TestContext.Out.WriteLine($"[WARNING] {DateTime.Now} {info}\n");
break;
case LCLogLevel.Error:
TestContext.Out.WriteLine($"[ERROR] {DateTime.Now} {info}\n");
break;
default:
TestContext.Out.WriteLine(info);
break;
}
}
Make sure debug logs are turned off before your app is published. Failure to do so may lead to the exposure of sensitive data.
Verifying
First of all, make sure you are able to connect to the server from your computer. You can test it by running the following command:
curl "https://{{host}}/1.1/date"
{{host}}
is the custom API domain.
If everything goes well, it will return the current date:
{ "__type": "Date", "iso": "2020-10-12T06:46:56.000Z" }
Now add the following code to your project:
LCObject testObject = new LCObject("TestObject");
testObject["words"] = "Hello world!";
await testObject.Save();
Save and run your program.
Then go to Developer Center > Your game > Game Services > Cloud Services > Data Storage > Data > TestObject
. If you see a row with its words
column being Hello world!
, it means that you have correctly installed the SDK.
See Debugging if you’re not seeing the content.
Debugging
This guide is written for the latest version of our SDK. If you encounter any errors, please first make sure you have the latest version installed.
401 Unauthorized
If you get a 401
error or see the following content in network logs:
{
"code": 401,
"error": "Unauthorized."
}
It means that the App ID or App Key might be incorrect or don’t match. If you have multiple apps, you might have used the App ID of one app with the App Key of another one, which will lead to such an error.
The Client Cannot Access the Internet
Make sure you have granted the required permissions to your mobile app.