Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use optional parameters in a WCF service method? [duplicate]

Tags:

I've seen posts like this and this but they are each a few years old.

Can I do something like this?

    [OperationContract]     [FaultContract(typeof(MyCustomFault))]     List<InventoryPart> SelectMany(string partialPartNumber, string division = null); 
like image 363
BBauer42 Avatar asked Apr 09 '14 13:04

BBauer42


People also ask

Can a method have 2 optional parameters?

Can a method have 2 optional parameters? If you want to define multiple optional parameters, then the method declaration must be as shown below. The defined GetDetails() method can be accessed as shown below. GetDetails(1); GetDetails(1, “Rohini”);

Can we have multiple optional parameters in C#?

In c#, we can also achieve the optional parameters by using method overloading functionality. Generally, the method overloading functionality will allow us to create multiple methods with the same name but with different parameters. To know more about overloading functionality in c#, check Method Overloading in C#.

Is adding an optional parameter a breaking change?

Common examples of additive, non-breaking changes include: Adding a resource or method. Adding a response field. Adding optional query parameters.

How do you pass optional parameters?

It is the simplest and easiest way to implement the optional parameter. In this way, you just simply define the optional parameters with their default value in the method definition. And always remember the optional parameter is the last parameter in the parameter list of the method.


1 Answers

You can't. There are many restrictions on WCF regarding the method signatures; some restrictions are because of the host mechanism, and others because of the WSDL/MEX.

Despite the fact that WCF could potentially let you have default parameters in your service code and overloaded methods and many other things, when you host your service it might or not start, or it could start but might or not work. It's tricky.

What I've done to overcome this, is that I use nullable parameters wherever required, then on my client code I always have a service layer that access to my autogenerated client proxy; my service layer has all the overloads and optional params I want. Example (dirty code):

WCF service:

[OperationContract] [FaultContract(typeof(MyCustomFault))] List<InventoryPart> SelectMany(string partialPartNumber, string division, int? subDivision, bool? isActive); 

Client Service Layer (not the autogenerated proxy, but one written by me)

public List<InventoryPart> GetParts(string partialPartNumber){     return GetParts(partialPartNumber, null); }  public List<InventoryPart> GetParts(string partialPartNumber, string division){     return GetParts(partialPartNumber, division, null); }  public List<InventoryPart> GetParts(string partialPartNumber, string division, int? subDivision){     return GetParts(partialPartNumber, division, subDivision, null); }  public List<InventoryPart> GetParts(string partialPartNumber, string division, int? subDivision, bool? isActive){     // This method is the one that actually calls the client proxy channels and all. } 

My client app consumes the Client Service Layer

public void LoadPartNumbers(){     var parts = ClientServiceLayer.GetParts(this.txtPartNumber.Text, null, (int) this.cboDivisions.SelectedItem ); } 
like image 115
DanielCuadra Avatar answered Sep 20 '22 20:09

DanielCuadra