datacontract

Why is using [DataMember(EmitDefaultValue = false)] not recommended?

无人久伴 提交于 2019-11-30 13:36:56
问题 In WCF you can define a contract using the [DataContract] and [DataMember] attributes, like this: [DataContract] public class Sample { [DataMember(EmitDefaultValue = false, IsRequired = false)] public string Test { get; set; } } This article on the MSDN states that using EmitDefaultValue = false is not recommended: However, i like to use this, because the XML that is generated using this construction is cleaner. Not specifying this setting results in: <Sample> <Test xsi:nil="true"/> </Sample>

Is there a way to export an XSD schema from a DataContract

对着背影说爱祢 提交于 2019-11-30 09:06:08
I'm using DataContractSerializer to serialize/deserialize my classes to/from XML. Everything works fine, but at some point I'd like to establish a standard schema for the format of these XML files independent of the actual code. That way if something breaks in the serialization process I can always go back and check what the standard schema should be. Or if I do need to modify the schema the modification is an explicit decision rather then just a later affect of modifying my code. In addition, other people may be writing other software that may not be .NET based that would need to read from

Passing an instance of anonymous type over WCF

ε祈祈猫儿з 提交于 2019-11-30 08:10:21
I have a WCF service method that expects an object and then retrieves its properties using reflection. On the client side I create an anonymous type object var obj = new {FirstName="John", LastName="Doe"} and pass it to the method. I'm getting an exception: Type '<>f__AnonymousType0`2[System.String,System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types. I can't mark the type or its

Why is using [DataMember(EmitDefaultValue = false)] not recommended?

自闭症网瘾萝莉.ら 提交于 2019-11-30 07:54:39
In WCF you can define a contract using the [DataContract] and [DataMember] attributes, like this: [DataContract] public class Sample { [DataMember(EmitDefaultValue = false, IsRequired = false)] public string Test { get; set; } } This article on the MSDN states that using EmitDefaultValue = false is not recommended: However, i like to use this, because the XML that is generated using this construction is cleaner. Not specifying this setting results in: <Sample> <Test xsi:nil="true"/> </Sample> while using the setting the element is ommited when there is no value: <Sample> </Sample> I'm curious

WCF, Entity Framework & Data Contracts

微笑、不失礼 提交于 2019-11-30 07:02:57
问题 Using VS 2008 & .NET 3.5 SP1: I am using WCF to allow clients to connect to a service that reads and writes database entries using Entity Framework. By default the entities that are generated automatically from the database have the DataContract attribute applied. Unfortunately many of the fields are exposed are not meant for consumption by the client (i.e. - records of who is accessing what data, etc.) and for security reasons I would rather keep them from being exposed. Is there any way to

DataContract serialization exception (data contract name is not expected)

感情迁移 提交于 2019-11-30 04:38:02
I have the following code: [DataContract] class TestContract { private String _Name; private Int32 _Age; [DataMember( Name = "Name" )] public String Name { get { return _Name; } set { _Name = value; } } [DataMember( Name = "Age" )] public Int32 Age { get { return _Age; } set { _Age = value; } } } [Serializable] public class DNCJsonDictionary<K, V> : ISerializable { Dictionary<K, V> dict = new Dictionary<K, V>(); public DNCJsonDictionary() { } protected DNCJsonDictionary( SerializationInfo info, StreamingContext context ) { } public void GetObjectData( SerializationInfo info, StreamingContext

Clone Whole Object Graph

懵懂的女人 提交于 2019-11-30 04:04:39
While using this code to serialize an object: public object Clone() { var serializer = new DataContractSerializer(GetType()); using (var ms = new System.IO.MemoryStream()) { serializer.WriteObject(ms, this); ms.Position = 0; return serializer.ReadObject(ms); } } I have noticed that it doesn't copy the relationships. Is there any way to make this happen? Simply use the constructor overload that accepts preserveObjectReferences , and set it to true: using System; using System.Runtime.Serialization; static class Program { public static T Clone<T>(T obj) where T : class { var serializer = new

WCF Client having problems recognizing ServiceKnownTypes?

旧时模样 提交于 2019-11-30 04:02:05
问题 How would I tell the WCF service what KnownTypes to use when passing data back to the client? I know I can use the [ServiceKnownType] attribute, which makes the service call run fine from a WCF Test Server, however it still fails from the client. Am I missing something here? [OperationContract] [ServiceKnownType(typeof(SubClassA))] [ServiceKnownType(typeof(SubClassB))] BaseClassZ GetObject(); Error message from client is: {"Element 'http://schemas.datacontract.org/2004/07/BaseClassZ' contains

WCF: Data contract being converted to message contract

Deadly 提交于 2019-11-29 23:30:01
问题 My WCF service exports a single operation, marked with the catch-all action and reply action so that it represents a common entry point to the service: [ServiceContract] public interface IService { [OperationContract (Action="*", ReplyAction="*")] Message MyMethod (Message msg); } Client proxies are still generated as data contracts. What I'm finding, however, is that despite the client sending a data contract, when msg is serialized, the body appears to be the equivalent message contract to

Write a webservice method with both URI and DataContract class parameters

独自空忆成欢 提交于 2019-11-29 12:25:00
How can I get a web service method to access parameters in the URI and complex POST data? I have a web service method with some parameters in the URI: "/api/{something}/{id}" I also have a class marked with DataContract/Member attributes that I want the webservice method to accept. Note that my class contains a subclass also marked with DataContract/Member attributes. [DataContract] public class MoreData{ [DataMember] public string SomeString { get; set; } [DataMember] public SubClass SubData {get; set;} } My method declaration looks like: [OperationContract] [WebInvoke( Method = "POST",