Legion Developers

Sharing Knowledge

Connect Salesforce Pub/Sub API with .NET

Pub/Sub API + C#

Hi guys, I want to share how to connect Salesforce Pub/Sub API with .NET.

I was recently working on an integration project where I needed to integrate C# with Salesforce’s pub/sub API, so to solve this requirement I did some research and finally managed to finish the task.

What is Salesforce Pub/Sub API:

Pub/Sub API provides a single interface for publishing and subscribing to platform events, including real-time event monitoring events, and change data capture events. Based on gRPC and HTTP/2, Pub/Sub API efficiently publishes and delivers binary event messages in the Apache Avro format.

Initial Project Configuration

First of all, we need to create a console application on visual studio, once the project is created we are going to install some nuget packages:

  1. Google.Protobuf
  2. Grpc.Net.Client
  3. Grpc.Tools
  4. AvroConvert

For this console app additionally I have used the following packages:

  1. Microsoft.Extensions.DependencyInjection
  2. Microsoft.Extensions.Logging.Console
  3. Newtonsoft.Json

When the packages are installed we are going to configure the project to connect with salesforce pub/sub api using grpc.

We need to review the .csproj file contents, it should content similar configuration to the next snippet.

<PackageReference Include="AvroConvert" Version="3.3.0" />
<PackageReference Include="Google.Protobuf" Version="3.21.12" />
<PackageReference Include="Grpc.Net.Client" Version="2.51.0" />
<PackageReference Include="Grpc.Tools" Version="2.51.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

If the .csproj is correct we can continue with the configuration of the salesforce pub/sub api client, so, we need to download the proto definition of pub/sub api from the official git repository: Proto File

Next, we should create a folder into the project, in my case I created a folder called Protos and I moved the proto file into it.

To add the proto file to our project also we need to declare into the csproj file:

<ItemGroup>
  <Protobuf Include="Protos\pubsub_api.proto" GrpcServices="Client" />
</ItemGroup>

Then we can build the project and the classes for pub/sub client are generated automatically on the obj folder:

Connect Salesforce Pub/Sub API with .NET

In my case building the project, two classes were created PubSubApi.cs and PubSubApiGrpc.cs. With this classes we can connect salesforce pub/sub API with .NET.

In the next part I am going to show you how to connect with salesforce to get access token and then with the auth data connect and consume the salesforce pub/sub API. so, don’t forget to stay tune for the next part on this blog.

Connect Salesforce Pub/Sub API with .NET

4 thoughts on “Connect Salesforce Pub/Sub API with .NET

  1. This is a promising start Juan… I have been looking for someone who would be able to do write this in C# but you’re the only one in the whole wide world to posted anything for .net core. Have you made any progress, would like to know the direction

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top