7

I wonder how I can specify a parameter of an OperationContract method in WCF as required so that the generated xsd contains minOccurs="1" instead of minOccurs="0".

Example:

[ServiceContract(Namespace = "http://myUrl.com")]  
public interface IMyWebService  
{  
   [OperationContract]  
   string DoSomething(string param1, string param2, string param3);  
}

generates this xsd:

<xs:element name="DoSomething">  
  <xs:complexType>  
    <xs:sequence>  
      <xs:element minOccurs="0" name="param1" nillable="true" type="xs:string" />  
      <xs:element minOccurs="0" name="param2" nillable="true" type="xs:string" />  
      <xs:element minOccurs="0" name="param3" nillable="true" type="xs:string" />  
    </xs:sequence>  
  </xs:complexType>  

But I want to define minOccurs="1" within the code without the necessity to manually fix it in the xsd file.

Jan-Patrick Ahnen
  • 1,380
  • 6
  • 17
  • 31
  • http://stackoverflow.com/questions/1438623/how-can-i-force-wcf-to-autogenerate-wsdls-with-required-method-parameters-minoc/3436039#3436039 – Freelancer May 28 '13 at 12:39
  • See [here](http://stackoverflow.com/questions/1438623/how-can-i-force-wcf-to-autogenerate-wsdls-with-required-method-parameters-minocc/3436039#3436039) I think that it is better solution. – Karel Kral Jan 20 '11 at 09:51

2 Answers2

9

You might need to wrap your parameters in a class, then you can use the DataMember attribute and specify IsRequired=true:

[ServiceContract(Namespace = "http://myUrl.com")]  
public interface IMyWebService  
{  
   [OperationContract]  
   string DoSomething(RequestMessage request);  
}

[DataContract]
public class RequestMessage
{
   [DataMember(IsRequired = true)]
   public string param1 { get; set; }

   [DataMember(IsRequired = true)]
   public string param3 { get; set; }

   [DataMember(IsRequired = true)]
   public string param3 { get; set; }
}
Graham Clark
  • 12,886
  • 8
  • 50
  • 82
  • This isn't the answer I hoped to read, but thank you to make clear how it must be done. – Jan-Patrick Ahnen Aug 10 '10 at 09:32
  • 5
    Won't this just make the 'request' argument in the OperationContract marked as minOccurs="0" in the generated xsd? – arathorn Apr 27 '11 at 20:44
  • @arathorn I see you never received a response here. You bring up a strong point. I think you are correct. Enforcement of an OperationContract parameter requirement in WCF (as far as I have read) can not be done on 'Nullable' types without entering message contract. Here is a link I found: http://social.msdn.microsoft.com/Forums/vstudio/en-US/e707ed20-c09c-4e26-927a-7c3071d74ed7/operationcontract-with-required-parameters?forum=wcf ... I think you are right on your point. – Zack Jannsen Oct 14 '13 at 11:14
3

This implementation is nice to me: http://thorarin.net/blog/post/2010/08/08/Controlling-WSDL-minOccurs-with-WCF.aspx

Pit Ming
  • 401
  • 2
  • 5
  • 13