I'm asking this out of ideas. Looked on Google a lot, did some tests myself but nothing.
I'm building some widely used WCF webservices which will be called both from .NET 2.0 clients through ASMX service reference and some newer .NET 4.5.2 clients thought a WCF service reference (Yes, I made them interoperable by just changing the endpoint in the config file).
After quite some research, the most useful stuff I found is THIS, which basically says that after .NET 3.5 SP1, you basically don't need to specify the DataContract
because the framework will take care of it if you don't specify it, automatically making the data contract for custom classes. The downside is that you have to renounce to some advanced features and customizations.
This is true as I tried to make a test project with .NET 4.5.2 with some random objects and all works fine without the need of specifying the data contract.
I then tried to pass the project to .NET 3.0 and it was still working with any data contract ever....
Moreover, from the link I passed:
without [DataMember], you cannot serialize non-public properties or fields
Which happens to be not true either
Questions are:
1) Can I Specify the DataContract
on a separate project and NOT into the WCF? (Example: Adding the DataContract
to the objects in the class library that contains the objects that client and webservice must exchange, and expect the WCF service to understand that that object has a datacontract to use)
2) Why use DataContract
if even in it's not necessary and I don't need fancy stuff? Can I go the easy way?
3) Why if I convert a project from 4.5.2 to 3.0 and vice versa the WCF continues working seamlessly? Shouldn't the DataContract
s be compulsory before .NET 3.5 SP1?
4) Both with DataContract
or not, I keep seeing the private fields of the objects... while guidelines says I shouldnt... WHAT?
I Really need an answer to the first one.
Here's the code of the project I used to make tests
Class library with custom objects. This is referenced in WCF and in a WinForms test client
public class MyTestObjs
{
public class TestObject1
{
public string Test1 { get; private set; }
public List<int> Test2 { get; private set; }
private TestObject2 Test3 { get; set; }
public TestObject1()
{
this.Test1 = "HELLO " + new Random().Next(2333);
this.Test2 = new List<int> { 1, 2, 3, 43, 544, 64 };
this.Test3 = new TestObject2();
}
}
private class TestObject2
{
public string Test1 { get; private set; }
public List<int> Test2 { get; private set; }
public TestObject2()
{
this.Test1 = "YYYYYYYY " + new Random().Next(2333);
this.Test2 = new List<int> { 1111, 2222, 3333, 4443, 55544, 64344 };
}
}
}
WCF Service exposed methods:
public string ReturnComplexTest1(ComplexObjTest.MyTestObjs.TestObject1 test)
{
string result = String.Empty;
result += test.Test1 + "_____";
foreach (int x in test.Test2)
{
result += x.ToString() + " - ";
}
return result;
}
public ComplexObjTest.MyTestObjs.TestObject1 ReturnComplexTest1Obj()
{
return new ComplexObjTest.MyTestObjs.TestObject1();
}
Winforms Test Client with service reference
public string ReturnComplexTest1(ComplexObjTest.MyTestObjs.TestObject1 test)
{
string result = String.Empty;
result += test.Test1 + "_____";
foreach (int x in test.Test2)
{
result += x.ToString() + " - ";
}
return result;
}
public ComplexObjTest.MyTestObjs.TestObject1 ReturnComplexTest1Obj()
{
return new ComplexObjTest.MyTestObjs.TestObject1();
}
As you can see from debugging with all the projects set to .NET 3.0 framework and NO DATACONTRACTS ANYWHERE IN THE CODE, this is the result:
I didnt' need to specify any datacontract, and I CAN SEE private fields... This doesn't sound right but it works flawlessly bot in 4.5.2 and 3.0 NET Framework. What am I missing?