Named and optional parameters, and WCF

大兔子大兔子 提交于 2019-11-29 09:09:04

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.

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

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