Juan
Juan

Reputation: 15725

array passed as parameter to a WCF service to be filled up comes back full of null's

I'm trying to write a WCF pipe service. One of my service's methods will receive an object[] as a parameter and fill it up, emulating IDataReader's GetValues method. But whenever I call this method from my client I get a full of null values array. I also tried changing the object[] parameter to ref object[] but nothing changes.

I've been doing some tests after first writing this question and this is weirder than I first though. The hole problem seems to occur in the client's side. Here is some code (all taken from client side):

object[] values = new object[reader.FieldCount];
// values is filled up with null's
reader.GetValues(values);
// values is again filled up with null's (see code bellow)

This is GetValues, called by the code above:

public int GetValues(object[] values)
{
    // values is filled out with null's, naturally
    return DatabaseProxy.Instance.GetValues(_ConnectionId, _ReaderId, ref values);
    // values has content here! (values[0] == "hello this is a string")
}

The GetValues method in DatabaseProxy.Instance is my server's method, which takes a ref parameter because otherwise it would return an array full of nulls. By using the ref modifier, now it does return values, but then these values disappear inside my client. Any idea what's going on here?

Upvotes: 0

Views: 1490

Answers (2)

Chris
Chris

Reputation: 3162

Edit

Scrapped my previous answer and looked at google and other SO questions and came up with this

It seems you can use ref, but you have to declare it in your service contract correctly for it to be used by WCF.

See the section Out and Ref Parameters

In which case your service side method should be declared

public int GetValues(int connectionId, int readerId, ref values)

Upvotes: 3

Darren Kopp
Darren Kopp

Reputation: 77657

Not exactly sure, but 99.9% sure that ref won't work in WCF the same way it does in memory. Return the array as your output rather than using a ref parameter.

Upvotes: 0

Related Questions