我有一个非常基本的WCF服务,它有一个名为SaveSchoolName(string SchoolName)
的方法,如果操作良好,它基本上返回布尔值为True。
。
我在我的客户端应用程序中添加了一个服务引用,并按以下方式使用该服务:
MyService.WebServicesClient svc = new MyService.WebServicesClient();
bool dataSaved = false;
dataSaved = svc.SaveSchoolName("School Name");
if(dataSaved){
// do something.
}
else{
// log not saved.
}
我想知道如何确定WCF服务调用的Http状态代码(200 - OK)。我试图搜索但似乎没有提供任何有关如何通过调用方法获取响应头的详细信息。
答案 0 :(得分:0)
您需要为此创建一个客户端消息检查器。
检查以下代码...使其工作只需将检查器添加到您的客户端。 BTW,显然这仅适用于HTTP:)
public class HttpStatusCodeMessageInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
if (reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
{
var httpResponseProperty = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
Console.WriteLine($"Response status is {(int)httpResponseProperty.StatusCode}");
}
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
}
答案 1 :(得分:0)
为了扩展 @Jorge's answer,我首先实现了 example here 中的 IClientMessageInspector
和 IEndpointBehavior
。我用 Jorge 提供的代码替换了 IClientMessageInspector.AfterReceiveReply
的内容:
public void AfterReceiveReply(ref Message reply, object correlationState)
{
if (reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
{
var httpResponseProperty = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
Console.WriteLine($"Response status is {(int)httpResponseProperty.StatusCode}");
}
}
现在,您可以选择继续继承 BehaviorExtensionElement
并将行为添加到您的客户端配置文件中。如果你采用这种方法,你就大功告成了。
但是,由于我的客户端配置是以编程方式创建的,因此我需要在此处执行一些额外的步骤。
在 docs 中,您可以看到可以使用 ServiceEndpoint.Behaviors.Add
方法添加端点行为。但是我们如何访问 ServiceEndpoint
对象?
这可以通过首先创建您的服务客户端对象,然后像这样使用其 Endpoint
属性来完成:
''' VB.NET
'' Creating the binding obj
Dim objMyBinding As New System.ServiceModel.WSHttpBinding()
objMyBinding.Name = "YOUR_BINDING_NAME"
'' Other details of binding added here ''
'' Creating the endpoint address obj
Dim objMyEndpoint As System.ServiceModel.EndpointAddress
objMyEndpoint = New System.ServiceModel.EndpointAddress(New Uri(ENDPOINT_ADDR_HERE))
'' Creating the Service obj
Dim objWebServices As CalculatorService = New CalculatorService(objMyBinding, objMyEndpoint)
'' Finally adding the behavior to the Service Endpoint (CustomEndpointBehavior is implementation of IEndpointBehavior)
objWebServices.Endpoint.EndpointBehaviors.Add(New CustomEndpointBehavior)
/// C#.NET
// Creating the binding obj
System.ServiceModel.WSHttpBinding objMyBinding = New System.ServiceModel.WSHttpBinding();
objMyBinding.Name = "YOUR_BINDING_NAME";
// Other details of binding added here //
// Creating the endpoint address obj
System.ServiceModel.EndpointAddress objMyEndpoint;
objMyEndpoint = New System.ServiceModel.EndpointAddress(New Uri(ENDPOINT_ADDR_HERE));
// Creating the Service obj
CalculatorService objWebServices = New CalculatorService(objMyBinding, objMyEndpoint);
// Finally adding the behavior to the Service Endpoint (CustomEndpointBehavior is implementation of IEndpointBehavior)
objWebServices.Endpoint.EndpointBehaviors.Add(New CustomEndpointBehavior());