Squidex
  • Welcome to Squidex
  • Getting Started
    • Squidex Cloud
    • Installation Instructions
      • Platforms
        • Install on Kubernetes
        • Install on AWS (Deprecated)
        • Install on AWS EC2 using Docker
        • Install on Azure
        • Install on Azure using ARM
        • Install on Google Cloud Platform (GCP)
        • Install on Docker
        • Install on Heroku
        • Install on IIS
        • Install on Render
        • Install on Vultr
      • Configuration
        • Deploying the Image Resizer Service
      • Troubleshooting and Support
        • Diagnose Runtime Issues
        • Restoring Deleted Apps
      • Install Identity (Deprecated)
      • External Identity Providers
        • Azure AD (OAuth) with Squidex
    • Contributing and Developing
      • Building
      • Developing
      • Extensions
        • Custom Rule Action
      • Contributing
      • Architecture
      • Translating
      • Squidex Docs Program
    • Roadmap
    • Quick Start Guides
      • Vue.js Blog with Squidex
      • React.js Blog with Squidex
      • Angular Blog with Squidex
  • Documentation
    • Introduction and Use Case
    • Concepts
      • Apps
      • Teams
      • Schemas
        • Field Rules
      • Content
        • Scheduled Publishing
      • Assets
        • Querying Assets
      • Localization
      • Migrations
      • Roles & Permissions
      • Rules
        • Publish an Event to Azure Queues using Rules
        • Populate Elasticsearch Index
      • Backups
      • Subscriptions
        • App Subscriptions v/s Team Subscriptions
      • Notifications
      • Dashboard
      • Workflows
    • Software Development Kits
      • TypeScript
      • .NET
        • Version v14 (and Earlier)
        • Version v15 (and Later)
      • PHP
      • Java
    • Developer Guides
      • API
        • Authentication
        • Postman
        • Queries
        • Assets
      • Automation Tools (CLI)
      • Scripting
        • Scripting Helper Methods
      • Embed Content
      • Custom Workflows
      • Custom Editors
      • Custom Sidebars
      • Preview Content
      • Rule Formatting
        • Simple
        • Script
        • Liquid
      • Tutorials
        • Building a Blog with Squidex and Next.js
  • Next
    • Squidex 3.0: API Compatibility
Powered by GitBook
On this page
  • Introduction
  • Install the SDK
  • Manually Creating Classes
  • Configure Multiple Apps
  • Creating Classes with Dependency Injection
  • Configure Multiple Apps
  • Configure the HTTP pipeline
  • Use Concrete Clients
  • Using Dependency Injection

Was this helpful?

  1. Documentation
  2. Software Development Kits
  3. .NET

Version v14 (and Earlier)

Learn How to Install, Initialize and Create Classes for Version 14 of the SDK

Previous.NETNextVersion v15 (and Later)

Last updated 2 years ago

Was this helpful?

Introduction

The basic concepts of the SDK are documented at the root page, linked below. Please note, they are the same for all versions of this package.

This document focuses on the initialization of the SDK and how to use the concrete client classes.

Install the SDK

The SDK is available on . You can install it with:

dotnet add package Squidex.ClientLibrary

Manually Creating Classes

The main entry class is SquidexClientManager, which handles authentication and creates the actual client classes, where each client is used for one endpoint such as assets, schemas and so on.

The client will create an access token using the App client credentials and cache this token in the memory for 30 days. When the token expires, it is recreated automatically. The cache is not shared between the instances of your application and not needed.

Read more about the authentication flow and best practices below:

To instantiate the client manager, you need the App Name, the Client Id and Client Secret. For self-hosted installations, the URL is also needed. For Squidex Cloud it is https://cloud.squidex.io.

ISquidexClientManager clientManager =
    new SquidexClientManager(
        new SquidexOptions
        {
            AppName = "app",
            ClientId = "id",
            ClientSecret = "secret",
            Url = "https://cloud.squidex.io"
        });

Configure Multiple Apps

The SDK supports multiple Apps using the normal options. When you create a request using an end point client you have to define the App Name, and the client manager picks the correct credentials. When you create a content client, you can also specify the App Name.

ISquidexClientManager clientManager =
    new SquidexClientManager(
        new SquidexOptions
        {
            AppName = "app",
            ClientId = "id",
            ClientSecret = "secret",
            Url = "https://cloud.squidex.io",
            AppCredentials = new Dictionary<string, AppCredentials>
            {
               ["other-website"] = new AppCredentials
               {
                   ClientId = "...",
                   ClientSecret = "...",
               }
            }
        });

Creating Classes with Dependency Injection

dotnet add package Squidex.ClientLibrary.ServiceExtensions

This package provides extension methods to register the client manager at the service collection.

services
    .AddSquidexClient(options =>
    {
        options.AppName = "app";
        options.ClientId = "id";
        options.ClientSecret = "secret";
        options.Url = "https://cloud.squidex.io";
    });

You can inject ISquidexClientManager to your other classes.

services.AddSquidexClient()
    .Configure<SquidexServiceOptions>(options =>
    {
        options.AppName = "app";
        options.ClientId = "id";
        options.ClientSecret = "secret";
        options.Url = "https://cloud.squidex.io";
    });

Another option is to bind it to a configuration section as follows:

services.AddSquidexClient();
services.Configure<SquidexServiceOptions>(
    configuration.GetSection("squidex"));

Configure Multiple Apps

You can also configure multiple Apps using the normal options as follows:

services.AddSquidexClient(options =>
{
    options.AppName = "app";
    options.ClientId = "id";
    options.ClientSecret = "secret";
    options.Url = "https://cloud.squidex.io";
    options.AppCredentials = new Dictionary<string, AppCredentials>
    {
       ["other-app"] = new AppCredentials
       {
           ClientId = "...",
           ClientSecret = "...",
       }
    };
})

Configure the HTTP pipeline

You can make changes to the HTTP pipeline using the following method:

serviceCollection.AddSquidexHttpClient()
   .AddHttpMessageHandler(() =>
   {
       // YOUR CODE
   }).Services

Use Concrete Clients

The classes for concrete endpoints can be created with the client manager. These instances are not cached and a new instance is returned for each call. Therefore, you should keep the instance as a local variable and field, and use them as often as possible.

var assetsClient = client.CreateAssetsClient();

var assets1 = await assetsClient.GetAssetsAsync();

// Just use the client directly.
var assets2 = await client.Assets.GetAssetsAsync();

The content clients are also not cached. They can be created using the following method:

var blog1 = client.CreateContentsClient<BlogPost, BlogPostData>("blog1");
var blog1 = client.CreateContentsClient<BlogPost, BlogPostData>("blog1");

ReferenceEquals(blog1, blog2) == false;

Using Dependency Injection

The endpoint clients are registered in the service locator. Therefore, you can also inject endpoint classes to your service class MyService.

{
    public MyService(IAssetsClient assetsClient)
    {
    }
}

The content clients need parameters to be created. Therefore, you have to register them manually.

services.AddSquidexClient(
    cm => cm.CreateContentsClient<BlogPost, BlogPost>("blog");

class MyService
{
    public MyService(IContentsClient<BlogPost, BlogPost> blog)
    {
    }
}

If you use (especially in ASP.NET Core) you can use the following package:

This configuration uses the , so it can also be configured the following way:

The package also integrates the to implement resilient HTTP requests. For example, this can be used to enable logging or to integrate , a resilience and transient-fault-handling library.

.NET
nuget.org
Authentication
Dependency Injection
Options Pattern
HttpClientFactory
Polly