问题
I am new to WCF and Entity framework.
I have a class library "A" which contains DatabaseEntities.edmx
(Entity Framework objectContext
).
This library is exposing a class "B" which contains a function FunctionB
, internally using the entity objects.
I have taken this library "A" reference into a WCF web service and inside the IService.cs
- I have coded it like this:
[OperationContract]
void FunctionB_Proxy();
Without defining any DataContract
I have gone into Service1.cs
and implemented this function
as below:
public void FunctionB_Proxy()
{
ClassB x=new ClassB();//Class of ClassLibrary
x.FunctionB();
}
This works fine.
But my question: is DataContract
optional in WCF?
Thanks in advance..
回答1:
As of .NET 3.5 SP1 - yes, the [DataContract]
is no longer required.
If you omit it, then the class will be serialized like the XML serializer does: all public properties on your class will be serialized by default.
However: if you start using a [DataContract]
on your class or a [DataMember]
on one of your properties, then you must decorate all properties you want to serialize with a [DataMember]
.
So it's either : leave out those attributes entirely (use the default behaviour like the XML serializer uses), or then be specific and decorate the class with [DataContract]
and every property you want to have serialized with [DataMember]
. I prefer this second approach.
来源:https://stackoverflow.com/questions/16881450/entity-framework-data-contract