Can't use optional parameters when implementing an interface for a WCF

后端 未结 5 823
刺人心
刺人心 2021-01-11 10:19

In my interface I have declared this.

[OperationContract]
[WebGet]
String GetStuff(String beep, String boop = \"too lazy to type\");

I impl

5条回答
  •  清歌不尽
    2021-01-11 10:48

    I get the compiler whining and weeping about no method with signature of a single parameter.

    Start at the beginning. That your compiler "whines" is because the service does not recognize optional parameters with default values, so it will just expose the method requiring all parameters. Based on this metadata you generate a client proxy ("Service Reference"), which also doesn't contain the method you expect; it only sees the method the service exposes: the one with the (String beep, String boop) signature. So that's why, in the end, you receive a compile error when you try to call a non-existing method on a class.

    Now when you call this method on the service, your client will have to provide both values. If you supply null, the service will see null, as the values for the default parameters have to be compiled into the caller. WCF does not support that, so you'll just have to create overloads as @StephenBorg suggested.

提交回复
热议问题