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

徘徊边缘 提交于 2019-12-19 05:14:07

问题


In my interface I have declared this.

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

I implemented it as follows.

String GetStuff(String beep, String boop = "too lazy to type") { ... }

It compiles and uploads as my WCF service. However, when I used it as a web reference and try to execute the code below, I get the compiler whining and weeping about no method with signature of a single parameter. The last line is the problem.

How can I then be too lazy to type by default?

ServiceClient client = new ServiceClient();
client.GetStuff("blobb", "not lazy");
client.GetStuff("blobb");

回答1:


Simply: default arguments are not supported.

By design and with reason. We use C# to write WCF contracts but that's a notational trick. Not every C# language feature can be implemented in SOAP, REST or JSon.




回答2:


You can try this, overloading the function.

[OperationContract]
MyResponse GetData(); 

[OperationContract(Name = "GetDataByFilter")]
MyResponse GetData(string filter);

Then another option is to use a DataContract instead of multiple parameters, and set IsRequired to false on the appropriate DataMembers, like explained in this question.




回答3:


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.




回答4:


You can do it like this:

[DataContract]
public class GetStuffParams
{
    [DataMember]
    string beep {get; set; }

    [DataMember]
    string boop  {get; set;}


    public GetStuffParams() { boop = "too lazy to type"; }
}


[OperationContract]
[WebGet]
String GetStuff(GetStuffParams stuffParams);



回答5:


you should check out the code generated when adding the service reference.

as the code is generated from WISDL, where the signature is (pseudo):

GetStuff(String , String )

it generates the code accordingly, not knowing about the optional parameters. so, if you want to get lazy, you should alter the proxy class generated or, as @Stephen Borg suggested, overload the function.



来源:https://stackoverflow.com/questions/17043134/cant-use-optional-parameters-when-implementing-an-interface-for-a-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!