Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WCF服务获取状态代码_C#_Wcf_Httpresponse - Fatal编程技术网

C#WCF服务获取状态代码

C#WCF服务获取状态代码,c#,wcf,httpresponse,C#,Wcf,Httpresponse,我有一个非常基本的WCF服务,它有一个名为SaveSchoolName(stringschoolname)的方法,如果操作良好,该方法基本上返回布尔值True。 . 我向我的客户端应用程序添加了一个服务引用,正在使用该服务,如下所示: MyService.WebServicesClient svc = new MyService.WebServicesClient(); bool dataSaved = false; dataSaved = svc.SaveSchoolName("School

我有一个非常基本的WCF服务,它有一个名为
SaveSchoolName(stringschoolname)
的方法,如果操作良好,该方法基本上返回布尔值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)。我尝试过搜索,但似乎没有人提供有关如何通过调用该方法获取响应头的详细信息。

您需要为此创建一个客户端消息检查器

请检查下面的代码。。。要使其工作,只需将检查员添加到您的客户。顺便说一句,显然这只适用于HTTP:)

为了扩展,我从实现的
IClientMessageInspector
IEndpointBehavior
开始。我用豪尔赫提供的代码替换了IClientMessageInspector的内容。接收后:

public-void-after-receivereply(ref-Message-reply,object-correlationState)
{
if(reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
{
var httpResponseProperty=(HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
WriteLine($“响应状态为{(int)httpResponseProperty.StatusCode}”);
}
}
现在,您可以选择继续继承
BehaviorExtensionElement
,并将该行为添加到客户端配置文件中。如果你采取这种方法,你会成功的

然而,由于我的客户机配置是以编程方式创建的,所以这里需要一些额外的步骤

在中,您可以看到可以使用
ServiceEndpoint.Behaviors.Add
方法添加端点行为。但是我们如何访问
ServiceEndpoint
对象呢

这可以通过首先创建服务客户端对象,然后使用其
端点
属性来实现,如下所示:

''VB.NET
“”正在创建绑定对象
Dim objMyBinding作为新System.ServiceModel.WSHttpBinding()绑定
objMyBinding.Name=“您的绑定名称”
“”此处添加了绑定的其他详细信息“”
“”正在创建终结点地址obj
Dim objMyEndpoint作为System.ServiceModel.EndpointAddress
objMyEndpoint=New System.ServiceModel.EndpointAddress(新Uri(ENDPOINT\u ADDR\u在此))
“”正在创建服务对象
Dim objWebServices As CalculatorService=新计算器服务(objMyBinding,objMyEndpoint)
“”最终将行为添加到服务端点(CustomEndpointBehavior是IEndpointBehavior的实现)
objWebServices.Endpoint.EndpointBehaviors.Add(新的CustomEndpointBehavior)
///C#.NET
//创建绑定对象
System.ServiceModel.WSHttpBinding objMyBinding=新的System.ServiceModel.WSHttpBinding();
objMyBinding.Name=“您的绑定名称”;
//此处添加了绑定的其他详细信息//
//创建端点地址obj
System.ServiceModel.EndpointAddress objMyEndpoint;
objMyEndpoint=New System.ServiceModel.EndpointAddress(新Uri(此处为ENDPOINT_ADDR_));
//创建服务对象
计算器服务objWebServices=新的计算器服务(objMyBinding,objMyEndpoint);
//最后将行为添加到服务端点(CustomEndpointBehavior是IEndpointBehavior的实现)
添加(新的CustomEndpointBehavior());

当您提到它只适用于HTTP时,您的意思是说它不适用于HTTPS吗?另外,我在哪里调用这个消息检查器?不,我指的是HTTP协议,这意味着它将与HTTP/HTTPS/WebSockets一起工作。至于你如何调用它。。您需要通过端点行为在客户端channelFactory中添加消息检查器。这将在客户端每次收到服务器的回复时自动执行。我对在哪里添加您提供的代码有点困惑。我实际上是在使用代码隐藏的web服务。您需要在客户端上添加消息检查器。您的代码正在使用ClientBase,因此请搜索“添加wcf消息检查器ClientBase”。对于初学者,请阅读以下内容:@JorgeDelConde如果我使用的是svcutil创建的代理,那么我必须添加一个新类-HttpStatusCodeMessageInspector?任何参考资料都会有帮助。我正在看这个-
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;
    }
}