Designing WCF interface: no out or ref parameters

2020-03-24 01:10发布

I have a WCF service and web client. Web service implements one method SubmitOrders. This method takes a collection of orders. The problem is that service must return an array of results for each order - true or false. Marking WCF paramters as out or ref makes no sense. What would you recommend?

[ServiceContact]
public bool SubmitOrders(OrdersInfo)

[DataContract]
public class OrdersInfo
{
  Order[] Orders;
}

标签: c# .net
6条回答
不美不萌又怎样
2楼-- · 2020-03-24 01:37

Special class that holds the order and the true/false, or an array of tupels.

查看更多
聊天终结者
3楼-- · 2020-03-24 01:43

Yes, it makes sense returning an out parameter on WCF operations. On response, the SOAP message will contain the element passed back.

There are good content on MSDN about data transfer: Specifying Data Transfer in Service Contracts

Also, you need to use the OperationContractAttribute (not ServiceContractAttribute) on the SubmitOrders.

查看更多
相关推荐>>
4楼-- · 2020-03-24 01:45

Well, if you want to avoid out and ref parameters, you could always return an array of the IDs of the orders that were successfully submitted.

查看更多
▲ chillily
5楼-- · 2020-03-24 01:59

Use complex type(another class with DataContract attribute) in return.

Like

[ServiceContact]
public OrdersResult SubmitOrders(OrdersInfo)

[DataContract]
public class OrdersInfo
{
  Order[] Orders;
}

[DataContract]
public class OrdersResult
{
  .....
}

Also add DataMember on Order[] Orders;

查看更多
迷人小祖宗
6楼-- · 2020-03-24 01:59

The method would look like:

public OrdersInfo SubmitOrders(OrderInfo orders){
}

where each item in the OrderInfo will have a SubmissionStatusInfo like:

class SubmissionStatusInfo{
 enum Status  { get; set; }
 string Message { get; set; }
}

where Status : Submitted, Failed, Error etc.
Message : a string giving some additional information about the status...

HTH

查看更多
老娘就宠你
7楼-- · 2020-03-24 02:00

Marking WCF paramters as out or ref makes no sense.

out parameters do make sense in WCF.

What would you recommend?

I recommend to use out parameters.


Note 1: It will move your out parameter to be the first parameter on you.

Note 2: Yes you can return objects with complex types in WCF. Tag your class with an attribute of [DataContract] and your properties with an attribute of [DataMember].

查看更多
登录 后发表回答