/
ServiceUriBuilder.cs
55 lines (47 loc) · 1.93 KB
/
ServiceUriBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
namespace HealthMetrics.Common
{
using System;
using System.Fabric;
public class ServiceUriBuilder
{
public ServiceUriBuilder(string serviceInstance)
{
this.ServiceInstance = serviceInstance;
}
public ServiceUriBuilder(string applicationInstance, string serviceInstance)
{
this.ApplicationInstance = applicationInstance;
this.ServiceInstance = serviceInstance;
}
/// <summary>
/// The name of the application instance that contains he service.
/// </summary>
public string ApplicationInstance { get; set; }
/// <summary>
/// The name of the service instance.
/// </summary>
public string ServiceInstance { get; set; }
public Uri ToUri()
{
string applicationInstance = this.ApplicationInstance;
if (String.IsNullOrEmpty(applicationInstance))
{
try
{
// the ApplicationName property here automatically prepends "fabric:/" for us
applicationInstance = FabricRuntime.GetActivationContext().ApplicationName.Replace("fabric:/", String.Empty);
}
catch (InvalidOperationException)
{
// FabricRuntime is not available.
// This indicates that this is being called from somewhere outside the Service Fabric cluster.
}
}
return new Uri("fabric:/" + applicationInstance + "/" + this.ServiceInstance);
}
}
}