STAIExtensions.Host.Grpc.Client 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package STAIExtensions.Host.Grpc.Client --version 1.0.2
NuGet\Install-Package STAIExtensions.Host.Grpc.Client -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="STAIExtensions.Host.Grpc.Client" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add STAIExtensions.Host.Grpc.Client --version 1.0.2
#r "nuget: STAIExtensions.Host.Grpc.Client, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install STAIExtensions.Host.Grpc.Client as a Cake Addin
#addin nuget:?package=STAIExtensions.Host.Grpc.Client&version=1.0.2

// Install STAIExtensions.Host.Grpc.Client as a Cake Tool
#tool nuget:?package=STAIExtensions.Host.Grpc.Client&version=1.0.2

STAIExtensions Host Grpc Client

This library contains a default .NET Protobuf Client. It also contains a managed wrapper to ease development of handling the connections and callbacks from and to the Grpc Server.

GitHub Workflow Status

Usage

To use the library in a .NET project, install the package from Nuget. Once installed, either the managed client or the built in generated Protobuf client can be used as both are exposed.

The one parameter to take note of is the Owner Id parameter used throughout the lifetime of this library. The object instantiating this class should create a unique value for the owner (or User) to be passed to the backend. This will ensure that the callbacks when views are updated are returned to the correct instance of the Grpc client. A suggested approach is to either generate a new Guid for the lifespan of the connection or use a user name for this value.

It should also be noted that when a user name is used for the owner and a view's parameters are set this will update the view globally. These changes will propagate to all other retrievals like the API and SignalR GetView data.

General Flow

For the general flow of constructing views and attaching it to datasets, see the main project readme documentation.

Generated Client

For more control over the process, the generated client can be used to connect to the host Grpc Server. The generated client exposes both Async and Sync calls.

Example Code:
using Grpc.Net.Client;

...

private STAIExtensionsGrpcService.STAIExtensionsGrpcServiceClient _client;

public void Connect()
{
    var channel = GrpcChannel.ForAddress("https://localhost:1234");
    _client = new STAIExtensionsGrpcService.STAIExtensionsGrpcServiceClient(channel);
}

public void GetView(string viewId, string ownerId, CancellationToken cancellationToken = default)
{
    var response = this._client.GetView(new GetViewRequest()
    {
        OwnerId = ownerId,
        ViewId = viewId
    }, null, null, cancellationToken);
    // Do something with the response        
}
...

Generated Client Methods:
Method Async Available Description
AttachViewToDataset yes Attaches a view to a dataset for updates
CreateView yes Creates a new view with the specified type name
DetachViewFromDataset yes Detaches the view from the dataset
GetMyViews yes Get the views linked to the owner Id
GetRegisteredViews yes Load all the types of views that can be created
GetView yes Gets the view in it's current state. Due to the message type only the common properties are returned
GetViewJson yes Gets the view in a Json format with all defined properties
ListDataSets yes Lists the currently running datasets that views can be attached to
OnDataSetUpdated yes Callback stream that notifies the client that a dataset has updated
OnDataViewUpdated yes Callback stream that notifies the client that a view belonging to the owner has been updated
RegisterConnectionState yes Callback stream to monitor the streaming connection states. The server will stream the server time in UTC every second
RemoveView yes Removes a view from all datasets and disposes it
SetViewAutoRefreshDisabled yes Pauses the view updates on a view
SetViewAutoRefreshEnabled yes Resumes the view updates on a view
SetViewParameters yes Sets the view parameters. The allowed parameters can be found on the view. The Rpc call takes a value in the format of a Dictionary<string, object> Json payload

Managed Client

The managed client is a wrapper around the common functions and is there to assist with the base functions and setup. As part of the options it also allows the full option set of the GrpcChannelOptions to be passed along. Another option exposed is the UseHttp2UnencryptedSupport that adds legacy support for Grpc.

Additionally to the basic setup and configuration, this managed client allows for the default authorization to be injected as part of the options. This will use a Bearer token that will be sent across in the header metadata and validated against the server. The server host and the client bearer token must be the same.

The managed client will also automatically watch the streaming connection states and attempt to reconnect the channel and client when a connection drop is noticed. This might work fine in some cases, but when the hosting application restarted, the Views that were created during this session will not exist and have to be recreated.

Example Code:

Note: The example below just waits for the connection to establish before continuing with the rest of the operations. A better practice would be to attach an event to the OnConnectionStateChanged event and drive the process from there.


public async Task SetupManagedClient()
{
    using var managedClient =
        new Host.Grpc.Client.GrpcClientManaged(new GrpcClientManagedOptions("https://localhost:44309", "ABC")
        {
            UseDefaultAuthorization = true,
            AuthBearerToken = "SameAsServerConfiguration"
        });
    
    // Wait for the connection
    while (managedClient.ConnectionState != ConnectionState.Connected)
    {
        await Task.Delay(200);
    }
    
    managedClient.OnDataSetUpdated += (sender, id) =>
    {
        Console.WriteLine($"DataSet object with Id {id} has been updated");
    };

    managedClient.OnDataSetViewUpdated += (sender, id) =>
    {
        Console.WriteLine($"DataView object with Id {id} has been updated");
    };
    
    managedClient.OnDataSetViewUpdatedJson += (sender, e) =>
    {
        Console.WriteLine($"DataView object with Id {e.ViewId} has been updated to Json: {e.Payload}");
    };
    
    var view = await managedClient.CreateViewAsync(
        "STAIExtensions.Default.Views.BrowserTimingsView, STAIExtensions.Default, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
    ....
    
}

Managed Client Methods:

All the calls via the managed client are Asynchronous. The callback stream channels are handled in the managed client and will raise the appropriate events that can be attached to.

Method Description
AttachViewToDatasetAsync Attaches a view to a dataset for updates
CreateViewAsync Creates a new view with the specified type name
DetachViewFromDatasetAsync Detaches the view from the dataset
GetMyViewsAsync Get the views linked to the owner Id
GetRegisteredViewsAsync Load all the types of views that can be created
GetViewAsync Gets the view in it's current state. Due to the message type only the common properties are returned
GetViewJsonAsync Gets the view in a Json format with all defined properties
ListDataSetsAsync Lists the currently running datasets that views can be attached to
RemoveViewAsync Removes a view from all datasets and disposes it
SetViewAutoRefreshDisabledAsync Pauses the view updates on a view
SetViewAutoRefreshEnabledAsync Resumes the view updates on a view
SetViewParametersAsync Sets the view parameters. The allowed parameters can be found on the view. The Rpc call takes a value in the format of a Dictionary<string, object> Json payload
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 470 1/27/2022
1.0.3 401 1/18/2022
1.0.2 259 1/6/2022
1.0.1 245 1/6/2022

v1.0.2 - Added package Readme
           v1.0.1 - Added package Icon
           v1.0.0 - Initial Release