WCF数据限制

时间:2017-01-30 23:50:39

标签: c# vb.net wcf xsd

是否仍无法通知消费客户数据限制?

这个问题肯定是其他从未回答或至少5年没有可行解决方案的问题的重复。这些链接已经老化或者没有帮助,或者参考.Net 3.x,那时我们无能为力。

要明确的是,这与服务验证无关......请不要去那里。这只是通过自动生成的WSDL / XSD 让客户了解这些限制。

给定以下WCF服务,指定StringLength,Range和DefaultValue ....

VB版:

<ServiceContract([Namespace]:="example.com")>
Public Interface IWCF_Service
    <OperationContract()>
    Function Test1(Value As Something) As String

    Class Something
        <StringLength(50), DefaultValue("Whatever")>
        Public Property Thing1 As String = "Whatever"

        <Range(5, 50), DefaultValue(10), Required>
        Public Property Thing2 As Int32 = 10
    End Class
End Interface

C#版本:

[ServiceContract(Namespace = "example.com")]
public interface IWCF_Service
{
    [OperationContract()]
    string Test1(Something Value);

    public class Something
    {
        [StringLength(50), DefaultValue("Whatever")]
        public string Thing1 { get; set; }

        [Range(5, 50), DefaultValue(10), Required()]
        public Int32 Thing2 { get; set; }
    }
}

...生成的XSD缺少默认值和限制,而Thing2应该是minOccurs="1",因为它是必需的:

<xs:complexType name="IWCF_Service.Something">
    <xs:sequence>
        <xs:element minOccurs="0" name="Thing1" nillable="true" type="xs:string" />
        <xs:element minOccurs="0" name="Thing2" type="xs:int" />
    </xs:sequence>
</xs:complexType>

这就是我期待的(或类似的):

<xs:complexType name="IWCF_Service.Something">
    <xs:sequence>
        <xs:element minOccurs="0" name="Thing1" nillable="true" default="Whatever">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="50" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="Thing2" default="10">
            <xs:simpleType>
                <xs:restriction base="xs:int">
                    <xs:minInclusive value="5" />
                    <xs:maxInclusive value="50" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

1 个答案:

答案 0 :(得分:-1)

对我来说,我通过制作一个必须可变的附加类来做到这一点

status as short 
const SUCCESS as short= 1
const FAILED as short= 0
msg as string 

和一个返回该类对象的函数名调用,我所有的服务调用都重定向到该函数,即res的常数,以检查是否已应用了所需的函数,如果不是,则根据消息进行操作< / p>

dim Result=//wcf call
if Result.status=SUCCSES then
//do something
else 
msgbox(Result.msg)
end if