WCF: is there an attribute to make parameters in the OperationContract required?

后端 未结 2 1257
遇见更好的自我
遇见更好的自我 2020-12-19 03:08

I use [DataMember(IsRequired=true)] to make the DataContract properties required. There doesn\'t seem to be some IsRequired for the OperationContract parameters

相关标签:
2条回答
  • 2020-12-19 03:54

    No. Just like any regular method, you'll need to check whether reference type parameters have a value or are null.

    Just apply your normal defensive programming patterns, checking reference types before accessing their properties.

    0 讨论(0)
  • 2020-12-19 03:58

    Unfortunately it can't be done using default WCF. There exist a few workarounds:

    • A custom RequiredParametersBehavior attribute
    • Using the Validation Application Block from the Enterprise Library and associate a ruleset to your method

    You can however implement a FaultContract and throw a fault when the input parameter is null.

    0 讨论(0)
提交回复
热议问题