我已经实现了一个简单的Web服务,并且我想返回适当的Http Status Error代码,具体取决于被调用方法中发生的错误。

我正在将.NET Framework 4.7.2与Visual Studio 2019和Visual Studio内置的IIS Express 10一起使用以进行测试。我正在使用Boomerang扩展程序(用于Chrome)调用该服务。

我已经实现了带有名称的FindPerson方法。如果找不到该人,或者找到一个以上的人,我想返回“未找到”响应(404)。

我已经实现了一个简单的ServiceError类,该类用于引发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

这是我得到的回应。就像我说的那样,详细信息是正确的...但是Http状态错误代码是500,而不是404:
<s:Envelope
    xmlns:s="http://www.w3.org/2003/05/soap-envelope"
    xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
        <a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/soap/fault</a:Action>
    </s:Header>
    <s:Body>
        <s:Fault>
            <s:Code>
                <s:Value>s:Sender</s:Value>
                <s:Subcode>
                    <s:Value
                        xmlns:a="http://schemas.microsoft.com/2009/WebFault">a:NotFound
                    </s:Value>
                </s:Subcode>
            </s:Code>
            <s:Reason>
                <s:Text xml:lang="en-US">Not Found</s:Text>
            </s:Reason>
            <s:Detail>
                <ServiceError
                    xmlns="http://schemas.datacontract.org/2004/07/My_Web_Service"
                    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                    <ErrorDetails/>
                    <ErrorInfo>No Person with the name provided was found.</ErrorInfo>
                </ServiceError>
            </s:Detail>
        </s:Fault>
    </s:Body>
</s:Envelope>

编辑:
我找到了解决我问题的方法,但这只是让我意识到我需要找到一个更好的方法。

该解决方案涉及实现一种称为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 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实现的使用方应用程序调用服务的任何人,这都会造成困惑

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

我愿意接受有关.NET Web服务中错误管理最佳实践的任何建议。

谢谢

最佳答案

我找到了原始问题的解决方案:将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), 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

当使用Boomerang从浏览器中调用该方法时,此方法效果很好。但是,当我在使用vb.net的测试应用程序中测试调用该方法时,没有看到服务错误详细信息。我收到一条通用404错误,并显示一条通用错误消息,指出“无法到达端点”而不是“未找到该人”。通过使用.NET实现的使用方应用程序调用服务的任何人都会感到困惑。

因此,尽管这是我最初的问题的答案,但对于整个项目而言似乎还不够。碰到同一件事的其他人最好的运气。

08-03 16:33