如何在 WCF 服务上使用 HealthChecksUI
How to use HealthChecksUI on WCF service
我想知道是否有一些方法可以将 HealthChecks NuGet 添加到 WCF 服务,因为 WCF 服务中没有 Startup.cs 文件,我可以在其中配置此服务。我知道 WCF 服务是 "one big Startup.cs" 但不知道它是如何工作的。
WCF 目前未在 Dotnet Core Framework
中实现。因此,WCF 不支持 class 库。此外,WCF 项目通常托管在 IIS
中,即它是一个虚拟主机。生命周期事件可用于 ServicehostFacotory
扩展。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/extending-hosting-using-servicehostfactory
如果有什么我可以帮忙的,请随时告诉我。
我们必须做一些 json,HealthChecksUI 在 andress 上读取
在我的例子中,它在本地主机上.../Service/json/HealthCheck
并且只检查数据库...但是您可以添加一些代码来测试服务的其他部分
你必须使用 Newtonsoft json 库
所以我这样做了:
我服务:
[OperationContract]
[WebGet(UriTemplate = "/HealthCheck", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
Stream HealthCheck();
Service.svc.cs:
public Stream HealthCheck()
{
var healthChecker = new HealthChecker();
WebOperationContext.Current.OutgoingResponse.ContentType ="application/json;
charset=utf-8";
return new
MemoryStream(Encoding.UTF8.GetBytes(healthChecker.HealthStatus().ToString()));
}
HealthChecker.cs
public class HealthChecker
{
public bool status { get; set; }
public TimeSpan totalDuration { get; set; }
public List<Entry> entries { get; set; }
public HealthChecker()
{
entries = new List<Entry>();
var watch = System.Diagnostics.Stopwatch.StartNew();
TimeSpan time;
string data;
bool dbOk = TestDbs(out time, out data);
var entry = new Entry
{
Type = "database",
data = data,
duration = time,
status = dbOk
};
bool checkStatus = true;
// Prepared in case of more entries (more controlls in one)
foreach (var x in entries)
checkStatus = checkStatus && x.status;
this.status = checkStatus;
this.entries.Add(entry);
watch.Stop();
time += watch.Elapsed;
this.totalDuration = time;
}
private bool TestDbs(out TimeSpan time, out string exception)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.DriverDatabase))
try
{
con.Open();
watch.Stop();
time = watch.Elapsed;
exception = "";
return true;
}
catch (Exception e)
{
exception = e.ToString();
watch.Stop();
time = watch.Elapsed;
return false;
}
}
public JObject HealthStatus()
{
string stringFormat = "hh\:mm\:ss\.fffffff";
var json = new JObject(
new JProperty("status", this.status ? "Healthy" : "Degraded"),
new JProperty("totalDuration", this.totalDuration.ToString(stringFormat)),
new JProperty("entries", new JObject(
new JProperty(this.entries.First().Type,
new JObject(
new JProperty("data",
new JObject(this.entries.First().data != null ? new JProperty("Error", this.entries.First().data) : null)),
new JProperty("duration", this.entries.First().duration.ToString(stringFormat)),
new JProperty("status", this.entries.First().status ? "Healthy" : "Degraded")
)
)
)
)
);
return json;
}
}
Entry.cs
public class Entry
{
public string Type {get; set; }
public string data { get; set; }
public TimeSpan duration { get; set; }
public bool status { get; set; }
}
最后我添加了 webconfig
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="10485760">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="10485760" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="None"/>
</binding>
</basicHttpBinding>
<basicHttpsBinding>
<binding maxReceivedMessageSize="20971520">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="20971520" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="Transport"/>
</binding>
</basicHttpsBinding>
</bindings>
<client/>
<services>
<service name="DriverService.DriverService">
<endpoint name="jsonEP"
address="json"
binding="webHttpBinding"
behaviorConfiguration="json"
contract="DriverService.IDriverService"/>
<endpoint address="" binding="basicHttpBinding" contract="DriverService.IDriverService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="256" maxConcurrentInstances="2147483647"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="json">
<webHttp defaultOutgoingResponseFormat="Json"/>
</behavior>
</endpointBehaviors>
</behaviors>
我想知道是否有一些方法可以将 HealthChecks NuGet 添加到 WCF 服务,因为 WCF 服务中没有 Startup.cs 文件,我可以在其中配置此服务。我知道 WCF 服务是 "one big Startup.cs" 但不知道它是如何工作的。
WCF 目前未在 Dotnet Core Framework
中实现。因此,WCF 不支持 class 库。此外,WCF 项目通常托管在 IIS
中,即它是一个虚拟主机。生命周期事件可用于 ServicehostFacotory
扩展。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/extending-hosting-using-servicehostfactory
如果有什么我可以帮忙的,请随时告诉我。
我们必须做一些 json,HealthChecksUI 在 andress 上读取 在我的例子中,它在本地主机上.../Service/json/HealthCheck
并且只检查数据库...但是您可以添加一些代码来测试服务的其他部分
你必须使用 Newtonsoft json 库
所以我这样做了:
我服务:
[OperationContract]
[WebGet(UriTemplate = "/HealthCheck", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
Stream HealthCheck();
Service.svc.cs:
public Stream HealthCheck()
{
var healthChecker = new HealthChecker();
WebOperationContext.Current.OutgoingResponse.ContentType ="application/json;
charset=utf-8";
return new
MemoryStream(Encoding.UTF8.GetBytes(healthChecker.HealthStatus().ToString()));
}
HealthChecker.cs
public class HealthChecker
{
public bool status { get; set; }
public TimeSpan totalDuration { get; set; }
public List<Entry> entries { get; set; }
public HealthChecker()
{
entries = new List<Entry>();
var watch = System.Diagnostics.Stopwatch.StartNew();
TimeSpan time;
string data;
bool dbOk = TestDbs(out time, out data);
var entry = new Entry
{
Type = "database",
data = data,
duration = time,
status = dbOk
};
bool checkStatus = true;
// Prepared in case of more entries (more controlls in one)
foreach (var x in entries)
checkStatus = checkStatus && x.status;
this.status = checkStatus;
this.entries.Add(entry);
watch.Stop();
time += watch.Elapsed;
this.totalDuration = time;
}
private bool TestDbs(out TimeSpan time, out string exception)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.DriverDatabase))
try
{
con.Open();
watch.Stop();
time = watch.Elapsed;
exception = "";
return true;
}
catch (Exception e)
{
exception = e.ToString();
watch.Stop();
time = watch.Elapsed;
return false;
}
}
public JObject HealthStatus()
{
string stringFormat = "hh\:mm\:ss\.fffffff";
var json = new JObject(
new JProperty("status", this.status ? "Healthy" : "Degraded"),
new JProperty("totalDuration", this.totalDuration.ToString(stringFormat)),
new JProperty("entries", new JObject(
new JProperty(this.entries.First().Type,
new JObject(
new JProperty("data",
new JObject(this.entries.First().data != null ? new JProperty("Error", this.entries.First().data) : null)),
new JProperty("duration", this.entries.First().duration.ToString(stringFormat)),
new JProperty("status", this.entries.First().status ? "Healthy" : "Degraded")
)
)
)
)
);
return json;
}
}
Entry.cs
public class Entry
{
public string Type {get; set; }
public string data { get; set; }
public TimeSpan duration { get; set; }
public bool status { get; set; }
}
最后我添加了 webconfig
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="10485760">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="10485760" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="None"/>
</binding>
</basicHttpBinding>
<basicHttpsBinding>
<binding maxReceivedMessageSize="20971520">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="20971520" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="Transport"/>
</binding>
</basicHttpsBinding>
</bindings>
<client/>
<services>
<service name="DriverService.DriverService">
<endpoint name="jsonEP"
address="json"
binding="webHttpBinding"
behaviorConfiguration="json"
contract="DriverService.IDriverService"/>
<endpoint address="" binding="basicHttpBinding" contract="DriverService.IDriverService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="256" maxConcurrentInstances="2147483647"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="json">
<webHttp defaultOutgoingResponseFormat="Json"/>
</behavior>
</endpointBehaviors>
</behaviors>