asmx asp.net webservice返回多个类wsdl

发布于 2024-11-30 14:05:35 字数 671 浏览 1 评论 0原文

我们正在为客户开发网络服务。我们不应该抛出 SoapExceptions,因此,我们捕获服务器端的每个异常,并返回一个自定义的 Exception 类。

Public Class Order
...
End Class

Public Class MyException
...
End Class

然后在我的 web 服务中一个函数(webmethod):

Public Function GetOrder(ByVal id As Integer) As Object

    Try
        ...
        Return New Order()
    Catch ex As Exception
        Return New MyException(ex.Message)
    End Try

End Function

现在的问题是,因为我的 webmethod 返回类型 [Object]。生成的 wdsl 不包含订单或异常。

我可以将 [Object] 更改为 [Order] 或 [MyException],但 wsdl 中只生成其中之一。

那么有人知道我应该如何处理这个问题吗?我想要在我的 wsdl 中同时使用 MyException 类型和 Order 类型,但我就是无法让它工作。

谢谢大家。

We are developing a webservice for a client. We are not supose to throw SoapExceptions, so instead, we catch every exception server side, and return a custom Exception class.

Public Class Order
...
End Class

Public Class MyException
...
End Class

And then in my webservice a function (webmethod):

Public Function GetOrder(ByVal id As Integer) As Object

    Try
        ...
        Return New Order()
    Catch ex As Exception
        Return New MyException(ex.Message)
    End Try

End Function

The problem now is, that since my webmethod is returning the type [Object]. The wdsl that is generated does not contain the order, or the exception.

I can change the [Object] to [Order] Or [MyException], but only one of them is generated in the wsdl.

So does anybody have an idea of how i should handle this? I want both the MyException type and the Order type in my wsdl, but i just cant get it working.

Thank you all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仲春光 2024-12-07 14:05:35

如果您定义了 MyException

Public Class MyException
        inherits System.Exception
    ...
    End Class

那么您不需要返回自定义异常,只需抛出它即可。

那么你可以定义

Public Function GetOrder(ByVal id As Integer) As Order

    Try
        ...
        Return New Order()
    Catch ex As Exception
        Throw New MyException(ex.Message)
    End Try

End Function

我记得(已经有一段时间了)尝试从 Web 方法返回多个对象可能会非常麻烦

If your definition of MyException

Public Class MyException
        inherits System.Exception
    ...
    End Class

then you shouldn't need to return the Custom Exception just throw it.

then you can define

Public Function GetOrder(ByVal id As Integer) As Order

    Try
        ...
        Return New Order()
    Catch ex As Exception
        Throw New MyException(ex.Message)
    End Try

End Function

As I recall (and it's been a while) trying to return multiple objects from a web method can prove to be extremely troublesome

我还不会笑 2024-12-07 14:05:35

如果您确实想返回多个对象,那么也许您应该创建一个“包装器”对象,例如如下所示:

'please note: I don't normally use VB.NET, so there might be some errors
Public Class OrderResponse

Public Property Order() As Order
    Get
        Return m_Order
    End Get
    Set
        m_Order = Value
    End Set
End Property
Private m_Order As Order

Public Property Exception() As MyException
    Get
        Return m_Exception
    End Get
    Set
        m_Exception = Value
    End Set
End Property
Private m_Exception As MyException
End Class

然后更改您的方法以返回该类的实例,并将属性 Order 或 Exception 设置为相应的值:

Public Function GetOrder(ByVal id As Integer) As OrderResponse
    ...
End Function

If you really want to return multiple objects, then maybe you should create a "wrapper" object, e.g something like this:

'please note: I don't normally use VB.NET, so there might be some errors
Public Class OrderResponse

Public Property Order() As Order
    Get
        Return m_Order
    End Get
    Set
        m_Order = Value
    End Set
End Property
Private m_Order As Order

Public Property Exception() As MyException
    Get
        Return m_Exception
    End Get
    Set
        m_Exception = Value
    End Set
End Property
Private m_Exception As MyException
End Class

Then change your method to return an instance of that class, with either the property Order or Exception set to the respective value:

Public Function GetOrder(ByVal id As Integer) As OrderResponse
    ...
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文