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

牧云@^-^@ 提交于 2019-12-12 07:45:35

问题


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);

回答1:


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 );
}


来源:https://stackoverflow.com/questions/22963789/can-you-use-optional-parameters-in-a-wcf-service-method

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