1

Is it possible to change the default value of nillable on strings to false in the wsdl using WCF? I can't find any attributes or settings doing this out of the box, but is it possible to extend WCF in some way by using attributes to do this myself? Or is there a better way around? I need the possibility mark some of my string properties as nillable=false, but not all.

e.g:

[DataMember]
[Nillable(false)]
public string MyData{ get; set; }
Markus
  • 1,614
  • 1
  • 22
  • 32
  • Check out http://stackoverflow.com/questions/1589750/wcf-string-element-nillable-false – GTG May 15 '12 at 15:01
  • It seems that he solved it by setting all his strings, or all required, to nillable false, which is not really what I would like to do. I've managed to get the WsdlExporter to work, but now I need to do some matching between the generated schema and my interfaces and classes to get my [Nillable] attribute. Any ideas on where to find that kind of information? – Markus May 16 '12 at 08:50
  • Related: http://stackoverflow.com/questions/9039850/wcf-wsdl-nillable-attributes/21266714#21266714 – Steven Jan 22 '14 at 12:51

2 Answers2

0
[DataMember(IsRequired=True)]

This should do it I believe ?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Chris
  • 2,471
  • 25
  • 36
  • Tried that, it only sets that the member is required in the message. A string can be null and still part of the message. – Markus May 16 '12 at 06:18
  • I concur. This is *not* the answer and WCF consumers (like SharePoint BCS) will still assume the value is able to be null - which it will gripe about when trying to use it as an Identity. Thus this is not an appropriate solution for this problem. – user2246674 Jul 18 '13 at 01:17
0

You have to write your own WsdlExportExtension to achieve this.

Here's a sample:

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 the [WsdlExportBehavior] to your service class.

Hope this helps.

Vizu
  • 1,871
  • 1
  • 15
  • 21