locked
optional parameter for operationcontract RRS feed

  • Question

  •  

    Does anybody know if I can specify an optional parameter in a operationcontract for WCF. For example, if I have a method that has an optional parameter as the second parameter, how do I specify it?

     

    [operationcontract]

    doSomething(String method, String parameters = "")

    Friday, December 14, 2007 4:20 AM

All replies

  • Ahhh... default parameters in c#?

     

     

    Wednesday, December 19, 2007 8:17 AM
  • I have the same question as jvshore but it doesn't have to be in C# because I code in VB.Net :)

    Thursday, January 14, 2010 11:06 AM

  • It's very long now, Anybody can answer it?
    Tuesday, February 15, 2011 4:37 AM
  • No, it cannot be done. You can, however, use overload (and change the operation name, since multiple operations on the same contract cannot have the same name in SOAP) to emulate default parameters, like in the example below.

      public class Post_ce3c69b6_ccb0_4455_8cb0_96523fa3dc20
      {
        [ServiceContract]
        public interface ITest
        {
          [OperationContract]
          string Process(string param);
          [OperationContract(Name = "ProcessWithDefault")]
          string Process();
        }
        public class Service : ITest
        {
          public string Process(string param)
          {
            return param;
          }
    
          public string Process()
          {
            return Process("Default parameter");
          }
        }
        public static void Test()
        {
          string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
          ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
          host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
          host.Open();
          Console.WriteLine("Host opened");
    
          ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
          ITest proxy = factory.CreateChannel();
    
          Console.WriteLine(proxy.Process());
          Console.WriteLine(proxy.Process("hello world"));
    
          ((IClientChannel)proxy).Close();
          factory.Close();
    
          Console.Write("Press ENTER to close the host");
          Console.ReadLine();
          host.Close();
        }
      }
    
    
    Tuesday, February 15, 2011 6:28 AM