简体   繁体   English

C# WCF 服务获取状态码

[英]C# WCF Service Get Status Code

I have a very basic WCF service which has a method named SaveSchoolName(string SchoolName) which basically returns as boolean value of True if the operation is good.我有一个非常基本的 WCF 服务,它有一个名为SaveSchoolName(string SchoolName)的方法,如果操作良好,它基本上返回 True 的布尔值。 . . I added a service reference to my client application and is consuming the service as follows:我向我的客户端应用程序添加了一个服务引用,并按如下方式使用该服务:

MyService.WebServicesClient svc = new MyService.WebServicesClient();
bool dataSaved = false;
dataSaved = svc.SaveSchoolName("School Name");

if(dataSaved){
  // do something.
}
else{
  // log not saved.
}

I want to know how do I determine the Http Status Code (200 - OK) for the WCF Service call.我想知道如何确定 WCF 服务调用的 Http 状态代码(200 - OK)。 I have tried to search but none seems to provide any detailed info on how I would be able to get the response headers from invoking the method.我试图进行搜索,但似乎没有提供任何有关如何通过调用该方法获取响应标头的详细信息。

You need to create a client message inspector for this.您需要为此创建一个客户端消息检查器。

Check the below code out ... to make it work just add the inspector to your client.检查下面的代码......使其工作只需将检查器添加到您的客户端。 BTW, obviously this only works for HTTP :)顺便说一句,显然这仅适用于 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;
    }
}

To extend @Jorge's answer , I started by implementing the IClientMessageInspector and IEndpointBehavior from the example here .为了扩展IClientMessageInspector的回答,我首先从这里示例中实现了IClientMessageInspectorIEndpointBehavior I replaced the contents of IClientMessageInspector.AfterReceiveReply with the code provided by Jorge:我用 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}");
    }
}

Now, you can choose to continue inheriting BehaviorExtensionElement and add the behavior to your client configuration file.现在,您可以选择继续继承BehaviorExtensionElement并将BehaviorExtensionElement添加到您的客户端配置文件中。 If you take this approach you'll be done.如果你采用这种方法,你就大功告成了。

However, since my client configuration was created programmatically, I needed a few additional steps here.但是,由于我的客户端配置是以编程方式创建的,因此我需要在此处执行一些额外的步骤。

In the docs you can see that an Endpoint Behavior can be added by using the ServiceEndpoint.Behaviors.Add method.文档中,您可以看到可以使用ServiceEndpoint.Behaviors.Add方法添加端点行为。 But how do we get access to the ServiceEndpoint object?但是我们如何访问ServiceEndpoint对象呢?

This can be done by first creating your service client object, and then using its Endpoint property like so:这可以通过首先创建您的服务客户端对象,然后像这样使用其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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM