本文介绍了如何将有限值设置为 maxoccurs 而不是“无界"在 WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务.当我公开我的服务时,我的所有 Datacontracts 都被转换为元素,并且每个带有 ComplextType 标记的元素都被转换为ArrayOf".在 ComplextType 标签中,maxOccurs 的默认值是无界".

I have a WCF service. When I expose my service, all my Datacontracts are getting converted to element and also an "ArrayOf" for each element with ComplextType tag. In ComplextType tag, the maxOccurs is getting default value as "unbounded".

但实际上我想覆盖这个 maxOccurs 值并为生成的 XSD 文件中ArrayOf"中的每个元素设置一个有限值(例如:maxOccurs="10").

But actually I want to over write this maxOccurs value and set a finite value(Eg: maxOccurs="10") for each element inside "ArrayOf" in the generated XSD files.

我尝试实现 IXMLSerializable,但没有奏效.有人可以帮我穿上这个吗?

I tried implementing IXMLSerializable, didn't work. Can someone please help me put on this?

注意:服务和数据合同的命名空间是不同的

Note: for both service and data contracts namespace is different

我的服务合同:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [FaultContract(typeof(ServiceApplicationFault), Action = ServiceApplicationFault.FaultContractAction)]
    EmployeeModel GetDetails(String EmpId);
}

我的数据合同

[DataContract]
public class EmployeeModel
{
    [DataMember]
    public string EmpId { get; set; }

    [DataMember]
    public string EmpName { get; set; }

    [DataMember]
    public string EmpDept { get; set; }

}

我生成的 XSD

<xs:complexType name="ArrayOfEmployeeModel">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="EmployeeModel" nillable="true" type="tns:EmployeeModel"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfEmployeeModel" nillable="true" type="tns:ArrayOfEmployeeModel"/>
<xs:complexType name="EmployeeModel">
<xs:sequence>
<xs:element minOccurs="0" name="EmpId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="EmpName" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="EmpDept" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="EmployeeModel" nillable="true" type="tns:EmployeeModel"/>

推荐答案

经过长时间的研究,我找到了问题的解决方案.我们可以通过覆盖生成的 XSD 模式来做到这一点.可以通过实现IContractBehavior、IWsdlExportExtension 接口来重写此模式.

After a long research I found solution for my question. We can do this by over writing the generated XSD schema. Over writing this schema can be done by implementing IContractBehavior, IWsdlExportExtension interfaces.

Step1:创建一个名称后缀为Attribute"的类,并为该类实现Attribute类和IContractBehavior、IWsdlExportExtension.

Step1: Create a class with name with suffix "Attribute" and implement Attribute class and IContractBehavior, IWsdlExportExtension to that class.

第 2 步:将此类作为属性添加到您的服务合同中.

Step 2: Add this class as an attribute to your service contract.

步骤 3:现在在方法 IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context) 中实现您的要求.此时,您将获得可用的元数据信息.

Step3: Now implement your requirement in the method IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context). At this point you will be having metadata information available.

有关详细信息,请参阅此处此处

For more info refer here and here

谢谢大家!

这篇关于如何将有限值设置为 maxoccurs 而不是“无界"在 WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:33