This Example shows how to configure the usage sharing feature of
the Pipeline API. Usage sharing is a feature that allows you to share details
of the requests you are processing with 51Degrees.
Usage sharing works by adding a 'ShareUsageElement' to the pipeline.
This element will collect evidence values as they pass through,
periodically sending them to 51Degrees for processing.
If you want to know more about usage sharing, and why you should
consider using it, see the usage sharing
feature page.
Usage sharing is enabled by default if using a pipeline builder that is derived from FiftyOnePipelineBuilder. For instance, the DeviceDetectionPipelineBuilder. In this example, we show how to add a ShareUsageElement to a Pipeline using configuration.
As with all ElementBuilders, this can also be handled in code, using the ShareUsageBuilder. The commented section in the example demonstrates this.
The appsettings.json file contains all the configuration options. These are all optional, so each can be omitted if the default for that option is sufficient:
{
"PipelineOptions": {
"Elements": [
{
"BuilderName": "ShareUsageBuilder",
"BuildParameters": {
"SharePercentage": 0.1,
"ShareUsageUrl": "https://devices-v4.51degrees.com/new.ashx",
"MinimumEntriesPerMessage": 50,
"RepeatEvidenceIntervalMinutes": 10,
"TrackSession": false,
"AspSessionCookieName": "asp.net_sessionid",
"IncludedQueryStringParameters": "a,b",
"ShareAllQueryStringParameters": false,
"BlockedHttpHeader": "MySecretHeader",
"IgnoreFlowDataEvidenceFilter": "evidenceKey:evidenceValue",
"ShareAllEvidence": false,
"AddTimeout": 5,
"TakeTimeout": 100
}
}
]
}
}
For further details of what each setting does, see the share usage builder reference
This example is available in full on GitHub.
Expected output
Constructing pipeline from configuration file.
Pipeline created with share usage element, evidence processed
with this pipeline will now be shared with 51Degrees using the
specified configuration.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
{
public class Program
{
public static void Main(string[] args)
{
var instance = new Program();
instance.RunExample();
Console.WriteLine("==========================================");
Console.WriteLine("Example complete. Press any key to exit.");
Console.ReadKey();
}
{
Console.WriteLine($"Constructing pipeline from configuration file.");
Console.WriteLine();
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
PipelineOptions options = new PipelineOptions();
config.Bind("PipelineOptions", options);
IServiceProvider serviceProvider = new ServiceCollection()
.AddSingleton<ILoggerFactory>(new LoggerFactory())
.AddSingleton<HttpClient>()
.AddSingleton<ShareUsageBuilder>()
.BuildServiceProvider();
var factory = serviceProvider.GetRequiredService<ILoggerFactory>();
var pipeline = new PipelineBuilder(factory, serviceProvider)
.BuildFromConfiguration(options);
Console.WriteLine($"Pipeline created with share usage " +
$"element, evidence processed with this pipeline will " +
$"now be periodically shared with 51Degrees using " +
$"the specified configuration.");
}
}
}