c# - 可以在 WCF 服务方法中使用可选参数吗?

标签 c# wcf optional-parameters

<分区>

我看过类似 this 的帖子和 this但他们每个人都只有几岁。

我可以做这样的事情吗?

    [OperationContract]
    [FaultContract(typeof(MyCustomFault))]
    List<InventoryPart> SelectMany(string partialPartNumber, string division = null);

最佳答案

你不能。 WCF 在方法签名方面有很多限制;有些限制是因为主机机制,有些是因为 WSDL/MEX。

尽管 WCF 可能允许您在服务代码中使用默认参数和重载方法以及许多其他内容,但当您托管服务时,它可能会启动,也可能不会启动,或者它可以启动但可能会工作,也可能不会工作。这很棘手。

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

WCF 服务:

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

客户端服务层(不是自动生成的代理,是我写的)

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.
}

我的客户端应用使用客户端服务层

public void LoadPartNumbers(){
    var parts = ClientServiceLayer.GetParts(this.txtPartNumber.Text, null, (int) this.cboDivisions.SelectedItem );
}

关于c# - 可以在 WCF 服务方法中使用可选参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963789/

相关文章:

wcf - 请求/响应消息传递模式 - Azure 服务总线

Javascript 回调和可选参数

C# Web Api action方法自动解码查询参数

c# - 确定挂起的终结器源

c# - 是否可以在 Asp.net Core 解决方案的单独层中管理 DBContext 的注入(inject)

c# - EF、Code First、WCF => 空集合问题

wcf - 在Unity容器中注册WCF服务

c++ - 在 C++ 中通过引用传递可选参数

linux - 创建 bash 脚本以 ssh 到不同的系统并运行命令

c# - 从 azure blob 存储下载 zip 后无法访问该 zip