Utility Methods API for C# SDK

The methods on this page are utility methods that don't fit into other categories.

Request execution

We recommend using try and catch statements when working with the C# SDK.

If there's an issue with the provided API parameter values, like missing a required parameter, the SDK throws an exception. However, if there is a server-side API execution issue or a network problem, the error details are contained within the status.

try
{
PNResult<PNPublishResult> publishResponse = await pubnub.Publish()
.Message("Why do Java developers wear glasses? Because they can't C#.")
.Channel("my_channel")
.ExecuteAsync();

PNStatus status = publishResponse.Status;

Console.WriteLine("Server status code : " + status.StatusCode.ToString());
}
catch (Exception ex)
{
Console.WriteLine($"Request can't be executed due to error: {ex.Message}");
}

Destroy

Destroy frees up the threads and allows for clean exit.

Method(s)

destroy()

Basic Usage

Reference code

This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.


Returns

None

Encrypt

This function allows to encrypt the data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a separate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To encrypt the data you can use the following method(s) in C# SDK.

pubnub.Encrypt(inputString, cipherKey)
* required
ParameterDescription
inputString *
Type: String
The data to encrypt.
cipherKey
Type: String
Cipher key to use for encryption.

Basic Usage

Encrypt part of message


Encrypt File

This function allow to encrypt the file content/data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a separate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To encrypt the file you can use the following method(s) in C# SDK.

pubnub.EncryptFile(sourceFile, destinationFile) 
pubnub.EncryptFile(sourceFile, destinationFile, cipherKey)
* required
ParameterDescription
sourceFile *
Type: String
File to be encrypted.
destinationFile *
Type: String
Path of the encrypted file to be saved.
cipherKey
Type: String
Cipher Key to use for encryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.


This parameter is deprecated and will be removed in a future version. Please use the CryptoModule from PubNub config instead.
byte[] outputBytes = pubnub.EncryptFile(sourceBytes) byte[] outputBytes = pubnub.EncryptFile(sourceBytes, cipherKey)
* required
ParameterDescription
sourceBytes *
Type: byte[]
byte array of the file.
cipherKey
Type: String
Cipher Key to use for encryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.

Basic Usage


Decrypt

This function allows to decrypt the data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a separate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To decrypt the data you can use the following method(s) in C# SDK.

pubnub.Decrypt(inputString, cipherKey)
* required
ParameterDescription
inputString *
Type: String
The data to decrypt.
cipherKey
Type: String
Cipher key to use for encryption.

This parameter is deprecated and will be removed in a future version. Please use the CryptoModule from PubNub config instead.

Basic Usage

Decrypt part of message


Decrypt File

This function allow to decrypt the file content/data.

Method(s)

To decrypt the file you can use the following method(s) in C# SDK.

pubnub.DecryptFile(sourceFile, destinationFile); pubnub.DecryptFile(sourceFile, destinationFile, cipherKey);
* required
ParameterDescription
sourceFile *
Type: String
File to be decrypted.
destinationFile *
Type: String
Path of the decrypted file to be saved.
cipherKey
Type: String
Cipher Key to use for decryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.
byte[] outputBytes = pubnub.DecryptFile(sourceBytes) byte[] outputBytes = pubnub.DecryptFile(sourceBytes, cipherKey)
* required
ParameterDescription
sourceBytes *
Type: byte[]
byte array of the file.
cipherKey
Type: String
Cipher Key to use for decryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.

Basic Usage


Disconnect

Call the Disconnect method to force the SDK to stop all requests to PubNub server when there are active subscribe channels.

Method(s)

To disconnect the data transmission you can use the following method(s) in C# SDK.

Disconnect<T>()

This method doesn't take any arguments.

Basic Usage


Get Subscribed Channel Groups

Returns all the subscribed channel groups in a List of type String.

Method(s)

To Get Subscribe Channel Groups you can use the following method(s) in the C# SDK:

List<string> GetSubscribedChannelGroups()

Basic Usage

Get Subscribed Channel Groups


Response

List<String>

["channelGroup1", "channelGroup2"]

Get Subscribed Channels

Returns all the subscribed channels in a List of type String.

Method(s)

To Get Subscribed Channels you can use the following method(s) in the C# SDK:

List<string> GetSubscribedChannels()

Basic Usage

Get Subscribed Channels


Response

List<String>

["channel1", "channel2"]

Reconnect

Call the reconnect method to force the SDK to try and reach out PubNub.

Method(s)

To reconnect the data you can use the following method(s) in C# SDK.

Reconnect<T>(bool resetSubscribeToken)
* required
ParameterDescription
resetSubscribeToken
Type: bool
Passing true will send zero timetoken upon reconnect.

Basic Usage


Last updated on