.Net WebService WSDL 复杂类型是否需要属性才能获取/设置?
我正在定义一个调用服务库的 Web 服务。服务库有一个名为 customer 的对象,其客户 ID 定义如下:
public class Customer
{
private int _customerID;
public int CustomerID
{
get { return _customerID; }
}
}
当我尝试通过 Web 服务传回客户对象时,生成的 WSDL 似乎不包含属性 CustomerID,除非我定义了 setter 。在某些情况下,似乎不需要设置器。我是否遗漏了某些内容,或者生成 WSDL 是否需要属性具有 getter/setter 才能暴露给客户端?
I am defining a web service which calls a service library. The service library has an object called customer with a customer ID defined the following way:
public class Customer
{
private int _customerID;
public int CustomerID
{
get { return _customerID; }
}
}
When I try to pass a customer object back through my web service, it seems that the WSDL generated doesn't include the property CustomerID, unless I define a setter. It seems like requiring a setter is undesireable in certain circumstances. Am I missing something, or does generating a WSDL require a property to have a getter/setter in order to be exposed to the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他答案是正确的,但不要提及这是 ASMX Web 服务使用的 XML Serializer 的限制。
ASMX Web 服务现在被 Microsoft 视为“遗留技术”,不应用于新服务开发。他们的替代品 WCF 没有这个限制。
The other answers are correct, but don't mention that this is a restriction of the XML Serializer, which is used by ASMX web services.
ASMX web services are now considered by Microsoft to be "legacy technology", and should not be used for new service development. Their replacement, WCF, does not have this restriction.
看看这是否有帮助: http://support.microsoft.com/kb/313584
看起来是这样是一项要求,您可以做的是在客户端尝试设置此属性时抛出异常。
从我发布的链接
您的可能会变成:
See if this helps: http://support.microsoft.com/kb/313584
It appears this is a requirement, what you could do is throw an exception should a client try to set this property.
From the link I posted
Yours could become:
setter 是必需的,以便 .Net 可以在传输后(反序列化期间)设置属性。您可以通过实现自己的对象或可能将其设置为内部来解决此问题:
internal set { }
这实际上更多是出于安全考虑,因此应该支持它,但默认的 .Net 行为是 1- 1 WSDL 中的属性映射。如果内部 setter 不起作用(我希望它不会)并且您不想为自定义 ser/deser 烦恼,那么只需向 setter 添加一个
throw new SecurityException()
即可。您可以通过实现自己的 自定义来绕过所需的设置器序列化,但我不确定 WSDL 会是什么样子。我还没有尝试过,但我怀疑自定义序列化和内部设置器的组合可以解决该问题。
编辑: 正如 @John Saunders 指出的,WCF 中可能不存在设置器限制。
The setter is required so .Net can set the property after transfer (during deserialization). You can solve this by implementing your own object or possibly by setting it as internal:
internal set { }
This is actually more of a security consideration so it should be supported, but default .Net behavior is 1-1 mapping of properties in WSDL. If internal setter doesn't work (I expect it won't) and you don't want to bother with custom ser/deser then just add a
throw new SecurityException()
to the setter.You can bypass the required setter by implementing your own custom serialization, but I'm unsure how the WSDL will look. I haven't tried it, but I suspect a combination of custom serialization and internal setter will solve the problem.
EDIT: As @John Saunders points out the setter limitation may not be there in WCF.