Click here to Skip to main content
14,734,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys,

This is very Important !!

I am working on the web service to WCF application in which I have many classes defined in the following pattern.

public class Foo
{
        private string _foo1;
        private string _foo2;

       public string Property1
       {
           get { return _foo1; }
           set { _foo1= value; }
       }

       private string _chkSubsidiaryCompany;
       public string Property2
       {
           get { return _foo2; }
           set { _foo2= value; }
       }

       private List<string> _foo3=new List<string>();
       public List<string> Perperty3
       {
           get { return _foo3; }
           set { _foo3= value; }
       }      
}


So as per WCF 4.0 we are not required to set each property / class with DataMember / DataContract respectively.

When I consume the Foo object in the client side the Lists are not getting initialized with the empty container but with 'Null'

As I have a huge code base, I don't want to decorate the entities with DataContract / DataMember.

Concern: How do I convert the null initialized collection at client side to a empty container?
I just want to remove the Null assigned to any collection at the client side after serialization.



Things I have tried already.

1. Initializing the colelction int the entity constructor.
2. Changed the service configuration to System.Collection.Generic List while adding the web reference in the client.

In the interest of time, Please Help

Thanks in advance.

"Happy Coding"
Posted
Comments
Pheonyx 9-Aug-13 9:30am
   
I don't know if this will work, hence it being a comment not a solution but if you changed the get in the property from:

get { return _foo3; }

to

get { return _foo3 ?? new List<string>(); }

would that solve your issue
TapasU 10-Aug-13 1:54am
   
Hey.. This one is not working.. still getting Null in the list :(.
Any other workaround do you know? This I feel is one of the basic stuff but Iwas really surprised I could not find any relevant posts on the internet.
virusstorm 9-Aug-13 10:05am
   
I think the issue you are running into is that while you don't need to use the attributes, using them actually strong type the objects which allows you to reuse the types on the client side if you have reference to the common DLL.
TapasU 10-Aug-13 1:55am
   
Hey, I tried using the attributes too still the list on the cient side is Null :(. Any other workaround?

Hi All,
Adding a null check in the property worked for me. I don't understand somehow the same code was not serving my purpose before but it did eventually in the end.

Thanks anyways for the responses.

private List<string> _foo1 = new List<string>();
public List<string> MyProperty
{
    get { return _foo1; }
    set { if (value != null) _foo1= value; }
}


"Happy Coding"
   
Why don't you just specify the data contracts and data members? It ain't much of a hassle.

You could also remove any try catches that return null in the contract implementation file.

I'd also initialize the _foo1 and foo2 to some value or string.Empty
   
v2
Comments
TapasU 12-Aug-13 0:03am
   
Hi.. Thanks for the response. Howver, even if I mention DataContract / DataMember in my entity classes. The List still returns Null where I am expecting an empty container (new List()).
Have you encountered such situation? I think this has something to do with the DataContractSerializer used by the WCF because the same piece of code works very well with the Web services which uses XmlSerialization. Thanks. Any help would be appreciated.
Member 9549287 12-Aug-13 4:37am
   
as of now I can't really tell what's wrong with your code unless i see a snippet. the specific function seems to always return null. a common problem when an implementation similar to this:
Member 9549287 12-Aug-13 4:37am
   
<pre lang="c#">

public object somefunction()
{
var res = new object();
...

try
{
...
//some code that for some reason always results in an exception
...
return res;
}
catch
{
return null;
}
}

</pre>

I'm not sure which your case is, but you could try using the wcftestclient with breakpoints in your code to know exactly where the problem occurs.
TapasU 12-Aug-13 7:08am
   
Tried many things, still not working. I am wondering how come we don't have any standard way to initialize DataMembers in WCF? Or is this a known bug / missing funcitonality in WCF?
Member 9549287 12-Aug-13 9:07am
   
in your client app, have you tried right clicking on the service reference you added, gone to 'configure service reference' . In 'Collection Type' you may try selecting 'System.Collections.Generic.List' for your case

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900