你可以在一个WCF服务方法使用可选参数? [英] Can you use optional parameters in a WCF service method?

查看:281
本文介绍了你可以在一个WCF服务方法使用可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过像这并的this 但他们各自几年的历史。



我可以做这样的事情?

  [OperationContract的] 
[FaultContract(typeof运算(MyCustomFault))]
名单< InventoryPart>的SelectMany(字符串partialPartNumber,弦格= NULL);


解决方案

您不能。上有WCF有关方法签名诸多限制;一些限制是因为主机的机制,以及其他因的WSDL / MEX。



尽管WCF可能让你在你的服务代码默认参数和重载方法和许多其他的事情,当你托管的服务,可能还是无法启动,也可能启动,但可能还是无法正常工作。这是棘手。



我所做的,以克服这个问题,是我用空的参数的任何需要的地方,然后在我的客户端代码,我总是有这种对服务的访问层我的自动生成的客户端代理;我的服务层具有所有我想要的重载和可选PARAMS。示例(脏代码):



WCF服务:



  [OperationContract的] 
[FaultContract(typeof运算(MyCustomFault))]
名单,LT; InventoryPart>的SelectMany(字符串partialPartNumber,弦师,诠释细分,BOOL isActive?);



客户服务层(未自动生成的代理,而是一个由我写的)

 公开名单< InventoryPart> GETPARTS(字符串partialPartNumber){
返回GETPARTS(partialPartNumber,NULL);
}

公开名单< InventoryPart> GETPARTS(字符串partialPartNumber,弦师){
返回GETPARTS(partialPartNumber,分裂,NULL);
}

公开名单< InventoryPart> GETPARTS(?字符串partialPartNumber,弦师,诠释分区){
返回GETPARTS(partialPartNumber,分裂,细分,NULL);
}

公开名单< InventoryPart> GETPARTS(字符串partialPartNumber,弦师,诠释?细分,布尔?isActive){
//此方法是实际调用客户端代理渠道和所有的人。
}

我的客户端应用程序将在客户端服务层

 公共无效LoadPartNumbers(){
VAR部分= ClientServiceLayer.GetParts(this.txtPartNumber.Text,空,(INT )this.cboDivisions.SelectedItem);
}


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

解决方案

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

这篇关于你可以在一个WCF服务方法使用可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆