您可以在WCF服务方法中使用可选参数吗?
我看过这样的post和这个但是它们都是几年前的。
我可以这样做吗?
[OperationContract] [FaultContract(typeof(MyCustomFault))] List SelectMany(string partialPartNumber, string division = null);
你不能。 关于方法签名,WCF有许多限制; 一些限制是因为主机机制,而其他因为WSDL / MEX。
尽管WCF可能会让您在服务代码和重载方法以及许多其他方面拥有默认参数,但是当您托管服务时它可能启动或不启动,或者它可以启动但可能起作用或不起作用。 这很棘手。
我为克服这个问题所做的是,我在需要的地方使用可以为空的参数,然后在我的客户端代码上,我总是有一个访问我自动生成的客户端代理的服务层; 我的服务层有我想要的所有重载和可选参数。 示例(脏代码):
WCF服务:
[OperationContract] [FaultContract(typeof(MyCustomFault))] List SelectMany(string partialPartNumber, string division, int? subDivision, bool? isActive);
客户端服务层(不是自动生成的代理,而是由我编写的代理)
public List GetParts(string partialPartNumber){ return GetParts(partialPartNumber, null); } public List GetParts(string partialPartNumber, string division){ return GetParts(partialPartNumber, division, null); } public List GetParts(string partialPartNumber, string division, int? subDivision){ return GetParts(partialPartNumber, division, subDivision, null); } public List GetParts(string partialPartNumber, string division, int? subDivision, bool? isActive){ // This method is the one that actually calls the client proxy channels and all. }
我的客户端应用程序使用客户端服务层
上述就是C#学习教程:您可以在WCF服务方法中使用可选参数吗?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public void LoadPartNumbers(){ var parts = ClientServiceLayer.GetParts(this.txtPartNumber.Text, null, (int) this.cboDivisions.SelectedItem ); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1011520.html