\r\n

51Degrees Pipeline .NET  4.2Newer Version 4.3

51Degrees Pipeline for .NET

UsageSharing/Program.cs

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.
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2020 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
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.");
// Wait for user to press a key.
Console.ReadKey();
}
public void RunExample()
{
Console.WriteLine($"Constructing pipeline from configuration file.");
Console.WriteLine();
// Create the configuration object
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
// Bind the configuration to a pipeline options instance
PipelineOptions options = new PipelineOptions();
config.Bind("PipelineOptions", options);
// We need to create an IServiceProvider that will allow
// the PipelineBuilder to create the element builder instances
// it is going to use.
// In this case, a ShareUsageBuilder.
// As ShareUsageBuilder requires an ILoggerFactory and
// an HttpClient instance, we need to add these to the
// service provider.
IServiceProvider serviceProvider = new ServiceCollection()
.AddSingleton<ILoggerFactory>(new LoggerFactory())
.AddSingleton<HttpClient>()
.AddSingleton<ShareUsageBuilder>()
.BuildServiceProvider();
var factory = serviceProvider.GetRequiredService<ILoggerFactory>();
// Create the pipeline using the options object
var pipeline = new PipelineBuilder(factory, serviceProvider)
.BuildFromConfiguration(options);
// Alternatively, the commented code below shows how to
// configure the ShareUsageElement in code, rather than
// using a configuration file.
//var http = new System.Net.Http.HttpClient();
//var sharingElement = new ShareUsageBuilder(factory, http)
// .SetSharePercentage(0.1)
// .SetMinimumEntriesPerMessage(2500)
// .Build();
//var pipeline = new PipelineBuilder(factory)
// .AddFlowElement(sharingElement);
Console.WriteLine($"Pipeline created with share usage " +
$"element, evidence processed with this pipeline will " +
$"now be periodically shared with 51Degrees using " +
$"the specified configuration.");
}
}
}