网络服务错误它没有无参数构造函数'
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里问题的根源是类
Microsoft.Web.Services3.Security.Tokens.UsernameToken
没有无参数构造函数。它有 3 个,但它们都需要一个参数。 MSDN 上的 UsernameToken 构造函数。UsernameToken (XmlElement)
UsernameToken (String, String)
UsernameToken (String, String, PasswordOption)
问题是在反序列化过程中,XmlSerializer调用无参数构造函数来创建该类的实例。它无法反序列化没有无参数构造函数的类型。
我觉得你无能为力来解决这个问题。我只建议创建一个分部类,并自己实现该零参数构造函数。
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.
p。坎贝尔是对的,这是因为 XmlSerializer 需要无参数构造函数。
我不知道 WSE,但通过查看 Aleem 博客上的这篇文章,我不认为 UsernameToken 应该作为常规参数传递给 Web 方法 - 它应该在 WS-Security SOAP 标头中传递。您可以通过调用 SetClientCredential()。这是上面博客文章中的示例:
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:
您不能在 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 realUsernameToken
object anyway, there is a proxy class created from the WSDL information.