ServiceStack SOAP端点在验证错误时返回HTML

时间:2013-06-30 14:16:23

标签: .net vb.net servicestack

我已经使用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

1 个答案:

答案 0 :(得分:0)


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