---
layout: Conceptual
title: Reference - .NET SDK for Azure Web PubSub | Azure Docs
canonicalUrl: https://docs.azure.cn/en-us/azure-web-pubsub/reference-server-sdk-csharp
breadcrumb_path: /bread/toc.json
uhfHeaderId: mooncake
recommendations: false
description: This reference describes the .NET SDK for the Azure Web PubSub service.
ms.service: azure-web-pubsub
ms.custom: devx-track-dotnet
ms.topic: conceptual
origin.date: 2021-11-11T00:00:00.0000000Z
author: WendiZ
ms.date: 2025-12-15T00:00:00.0000000Z
ms.author: v-wendyzhang
locale: en-us
document_id: aa88dba4-2384-cad5-b9e9-12906aae3720
document_version_independent_id: bcefc7ed-365a-660d-42ae-7bc1735f4d6a
updated_at: 2025-12-12T08:32:00.0000000Z
original_content_git_url: https://github.com/MicrosoftDocs/mc-docs-pr/blob/live/articles/azure-web-pubsub/reference-server-sdk-csharp.md
gitcommit: https://github.com/MicrosoftDocs/mc-docs-pr/blob/d2679c596a597f82d6d70ce4c918cc9636d7e4d0/articles/azure-web-pubsub/reference-server-sdk-csharp.md
git_commit_id: d2679c596a597f82d6d70ce4c918cc9636d7e4d0
site_name: DocsAzureCN
depot_name: Azure.mooncake-docs
page_type: conceptual
toc_rel: toc.json
feedback_system: None
feedback_product_url: ''
feedback_help_link_type: ''
feedback_help_link_url: ''
word_count: 364
asset_id: azure-web-pubsub/reference-server-sdk-csharp
moniker_range_name: 
monikers: []
item_type: Content
source_path: articles/azure-web-pubsub/reference-server-sdk-csharp.md
cmProducts:
- https://authoring-docs-microsoft.poolparty.biz/devrel/04137f57-aca0-4c2b-a1cd-a682fbcb60da
- https://authoring-docs-microsoft.poolparty.biz/devrel/7696cda6-0510-47f6-8302-71bb5d2e28cf
spProducts:
- https://authoring-docs-microsoft.poolparty.biz/devrel/2cb360d5-349b-44d8-bf04-4fdbc9d00189
- https://authoring-docs-microsoft.poolparty.biz/devrel/69c76c32-967e-4c65-b89a-74cc527db725
platformId: c8c21df9-3422-0942-3624-661a32602584
---

# Reference - .NET SDK for Azure Web PubSub | Azure Docs

[Azure Web PubSub Service](./) is an Azure-managed service that helps developers easily build web applications with real-time features and publish-subscribe pattern. Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests can also use Azure Web PubSub service.

You can use this library in your app server side to manage the WebSocket client connections, as shown in below diagram:

![The overflow diagram shows the overflow of using the service client library.](media/sdk-reference/service-client-overflow.png)

Use this library to:

- Send messages to hubs and groups.
- Send messages to particular users and connections.
- Organize users and connections into groups.
- Close connections
- Grant, revoke, and check permissions for an existing connection

[Source code](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Azure.Messaging.WebPubSub/src) | [Package](https://www.nuget.org/packages/Azure.Messaging.WebPubSub) | [API reference documentation](https://learn.microsoft.com/dotnet/api/overview/azure/messaging.webpubsub-readme) | [Product documentation](./) | [Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/)

## Getting started

### Install the package

Install the client library from [NuGet](https://www.nuget.org/):

```dotnetcli
dotnet add package Azure.Messaging.WebPubSub
```

### Prerequisites

- An [Azure subscription](https://account.windowsazure.cn/organization).
- An existing Azure Web PubSub service instance.

### Create and authenticate a `WebPubSubServiceClient`

In order to interact with the service, you'll need to create an instance of the `WebPubSubServiceClient` class. To make this possible, you'll need the connection string or a key, which you can access in the Azure portal.

```C
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
```

## Examples

### Broadcast a text message to all clients

```C
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));

serviceClient.SendToAll("Hello World!");
```

### Broadcast a JSON message to all clients

```C
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));

serviceClient.SendToAll(RequestContent.Create(
        new
        {
            Foo = "Hello World!",
            Bar = 42
        }),
        ContentType.ApplicationJson);
```

### Broadcast a binary message to all clients

```C
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));

Stream stream = BinaryData.FromString("Hello World!").ToStream();
serviceClient.SendToAll(RequestContent.Create(stream), ContentType.ApplicationOctetStream);
```

## Troubleshooting

### Setting up console logging

You can also [enable console logging](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md#logging) if you want to dig deeper into the requests you're making against the service.