Named and optional parameters, and WCF

后端 未结 3 2115
無奈伤痛
無奈伤痛 2020-12-18 19:58

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?

相关标签:
3条回答
  • 2020-12-18 20:17

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

    0 讨论(0)
  • 2020-12-18 20:26

    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  %>
    
    0 讨论(0)
  • 2020-12-18 20:31

    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.

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