20

so .Net 4 added named and optional parameters which are pretty sweet. I don't need to make as many 1 line overload methods.

Will that work over WCF?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
puffpio
  • 3,402
  • 6
  • 36
  • 41

3 Answers3

32

WSDL cannot describe optional parameters, so the answer is "no".

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 2
    It's unfortunate that the [OperationContract] just silently converts optional paramters into required parameters :( – epalm Dec 05 '11 at 16:21
  • 2
    @epalm: it doesn't convert anything. It just ignores things that are not relevant. – John Saunders Dec 05 '11 at 16:38
  • Clarification: when I host a WCF service and generate a proxy via Metadata Exchange, the methods in the generated Reference.cs have required parameters, even though the methods in the service contain optional parameters. – epalm Dec 05 '11 at 16:52
  • 2
    @epalm: this is for the reason I gave in my answer: WSDL and MEX don't know anything about optional parameters, nor any other platform-specific features. Watch them not support `async`, for instance. – John Saunders Dec 05 '11 at 17:02
20

Since these are compiler semantics I'd say no. However you'd expect them to work in the only following way.

On the Service Code side all code would accept the defaulted parameters.

On the client side I note that the 'Add Service Reference' tooling on VS2010 doesn't take the defaults and add them to the generated proxy. So You'd have to generate you're own proxy.

In this way the client code can use the defaults if the the default is specified in the client side contract implementation.

I would be that the same is true for the named parameters.

All in all yes, but the stuff is not carried over WCF. All that happens is that the client proxy will have to send into the channel factory as a proper parameter.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • 2
    The underlying reason for this is that neither WSDL, nor MEX knows anything about the features of the C# programming language. They have no way to describe optional parameters. – John Saunders Nov 12 '12 at 21:42
0

I'm Working with VS 2017, .Net 4 and a WcfService Project I've got a Service like this:

Interface:

namespace MyServiceNamespace
{
[ServiceContract]
public interface IMyService
{
  [OperationContract]
  [WebGet(ResponseFormat = WebMessageFormat.Json)]
  String GetSomething(int Produkt, DateTime MyDate, char SomeFlag);
}

public class myService : IMyService
{
  public String GetSomething(int Produkt, DateTime MyDate, char SomeFlag)
  {
    if(Produkt==0){Produkt=12345;}
    if(MyDate==DateTime.MinValue){MyDate=DateTime.Today;}
    if(SomeFlag=='\0'){SomeFlag='F';}
    return dosomething();
  }

}
}

i can call this with http://myserver/myservice.svc/GetSomething?Produkt=1&MyDate=2018-01-01&SomeFlag=A

but also http://myserver/myService.svc/GetSomething?Produkt=1&MyDate=2018-01-01

or even http://myserver/myservice.svc/GetSomething

the missing Parameters are filled with the "empty" or Default-Value of the type. I dont know if there is a better way to set your own Default value, because

public String GetSomething(int Produkt, DateTime MyDate, char SomeFlag='F')

did not work, still SomeFlag =='\0'

What i have not found out is how i can tell if the value is there because the paramater was missing, or was called with i.e. 1.1.0001

Update: The Markup MyService.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="MyServiceNameSpace.MyService" CodeBehind="MyService.svc.cs" Factory=System.ServiceModel.Activation.WebScriptServiceHostFactory  %>
SEn sen
  • 5
  • 6