【问题标题】:WCF data restrictionsWCF 数据限制
【发布时间】:2017-06-16 07:01:04
【问题描述】:

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

这个问题绝对是其他从未回答过或至少 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>

【问题讨论】:

  • 为什么说“这是我所期待的(或类似的):”VB 输出和 C# 输出哪一个?
  • @Chillzy C#/VB 代码是输入。自动生成的 WSDL / XSD 是我们没有得到的输出。
  • 我明白了。第一个输出是VB代码输出,第二个是C#输出?
  • @Chillzy VB 和 C# 都生成了上面非常短的 XSD。我希望他们生产的 XSD 越长。

标签: c# vb.net wcf xsd


【解决方案1】:

对我来说,我通过创建一个必须 var 的附加类来做到这一点

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

和一个返回此类对象的函数名调用,我所有的服务调用都重定向到这个函数,这是一个常量,用于 res 检查是否已应用想要的函数,如果没有,我会根据消息

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

【讨论】:

  • 我不知道你在回答什么问题,但不是这个。
猜你喜欢
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 2021-03-05
  • 2012-03-08
  • 2011-11-11
相关资源
最近更新 更多