使用WebFaultException进行WCF错误管理-错误的HttpStatusCode

使用WebFaultException进行WCF错误管理-错误的HttpStatusCode,wcf,soap,error-handling,Wcf,Soap,Error Handling,我已经实现了一个简单的web服务,我希望根据调用的方法中发生的错误返回适当的Http状态错误代码 目前,我正在使用Visual Studio 2019中的.NET framework 4.7.2和内置在Visual Studio中的IIS Express 10进行测试。我正在使用Chrome的回力棒分机呼叫服务 我实现了一个采用名称的FindPerson方法。如果找不到此人,或者找到了多个人,我想返回一个“未找到”响应(404) 我实现了一个简单的servicererror类,用于抛出WebFa

我已经实现了一个简单的web服务,我希望根据调用的方法中发生的错误返回适当的Http状态错误代码

目前,我正在使用Visual Studio 2019中的.NET framework 4.7.2和内置在Visual Studio中的IIS Express 10进行测试。我正在使用Chrome的回力棒分机呼叫服务

我实现了一个采用名称的
FindPerson
方法。如果找不到此人,或者找到了多个人,我想返回一个“未找到”响应(404)

我实现了一个简单的
servicererror
类,用于抛出
WebFaultException
以及未找到的错误代码。当我抛出
WebFaultException
时,相应的故障响应被发送到使用者(我看到了问题的详细信息)但是http状态是500(内部服务错误),而不是我使用的404错误(预期会收到)

下面是我简单的
FindPerson
方法:

Private Function FindPerson(ByVal name As String) As Person Implements MyService.FindPerson

    Dim foundPerson As Person = Nothing
    Dim people = GetPeople(name)
    Select Case people.Count
        Case Is > 1
            Throw New WebFaultException(Of ServiceError)(New ServiceError("More than 1 person found with the name provided.", ""), HttpStatusCode.NotFound)
        Case Is = 0
            Throw New WebFaultException(Of ServiceError)(New ServiceError("No Person with the name provided was found.", ""), HttpStatusCode.NotFound)
        Case Else
            foundPerson = people(0)
    End Select

    Return foundPerson
End Function
以下是我的ServiceError类:

<DataContract>
Public Class ServiceError
    <DataMember>
    Public Property ErrorInfo As String
    <DataMember>
    Public Property ErrorDetails As String
    Public Sub New(ByVal info As String, ByVal details As String)
        Me.ErrorInfo = info
        Me.ErrorDetails = details
    End Sub
    Public Sub New()

    End Sub

End Class
当我找不到我的人时,我调用了该方法:

Private Function FindPerson(ByVal name As String) As Person Implements MyService.FindPerson

  Dim foundPerson As Person = Nothing
  Dim people = GetPeople(name)
  Select Case people.Count
    Case Is > 1
        RaiseWebException(HttpStatusCode.NotFound, "More than 1 person found with the name provided.", "")
    Case Is = 0
        RaiseWebException(HttpStatusCode.NotFound, "Person with the name provided was found.", "")
    Case Else
        foundPerson = people(0)
  End Select

  Return foundPerson
End Function
Private Function FindPerson(ByVal name As String) As Person Implements MyService.FindPerson

  Dim foundPerson As Person = Nothing
  Dim people = GetPeople(name)
  Select Case people.Count
    Case Is > 1
        RaiseWebException(HttpStatusCode.NotFound, "More than 1 person found with the name provided.", "")
    Case Is = 0
        RaiseWebException(HttpStatusCode.NotFound, "Person with the name provided was found.", "")
    Case Else
        foundPerson = people(0)
  End Select

  Return foundPerson
End Function
这在使用Boomerang从浏览器调用方法时效果很好;但是,当我在vb.net消费测试应用程序中测试调用该方法时,我没有看到服务错误的详细信息。我得到了一个通用404错误,其中包含一条通用错误消息,该消息声明“无法到达端点”,而不是“找不到此人”。对于通过使用.NET实现的消费应用程序调用服务的任何人来说,这都会让人感到困惑

我不确定这是我未来服务中错误管理的最佳解决方案

对于.NETWeb服务中错误管理的最佳实践,我愿意接受任何建议


谢谢

我找到了我原来问题的解决方案:将
OutGoingResponse.StatusCode
设置为与我用于
WebFaultException的状态代码相同的状态代码

解决方案涉及实现一个名为
RaiseWebException
的方法,该方法将传出响应状态代码设置为WebFaultException中也设置的状态:

Private Sub RaiseWebException(ByVal status As HttpStatusCode, ByVal info As String, ByVal details As String)
        WebOperationContext.Current.OutgoingResponse.StatusCode = status
        Throw New WebFaultException(Of ServiceError)(New ServiceError(info, details), HttpStatusCode.NotFound)
End Sub
Private Sub RaiseWebException(ByVal status As HttpStatusCode, ByVal info As String, ByVal details As String)
        WebOperationContext.Current.OutgoingResponse.StatusCode = status
        Throw New WebFaultException(Of ServiceError)(New ServiceError(info, details), status)
End Sub
当我找不到我的人时,我调用该方法:

Private Function FindPerson(ByVal name As String) As Person Implements MyService.FindPerson

  Dim foundPerson As Person = Nothing
  Dim people = GetPeople(name)
  Select Case people.Count
    Case Is > 1
        RaiseWebException(HttpStatusCode.NotFound, "More than 1 person found with the name provided.", "")
    Case Is = 0
        RaiseWebException(HttpStatusCode.NotFound, "Person with the name provided was found.", "")
    Case Else
        foundPerson = people(0)
  End Select

  Return foundPerson
End Function
Private Function FindPerson(ByVal name As String) As Person Implements MyService.FindPerson

  Dim foundPerson As Person = Nothing
  Dim people = GetPeople(name)
  Select Case people.Count
    Case Is > 1
        RaiseWebException(HttpStatusCode.NotFound, "More than 1 person found with the name provided.", "")
    Case Is = 0
        RaiseWebException(HttpStatusCode.NotFound, "Person with the name provided was found.", "")
    Case Else
        foundPerson = people(0)
  End Select

  Return foundPerson
End Function
这在使用Boomerang从浏览器调用方法时效果很好;但是,当我在vb.net消费测试应用程序中测试调用该方法时,我没有看到服务错误的详细信息。我得到了一个通用404错误,其中包含一条通用错误消息,该消息声明“无法到达端点”,而不是“找不到此人”。对于通过使用.NET实现的消费应用程序调用服务的任何人来说,这都会让人感到困惑

因此,虽然这是我最初问题的答案,但它似乎不适合整个项目。祝所有面临同样问题的人好运