问题描述
限时送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:关键词]
发布评论