Setting nillable = false with WCF

Is it possible to change the default value for nillable in rows to false in wsdl using WCF? I cannot find any attributes or settings doing this out of the box, but is it possible to extend WCF in any way using the attributes for this? Or is there a better way? I need the ability to mark some of my string properties as nillable = false, but not all.

eg:

[DataMember] [Nillable(false)] public string MyData{ get; set; } 
+1
wsdl wcf
source share
2 answers
 [DataMember(IsRequired=True)] 

That should do it, I believe?

0
source share

To do this, you need to write your own WsdlExportExtension.

Here is an example:

 public class WsdlExportBehavior : Attribute, IContractBehavior, IWsdlExportExtension { public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) { } public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context) { var schemaSet = exporter.GeneratedXmlSchemas; foreach (var value in schemaSet.GlobalElements.Values) { MakeNotNillable(value); } foreach (var value in schemaSet.GlobalTypes.Values) { var complexType = value as XmlSchemaComplexType; if (complexType != null && complexType.ContentTypeParticle is XmlSchemaSequence) { var sequence = complexType.ContentTypeParticle as XmlSchemaSequence; foreach (var item in sequence.Items) { MakeNotNillable(item); } } } } private static void MakeNotNillable(object item) { var element = item as XmlSchemaElement; if (element != null) { element.IsNillable = false; } } public void AddBindingParameters(ContractDescription description, ServiceEndpoint endpoint, BindingParameterCollection parameters) { } public void ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, ClientRuntime client) { } public void ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, DispatchRuntime dispatch) { } public void Validate(ContractDescription description, ServiceEndpoint endpoint) { } } 

And apply [WsdlExportBehavior] to your class of service.

Hope this helps.

0
source share

All Articles