tags:

views:

144

answers:

6

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;
}
+1  A: 

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

TomTom
+1  A: 

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.

David M
@Captain: I'm not sure about WCF specifically, but SOAP services can use "out" parameters. I would guess WCF supports that since it makes sense at least with SOAP and TCP/IP.
Nelson
+2  A: 

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].

Brian R. Bondy
I agree, we've used them extensively in our own services. You could also mark the method as IsOneWay=False and pass back an array of whatever data you want to the caller rather than just a bool.
Jacob Ewald
Err.. I see. I thought that WCF serializes the class into messagesand the message is transfered to service. Out parameters assume someback message to client - am i right?
Captain Comic
Just don't add the OneWayAttribute! The default is Request/Response.
Erup
+1  A: 

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

Sunny
You can perform FaultException<SubmissionStatusInfo>. It's a "best practice" on passing back an error.
Erup
Yep, that makes more sense...
Sunny
+1  A: 

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;

Incognito
It's not needed to create another DataContract obj. Just pass as out parameter.
Erup
Both solutions will work. It depends from designed whether he wants to have out param or return complex type.
Incognito
+2  A: 

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.

Erup
Thank you. This is just a typo This was not copypaste, I just simplified the problem and typed code in a hurry.
Captain Comic