Skip to content

Add Dapr.Bindings project #1517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: release-1.16
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions all.sln
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Actors.Analyzers.Test"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Analyzers.Common", "test\Dapr.Analyzers.Common\Dapr.Analyzers.Common.csproj", "{7E23E229-6823-4D84-AF3A-AE14CEAEF52A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Bindings", "src\Dapr.Bindings\Dapr.Bindings.csproj", "{ACE88DA3-AFDC-478D-B3D9-B06460C64D2F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Bindings.Test", "test\Dapr.Bindings.Test\Dapr.Bindings.Test.csproj", "{A8A37DBD-8EB1-4346-9A0D-235403564355}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -445,6 +449,14 @@ Global
{7E23E229-6823-4D84-AF3A-AE14CEAEF52A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E23E229-6823-4D84-AF3A-AE14CEAEF52A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E23E229-6823-4D84-AF3A-AE14CEAEF52A}.Release|Any CPU.Build.0 = Release|Any CPU
{ACE88DA3-AFDC-478D-B3D9-B06460C64D2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACE88DA3-AFDC-478D-B3D9-B06460C64D2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACE88DA3-AFDC-478D-B3D9-B06460C64D2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACE88DA3-AFDC-478D-B3D9-B06460C64D2F}.Release|Any CPU.Build.0 = Release|Any CPU
{A8A37DBD-8EB1-4346-9A0D-235403564355}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8A37DBD-8EB1-4346-9A0D-235403564355}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8A37DBD-8EB1-4346-9A0D-235403564355}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8A37DBD-8EB1-4346-9A0D-235403564355}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -525,6 +537,8 @@ Global
{E49C822C-E921-48DF-897B-3E603CA596D2} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
{A2C0F203-11FF-4B7F-A94F-B9FD873573FE} = {DD020B34-460F-455F-8D17-CF4A949F100B}
{7E23E229-6823-4D84-AF3A-AE14CEAEF52A} = {DD020B34-460F-455F-8D17-CF4A949F100B}
{ACE88DA3-AFDC-478D-B3D9-B06460C64D2F} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
{A8A37DBD-8EB1-4346-9A0D-235403564355} = {DD020B34-460F-455F-8D17-CF4A949F100B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40}
Expand Down
38 changes: 38 additions & 0 deletions src/Dapr.Bindings/Dapr.Bindings.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>Dapr.Bindings</PackageId>
<Title>Dapr Bindings SDK</Title>
<Description>Used to engage with the Dapr bindings building block.</Description>
<VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf"/>
<PackageReference Include="Grpc.Net.Client"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions"/>
<PackageReference Include="Microsoft.Extensions.Http"/>
<PackageReference Include="Microsoft.Extensions.Configuration"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dapr.Common\Dapr.Common.csproj"/>
<ProjectReference Include="..\Dapr.Protos\Dapr.Protos.csproj"/>
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="$(AssemblyName).Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b1f597635c44597fcecb493e2b1327033b29b1a98ac956a1a538664b68f87d45fbaada0438a15a6265e62864947cc067d8da3a7d93c5eb2fcbb850e396c8684dba74ea477d82a1bbb18932c0efb30b64ff1677f85ae833818707ac8b49ad8062ca01d2c89d8ab1843ae73e8ba9649cd28666b539444dcdee3639f95e2a099bb2"/>
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.Abstractions">
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.14\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.14\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
146 changes: 146 additions & 0 deletions src/Dapr.Bindings/DaprBindingsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// ------------------------------------------------------------------------
// Copyright 2025 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Text.Json;
using Dapr.Bindings.Models;
using Dapr.Common;
using Autogenerated = Dapr.Client.Autogen.Grpc.v1.Dapr;

namespace Dapr.Bindings;

/// <summary>
/// <para>
/// Defines client operations for managing Dapr jobs.
/// Register the <see cref="DaprBindingsClient"/> for use via dependency injection with
/// <see><cref>DaprJobsServiceCollectionExtensions.AddDaprJobsClient</cref></see>.
/// </para>
/// <para>
/// Implementations of <see cref="DaprBindingsClient"/> implement <see cref="IDisposable"/> because the
/// client accesses network resources. For best performance, create a single long-lived client instance
/// and share it for the lifetime of the application. This is done for you if created via the DI extensions. Avoid
/// creating a disposing a client instance for each operation that the application performs - this can lead to socket
/// exhaustion and other problems.
/// </para>
/// </summary>
public abstract class DaprBindingsClient(
Autogenerated.DaprClient client,
HttpClient httpClient,
JsonSerializerOptions jsonSerializerOptions,
string? daprApiToken = null) : IDaprClient
{
private bool disposed;

/// <summary>
/// The HTTP client used by the client for calling the Dapr runtime.
/// </summary>
/// <remarks>
/// Property exposed for testing purposes.
/// </remarks>
internal readonly HttpClient HttpClient = httpClient;

/// <summary>
/// The Dapr API token value.
/// </summary>
/// <remarks>
/// Property exposed for testing purposes.
/// </remarks>
internal readonly string? DaprApiToken = daprApiToken;

/// <summary>
/// The autogenerated Dapr client.
/// </summary>
/// <remarks>
/// Property exposed for testing purposes.
/// </remarks>
internal Autogenerated.DaprClient Client { get; } = client;

/// <summary>
/// The JSON serializer options.
/// </summary>
/// <remarks>
/// Property exposed for testing purposes.
/// </remarks>
internal JsonSerializerOptions JsonSerializerOptions { get; } = jsonSerializerOptions;

/// <summary>
/// Invokes an output binding.
/// </summary>
/// <typeparam name="TRequest">The type of the data that will be JSON serialized and provided as the binding payload.</typeparam>
/// <param name="bindingName">The name of the binding to sent the event to.</param>
/// <param name="operation">The type of operation to perform on the binding.</param>
/// <param name="data">The data that will be JSON serialized and provided as the binding payload.</param>
/// <param name="metadata">A collection of metadata key-value pairs that will be provided to the binding. The valid metadata keys and values are determined by the type of binding used.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns>
public abstract Task InvokeBindingAsync<TRequest>(
string bindingName,
string operation,
TRequest data,
IReadOnlyDictionary<string, string>? metadata = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Invokes an output binding.
/// </summary>
/// <typeparam name="TRequest">The type of the data that will be JSON serialized and provided as the binding payload.</typeparam>
/// <typeparam name="TResponse">The type of the data that will be JSON deserialized from the binding response.</typeparam>
/// <param name="bindingName">The name of the binding to sent the event to.</param>
/// <param name="operation">The type of operation to perform on the binding.</param>
/// <param name="data">The data that will be JSON serialized and provided as the binding payload.</param>
/// <param name="metadata">A collection of metadata key-value pairs that will be provided to the binding. The valid metadata keys and values are determined by the type of binding used.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task{T}" /> that will complete when the operation has completed.</returns>
public abstract Task<TResponse?> InvokeBindingAsync<TRequest, TResponse>(
string bindingName,
string operation,
TRequest data,
IReadOnlyDictionary<string, string>? metadata = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Invokes a binding with the provided <paramref name="request" />. This method allows for control of the binding
/// input and output using raw bytes.
/// </summary>
/// <param name="request">The <see cref="DaprBindingRequest" /> to send.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task{T}" /> that will complete when the operation has completed.</returns>
public abstract Task<DaprBindingResponse> InvokeBindingAsync(DaprBindingRequest request, CancellationToken cancellationToken = default);

internal static KeyValuePair<string, string>? GetDaprApiTokenHeader(string apiToken)
{
if (string.IsNullOrWhiteSpace(apiToken))
{
return null;
}

return new KeyValuePair<string, string>("dapr-api-token", apiToken);
}

/// <inheritdoc />
public void Dispose()
{
if (!this.disposed)
{
Dispose(disposing: true);
this.disposed = true;
}
}

/// <summary>
/// Disposes the resources associated with the object.
/// </summary>
/// <param name="disposing"><c>true</c> if called by a call to the <c>Dispose</c> method; otherwise false.</param>
protected virtual void Dispose(bool disposing)
{
}
}
39 changes: 39 additions & 0 deletions src/Dapr.Bindings/DaprBindingsClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ------------------------------------------------------------------------
// Copyright 2025 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using Dapr.Common;
using Microsoft.Extensions.Configuration;
using Autogenerated = Dapr.Client.Autogen.Grpc.v1;

namespace Dapr.Bindings;

/// <summary>
/// Builds a <see cref="DaprBindingsClientBuilder"/>.
/// </summary>
/// <param name="configuration">An optional instance of <see cref="IConfiguration"/>.</param>
public sealed class DaprBindingsClientBuilder(IConfiguration? configuration = null) : DaprGenericClientBuilder<DaprBindingsClient>(configuration)
{
/// <summary>
/// Builds the client instance from the properties of the builder.
/// </summary>
/// <returns>The Dapr client instance.</returns>
/// <summary>
/// Builds the client instance from the properties of the builder.
/// </summary>
public override DaprBindingsClient Build()
{
var daprClientDependencies = this.BuildDaprClientDependencies(typeof(DaprBindingsClient).Assembly);
var client = new Autogenerated.Dapr.DaprClient(daprClientDependencies.channel);
return new DaprBindingsGrpcClient(client, daprClientDependencies.httpClient, this.JsonSerializerOptions, daprClientDependencies.daprApiToken);
}
}
Loading
Loading