【问题标题】:ServiceStack SOAP endpoint returning HTML on validation errorServiceStack SOAP 端点在验证错误时返回 HTML
【发布时间】:2013-06-27 19:03:49
【问题描述】:

我已经使用 ServiceStack 创建了一个简单的 web 服务,并且我已经使用内置的 FluentValidation 功能设置了一些验证。如果我使用带有无效数据的 JSON 请求访问服务,一切都会按预期返回。在我的单元测试中,我得到一个 WebServiceException 并且我的响应 DTO 的 ResponseStatus 已按预期填写。但是,如果我随后将完全相同的代码切换到使用 Soap12 客户端,则该服务将返回 HTML,并在其末尾带有一些 SOAP。我将生成的 HTML 保存到一个文件中并在浏览器中打开它,果然告诉我什么验证被触发了。 HTML 之后的 SOAP 没有填写 ResponseStatus(它设置为 i:nil="true")。使用 SOAP 端点时,这是预期的吗?

AppHost 验证设置:

Plugins.Add(New ValidationFeature())
container.RegisterValidators(GetType(AppHost).Assembly)

请求 DTO:

<DataContract()> _
Public Class Decode
    Inherits AbstractRequest

    <DataMember()> Public Property StopCode As String

End Class

请求验证器:

Public Class DecodeRequestValidator
    Inherits AbstractValidator(Of Decode)

    Public Sub New()
        RuleFor(Function(req) req.StopCode).Length(3)
    End Sub

End Class

响应 DTO:

<DataContract()> _
Public Class DecodeResponse
    Implements ServiceStack.ServiceInterface.ServiceModel.IHasResponseStatus

    <DataMember()> Public Property StopName As String
    <DataMember()> Public Property ResponseStatus As ServiceStack.ServiceInterface.ServiceModel.ResponseStatus Implements ServiceStack.ServiceInterface.ServiceModel.IHasResponseStatus.ResponseStatus

End Class

服务类:

Public Class DecodeService
    Inherits Service

    Public Function Any(request As Decode) As Object
        Dim response As New DecodeResponse()
        response.StopName = "test"
        Return response
    End Function

End Class

测试:

<Test()> _
Public Sub InvalidLengthStopReturnsFailure()
    Dim client = New Soap12ServiceClient("http://127.0.0.1:81/WebService")
    ' Works perfectly with JsonServiceClient

    Try
        Dim response = client _
       .Send(Of WebServices.DecodeResponse)(New Decode With {.StopCode = "12"})

        Assert.Fail("No exception thrown")
    Catch ex As WebServiceException
        Assert.IsNotNull(ex.ResponseDto) ' <-- FAIL - ex.ResponseDto is null
    End Try

End Sub

【问题讨论】:

    标签: .net vb.net servicestack


    【解决方案1】:


    我在使用soap12 客户端时遇到了类似的问题。在我的测试应用程序(asp.net)中,我使用几乎相同的设置调用 Web 服务。我收到“响应不是格式良好的 XML”。我实际上看不到响应,因为它似乎为空。 out ResponseStatus 变量为空,类似于您的情况。 我在使用针对soap12/soap11 webservice生成的wsdl文件时遇到了这个问题。 (我正在为 Web 服务使用服务堆栈)。
    解决方法:
    我设法在 VS 中使用“添加服务引用”,它返回了预期的流畅验证错误,没有问题,但这对我来说并不是一个真正的答案,因为我必须向可能没有使用 .NET 的客户发布。 如果您确实发现了问题的根源,我将不胜感激。 T

    【讨论】:

    • 我开始认为这不是 servicestack 的常见用例。解决方案可能是提交补丁......如果我有时间,我会这样做。
    • 嗨 ElRobbo,对我来说,问题是 FluentValidation 正在返回一个 Response 对象数组,如果您引用 github.com/ServiceStack/ServiceStack/wiki/SOAP-support 是一行:因为 VS.NET 的 Add Service Reference 已针对消费进行了优化。 asmx 或 WCF RPC 方法调用它不能正确支持多个返回值(例如,当您还需要一个 ResponseStatus 属性时)。我最终做的是实现我自己的 FluentValidation 版本(只是我需要的方法)并将我的响应 DTO 设置为单例。这确保了响应没有搞砸。希望这会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多