Wednesday, April 7, 2010

OperationContract with required parameters(WITH other options description)

1.Why are all parameters in all of your services declared as optional when most of the times they really aren't?"

Now if you navigate to SvcFile.svc?xsd=xsd0 (if not that then xsd=xsd1 or xsd=xsd2), you can see the schema for this operation and the xs:element elements for all 3 parameters and the return value have minOccurs="0" attribute. This essentially makes these parameters optional in the WSDL, SOAP UI and proxy/client code generated using some java toolkit they are using. As is pretty obvious, we don't want the control to even enter the Transfer method if the SOAP (1.2/1.1) request comes in without any of those parameters.

2.At least for simple parameters, you can use an IParameterInspector to enforce that they are present (see below). This solution has two limitations: it doesn't change the WSDL (which will still note the parameters are optional), and it doesn't work for nullable types. To work for nullable types you'd actually need a new formatter (basically doing the deserialization of the different parameters yourself).

changing the WSDL to reflect the required nature of parameters is my primary goal.
ts good to see how we can validate that required parameters are actually present in a message at the infrastructure level. However, that is not really a problem for most services because when these required parameters are "validated" by the service, in almost all cases a value of 0 or null is invalid anyways.

In this case, sort of rewriting the WSDL for the service (which is easy to do, if not to maintain - you'll need to save the WSDL+XSD files generated for the service, edit them, and link to them via the ExternalMetadataLocation property of the ServiceMetadataBehavior), you're left with the solution listed in your previous blog post.


I'm looking for a way to make method parameters required in an OperationContract without switching to MessageContract. For example, I want to be able to declare a method like the following:

public int Transfer([DataParameter(IsRequired=true)]int SourceAccount, [DataParameter(IsRequired=true)]int TargetAccount, [DataParameter(IsRequired=true)]double Amount);

and have System.ServiceModel.Description.
DataContractSerializerMessageContractExporter.ExportBody method not put minOccurs="0" in the WSDL for the operation.

No comments:

Post a Comment