Question

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);
Was it helpful?

Solution

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 );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top