.Net WebService WSDL 复杂类型是否需要属性才能获取/设置?

发布于 2024-10-18 05:24:12 字数 359 浏览 6 评论 0原文

我正在定义一个调用服务库的 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 技术交流群。

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

发布评论

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

评论(3

与他有关 2024-10-25 05:24:12

其他答案是正确的,但不要提及这是 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.

不甘平庸 2024-10-25 05:24:12

看看这是否有帮助: http://support.microsoft.com/kb/313584

看起来是这样是一项要求,您可以做的是在客户端尝试设置此属性时抛出异常。

从我发布的链接

要解决此问题,请添加 SET
财产的程序。你可以
要么将程序留空,所以
该程序没有效果,
或提出自定义异常以通知
客户认为该房产是
只读,如下:

Public Property Id() As Integer
    Get
        Return nID
    End Get
    Set(ByVal Value As Integer)
        Throw New Exception("Cannot set read-only property 'Id'")
    End Set
End Property

您的可能会变成:

public class Customer
{
    private int _customerID;
    public int CustomerID
    {
        get { return _customerID; }
        set { throw new exception("Cannot set value!"); }
    }

}

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

To work around this problem, add a SET
procedure for the property. You can
either leave the procedure empty, so
that that the procedure has no effect,
or raise a custom exception to inform
clients that the property is
read-only, as follows:

Public Property Id() As Integer
    Get
        Return nID
    End Get
    Set(ByVal Value As Integer)
        Throw New Exception("Cannot set read-only property 'Id'")
    End Set
End Property

Yours could become:

public class Customer
{
    private int _customerID;
    public int CustomerID
    {
        get { return _customerID; }
        set { throw new exception("Cannot set value!"); }
    }

}
北渚 2024-10-25 05:24:12

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.

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