简体   繁体   English

WCF服务+向/从服务和客户端传递和接收相同的对象

[英]WCF service + passing and receiving same object to/from service & client

Please I need best practices for WCF in my case below, 请在下面的情况下为WCF提供最佳做法,

I need to send the object instance to WCF service, WCF service processes and updates the same object and sends back to client. 我需要将对象实例发送到WCF服务,WCF服务处理并更新同一对象,然后发送回客户端。

So with best practice should i need to maintain 2 object instances? 因此,按照最佳实践,我是否需要维护2个对象实例? or I can use same instance and update and send back. 或者我可以使用同一实例并进行更新并发回。 Please advise. 请指教。

see below code for how I am writing currently. 请参阅以下代码,了解我目前的写作方式。 please help. 请帮忙。

[OperationContract]   
 Customer DoProcess(Customer customer);

As you don't want to persist the sent object then there is no need to maintain two objects. 由于您不想持久保存已发送的对象,因此无需维护两个对象。 It is always advisable to not to even create multiple references, nor objects. 始终建议不要创建多个引用或对象。

In case of Objects you will have a separate copy, that means more memory, and in reference variable also you will waste 4 to 8 bytes + CPU overhead. 对于对象,您将拥有一个单独的副本,这意味着更多的内存,并且在引用变量中,您还将浪费4到8个字节+ CPU开销。

With the principle of memory management, I recommend you update the same and return back. 根据内存管理的原则,我建议您对其进行更新并返回。

Consider the following scenario: client calls the WCF servcice function and passes a customer as parameter. 请考虑以下情形:客户端调用WCF服务函数并传递客户作为参数。

Customer c = new Customer();
WCFProxy.DoProcess(c);

Now the object will be serialized and sent to the wcf service. 现在,该对象将被序列化并发送到wcf服务。 the service will now execute the DoProcess function as follows; 服务现在将按以下方式执行DoProcess函数;

Customer DoProcess(Customer customer){
customer.name = 'abc';
return customer;
}

this will not update the customer passed by the client. 这不会更新客户传递的客户。 It will return a new customer that will be serialized and passed to the client. 它将返回一个新客户,该客户将被序列化并传递给客户端。

So, on the client you may set your customer instance to the returned customer as such: 因此,可以在客户端上将客户实例设置为返回的客户,如下所示:

c = WCFProxy.DoProcess(c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM