ServiceModel.Grpc.Client.DependencyInjection
1.11.1
dotnet add package ServiceModel.Grpc.Client.DependencyInjection --version 1.11.1
NuGet\Install-Package ServiceModel.Grpc.Client.DependencyInjection -Version 1.11.1
<PackageReference Include="ServiceModel.Grpc.Client.DependencyInjection" Version="1.11.1" />
paket add ServiceModel.Grpc.Client.DependencyInjection --version 1.11.1
#r "nuget: ServiceModel.Grpc.Client.DependencyInjection, 1.11.1"
// Install ServiceModel.Grpc.Client.DependencyInjection as a Cake Addin #addin nuget:?package=ServiceModel.Grpc.Client.DependencyInjection&version=1.11.1 // Install ServiceModel.Grpc.Client.DependencyInjection as a Cake Tool #tool nuget:?package=ServiceModel.Grpc.Client.DependencyInjection&version=1.11.1
ServiceModel.Grpc.Client.DependencyInjection
ServiceModel.Grpc
enables applications to communicate with gRPC services using a code-first approach (no .proto files), helps to get around limitations of gRPC protocol like "only reference types", "exact one input", "no nulls", "no value-types". Provides exception handling. Helps to migrate existing WCF solution to gRPC with minimum effort.
ServiceModel.Grpc.Client.DependencyInjection
provides client extensions for Microsoft.Extensions.DependencyInjection and Grpc.Net.ClientFactory packages.
Declare a service contract
A service contract is an interface decorated with the ServiceContract
attribute. Interface methods decorated with the OperationContract
attribute are treated as gRPC operations.
For example, following ICalculator
contract with unary Sum
operation and client streaming MultiplyBy
operation:
[ServiceContract]
public interface ICalculator
{
[OperationContract]
Task<long> Sum(long x, int y, int z, CancellationToken token = default);
[OperationContract]
ValueTask<(int Multiplier, IAsyncEnumerable<int> Values)> MultiplyBy(IAsyncEnumerable<int> values, int multiplier, CancellationToken token = default);
}
Configure Grpc.Net.ClientFactory registrations
After registering a gRPC service client by the generic AddGrpcClient method, configure Grpc.Net.ClientFactory to use ServiceModel.Grpc creator:
IServiceCollection services = ...
services
// Grpc.Net.ClientFactory registration
.AddGrpcClient<ICalculator>(options =>
{
options.Address = new Uri("https://localhost:5001");
})
// use ServiceModel.Grpc creator
.ConfigureServiceModelGrpcClientCreator<ICalculator>((options, serviceProvider) =>
{
options.MarshallerFactory = ...
options.ErrorHandler = ...
});
The gRPC service client is registered as transient with dependency injection. The client can now be injected and consumed directly in types created by DI.
IServiceProvider serviceProvider = ...
ICalculator calculator = serviceProvider.GetRequiredService<ICalculator>();
// call Sum: sum == 6
var sum = await calculator.Sum(1, 2, 3);
// call MultiplyBy: multiplier == 2, values == [] {2, 4, 6}
var (multiplier, values) = await calculator.MultiplyBy(new[] {1, 2, 3}, 2);
Register and configure gRPC clients with custom GrpcChannel
IServiceCollection services = ...;
services.AddSingleton<ChannelBase>(GrpcChannel.ForAddress("http://localhost:5000"));
services
.AddServiceModelGrpcClient<ICalculator>((options, serviceProvider) =>
{
options.MarshallerFactory = ...
options.ErrorHandler = ...
});
Links
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Grpc.Net.ClientFactory (>= 2.67.0)
- ServiceModel.Grpc (>= 1.11.1)
-
.NETStandard 2.1
- Grpc.Net.ClientFactory (>= 2.67.0)
- ServiceModel.Grpc (>= 1.11.1)
-
net6.0
- Grpc.Net.ClientFactory (>= 2.67.0)
- ServiceModel.Grpc (>= 1.11.1)
-
net8.0
- Grpc.Net.ClientFactory (>= 2.67.0)
- ServiceModel.Grpc (>= 1.11.1)
-
net9.0
- Grpc.Net.ClientFactory (>= 2.67.0)
- ServiceModel.Grpc (>= 1.11.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.