网络服务错误它没有无参数构造函数'

发布于 2024-09-13 15:50:46 字数 595 浏览 4 评论 0原文

我创建了一个 Web 服务,它是一种使用 Microsoft.Web.Services3.WebServicesClientProtocol 设置用户凭据的方法。示例代码是:

<WebMethod()> _
    Public Sub ClientCredential1(Of TSecurityToken As SecurityToken)_
         (ByVal UserCred As Microsoft.Web.Services3.Security.Tokens.UsernameToken)

        Dim cProxy As New Microsoft.Web.Services3.WebServicesClientProtocol()
        cProxy.SetClientCredential(UserCred)
    End Sub

当我运行 Web 服务时,出现以下错误:

“Microsoft.Web.Services3.Security.Tokens.UsernameToken 无法序列化,因为它没有无参数构造函数。”

有谁知道问题出在哪里吗?

I've created a web service , which can a method to set the user credential using Microsoft.Web.Services3.WebServicesClientProtocol. The sample code is :

<WebMethod()> _
    Public Sub ClientCredential1(Of TSecurityToken As SecurityToken)_
         (ByVal UserCred As Microsoft.Web.Services3.Security.Tokens.UsernameToken)

        Dim cProxy As New Microsoft.Web.Services3.WebServicesClientProtocol()
        cProxy.SetClientCredential(UserCred)
    End Sub

When I run the web service it gives this error:

"Microsoft.Web.Services3.Security.Tokens.UsernameToken cannot be serialized because it does not have a parameterless constructor."

Does any one know where is the problem ?

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

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

发布评论

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

评论(3

老街孤人 2024-09-20 15:50:46

这里问题的根源是类 Microsoft.Web.Services3.Security.Tokens.UsernameToken 没有无参数构造函数。它有 3 个,但它们都需要一个参数。 MSDN 上的 UsernameToken 构造函数。

  • UsernameToken (XmlElement)
  • UsernameToken (String, String)
  • UsernameToken (String, String, PasswordOption)

问题是在反序列化过程中,XmlSerializer调用无参数构造函数来创建该类的实例。它无法反序列化没有无参数构造函数的类型。

我觉得你无能为力来解决这个问题。我只建议创建一个分部类,并自己实现该零参数构造函数。

'ensure namespacing is correct.
Public Partial Class UsernameToken
    Public Sub New()
    End Sub    
End Class

The root of the problem here is that the class Microsoft.Web.Services3.Security.Tokens.UsernameToken doesn't have a parameter-less constructor. It's got 3 of them, but they all demand a parameter. UsernameToken constructors on MSDN.

  • UsernameToken (XmlElement)
  • UsernameToken (String, String)
  • UsernameToken (String, String, PasswordOption)

The problem is that during deserialization, XmlSerializer calls the parameterless constructor to create an instance of that class. It can't deserialize a type that doesn't have a parameterless constructor.

I get the sense there's not much you can do to work around this problem. I'd only suggest creating a partial class, and implementing that zero-param constructor yourself.

'ensure namespacing is correct.
Public Partial Class UsernameToken
    Public Sub New()
    End Sub    
End Class
七禾 2024-09-20 15:50:46

p。坎贝尔是对的,这是因为 XmlSerializer 需要无参数构造函数。

我不知道 WSE,但通过查看 Aleem 博客上的这篇文章,我不认为 UsernameToken 应该作为常规参数传递给 Web 方法 - 它应该在 WS-Security SOAP 标头中传递。您可以通过调用 SetClientCredential()。这是上面博客文章中的示例:

Dim oService As New WSETestService.ServiceWse

Dim U As New UsernameToken(“<User_Name>”, “<Password>”, PasswordOption.SendHashed)
oService.SetClientCredential(U)

p. campbell is right, it's because the XmlSerializer requires a parameterless constructor.

I don't know WSE, but from looking at this post on Aleem's Weblog, I don't think the UsernameToken is supposed to be passed as a regular argument to a web method - it's supposed to be passed in the WS-Security SOAP headers. You get the proxy to pass it in the headers by calling SetClientCredential(). Here's the example from the above blog post:

Dim oService As New WSETestService.ServiceWse

Dim U As New UsernameToken(“<User_Name>”, “<Password>”, PasswordOption.SendHashed)
oService.SetClientCredential(U)
旧情勿念 2024-09-20 15:50:46

您不能在 Web 服务中使用 Microsoft.Web.Services3.Security.Tokens.UsernameToken 类型的参数,因为它无法序列化(或更具体地说,无法反序列化)。

创建一个类,其中仅包含创建 UsernameToken 所需的数据并用作参数类型。无论如何,客户端不会创建真正的 UsernameToken 对象,而是根据 WSDL 信息创建了一个代理类。

You can't use a parameter of the type Microsoft.Web.Services3.Security.Tokens.UsernameToken in a web service, as it's not possible to serialise (or more specifically not possible to deserialise).

Create a class that just contains the data that you need to create a UsernameToken and use as parameter type. The client side would not create a real UsernameToken object anyway, there is a proxy class created from the WSDL information.

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