更新传入的对象的 WCF 方法

编程入门 行业动态 更新时间:2024-10-27 16:31:52
本文介绍了更新传入的对象的 WCF 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我认为如果我有一个 WCF OperationContract 接受一个对象并且需要在该对象上设置一个属性以便客户端获取更新,我是否需要声明它以返回该对象.

Am I correct in thinking that if I have a WCF OperationContract takes in an object and needs to set a property on that object so the client gets the update, I need to declare it to return the object.

例如给定数据合同:

[DataContract]
public class CompositeType
{
    [DataMember]
    public int Key { get; set; }

    [DataMember]
    public string Something { get; set; }
}

这不适用于 WCF:

public void GetDataUsingDataContract(CompositeType composite)
{
  composite.Key = 42;              
}

这会起作用:

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
  composite.Key = 42;

  return new CompositeType
  {
    Key = composite.Key,
    Something = composite.Something
  };           
}

推荐答案

IMO,创作通过副作用产生输出的方法是一件坏事".话虽如此,但是否存在需要这种模式的情况?是的.

IMO, authoring methods that produce output via side-effects is a "bad" thing. Having said that however, are there circumstances that necessitate this model? Yes.

当然 C# 编程模型允许这样做,WCF 是否已损坏?不.在某一时刻,人们必须意识到他们正在使用 WCF,并且作为一个框架,它试图满足大多数用例[例如,复制所有往返的所有输入参数以保留隐式副作用语义 一句话,就是傻].

Certainly C# programming model permits this, is WCF broken? No. At a certain point, one must realise they are consuming WCF, and as a framework it attempts to satisfy a majority of use-cases [for instance, replicating all input parameters on all round trips to preserve implicit side effect semantics is, in a word, silly].

当然,有一些方法可以解决这个问题 - C# 还提供了这些场景的显式声明,WCF 也支持这些!

Of course, there are ways to work around this - C# also provides for explicit declaration of these scenarios and WCF supports these as well!

例如

// use of "ref" indicates argument should be returned to 
// caller, black-eye and all!
public void GetDataUsingDataContract (ref CompositeType composite) 
{
    composite.Key = 42;         
}

试一试!

希望这有帮助:)

这篇关于更新传入的对象的 WCF 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 19:00:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   方法   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!