【问题标题】:Passing null for parameters in wcf results in object reference not set to an instance of an object在 wcf 中为参数传递 null 会导致对象引用未设置为对象的实例
【发布时间】:2014-03-27 07:00:43
【问题描述】:

我尝试了很长时间没有运气的解决方案。

我有带有以下接口的 netnamedpiped 服务

[OperationContract]
[ServiceKnownType(typeof(SubTable))]
float GetValue(Table table);

当我在客户端用 null 调用它时 proxy.GetValue(tbl);

我得到 null expcetion,因为 wcf 不能序列化 null 我希望能够通过执行以下操作来传递 null:

if  (tbl!= null)
var result = proxyGetValue(tbl);
else
var result = proxyGetValueWhenNull();

【问题讨论】:

  • 你打算用空输入得到什么输出?
  • @shree.pat18 只是服务器内部的逻辑

标签: c# wcf nullreferenceexception


【解决方案1】:

您是否尝试过将表格包装为另一个类的属性并设置属性 [DataMember(IsRequired=false/true)]?

[DataContract]
public class TableWrapper{
    [DataMember(IsRequired=false)]
    public Table Table{get;set;}
}

[OperationContract]
[ServiceKnownType(typeof(SubTable))]
float GetValue(TableWrapper tableWrapper);

if  (tableWrapper.Table!= null)
    var result = proxyGetValue(tableWrapper.Table);
else
    var result = proxyGetValueWhenNull();

【讨论】:

    猜你喜欢
    • 2015-09-09
    • 1970-01-01
    • 2013-10-13
    • 2012-07-06
    • 2019-03-23
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2014-09-02
    相关资源
    最近更新 更多