16

While using WCF and OperationContracts I have the following method defined:

    [OperationContract]
    [FaultContract(typeof(ValidationFault))]
    [FaultContract(typeof(FaultException<ExceptionDetail>))]
    int DoSomething(int someId, MyComplexType messageData);

When this gets translated to a WSDL by the WCF runtime, it ends up with with minoccurs="0" listed for the parameters someId and messageData (and subsequently throws a runtime error if these parameters are missing).

If I generate a proxy using SoapUI I get something that looks like this:

  <com:DoSomething>
     <!--Optional-->
     <com:EventId>1</com:EventId>
     <!--Optional-->
     <com:myComplexType >
        <com:id>1</com:id>
     </com:myComplexType >
  </com:DoSomething>

The id field in MyComplexType is marked up with DataMemeber attribute using IsRequired="true" and thus is exposed as mandatory.

It's obviously quite misleading for the WSDL to specify that a parameter is optional when it isn't, but I can't see any obvious way to markup the OperationContract to force WCF to recognise and expose these parameters as required.

I'm slightly baffled there doesn't seem an obvious way to do this (reading intellisense / msdn / google). Or I'm going blind and overlooking something obvious.

Any clues?

3
  • I've found this Microsoft Connect issue connect.microsoft.com/wcf/feedback/… that implies this might not be possible. Which sucks. Commented Sep 17, 2009 at 12:55
  • With further thought, it appears as though the parameters are listed as minoccurs="0" because the types themselves could be null, thus, if somebody supplied a null it'd still match the method sig. Still doesn't help for the purposes of WSDL generation and defining a contract with a calling application / organisation. Commented Sep 17, 2009 at 13:00
  • the generated WSDL isn't affected because the type could be null (reference types vs. value types). If a value type is omitted, the default value for that value type will be used. See my answer below however.
    – Thorarin
    Commented Aug 8, 2010 at 21:18

2 Answers 2

18

I've just written a Blog post about this subject, as I ran into the problem myself last week. It explains how you can modify the metadata that WCF generates at runtime.

Aside from downloading the source file, you only need to add an attribute to your contract definition. Like so:

[ServiceContract]
[RequiredParametersBehavior]
public interface ICalculatorService
{
    [OperationContract]
    int Add(int firstValue, int secondValue);
}

Here's the Blog post that explains it in more detail: Controlling WSDL minOccurs with WCF

1
  • Interesting approach. Can you tell me if this works with all types (marshal by reference and marshal by value)? I know that nullable types tend to read up as challenges in generated xsd ... I am wondering if this applies to what you are doing as well. Say I was passing several required 'string' parameters. Commented Oct 14, 2013 at 11:32
0

Check that MyComplexType is marked with a [DataContract] attribute.

For my own WCF contract, I found that minOccurs = 1 would not show up for IsRequired=true in the generated wsdl until the whole chain of objects implicated in the contract were marked as such.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.