datacontract

Passing an instance of anonymous type over WCF

て烟熏妆下的殇ゞ 提交于 2019-11-29 11:11:00
问题 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

Custom serialization with DataContractSerializer

戏子无情 提交于 2019-11-29 06:27:58
I'm currently using wrapper classes for my DataSets ,in order to implement custom serialization. I would like to use DataContractSerializer (more like have to use it) but still support the custom serialization. The problem is that the [DataContract] and [Serializable] attributes don't seem to get along so well... how could I override the serialization, and support BOTH DataContract & ISerializable serialization? The code for the wrapper DataSet class is brought here: [Serializable()] [System.Runtime.InteropServices.ComVisible(false)] public class TestDatasetWrapper : TestDataSet, ISerializable

How to serialize/deserialize a C# WCF DataContract to/from XML

﹥>﹥吖頭↗ 提交于 2019-11-29 05:59:40
I am developing a WCF service which will be consumed by multiple different client applications. In order to make one functionality work, the server needs to read an XML file into a C# DataContract which is then passed on to the concerned client. As far as I understand from the MSDN website, this is possible but I couldn't find any complete examples. In particular, the website talks about a 'stream' parameter which I don't quite get yet. My data contract has one property field which is a list of another data contract which has multiple simple property fields. e.g. [DataContract] public class

DataContract serialization exception (data contract name is not expected)

浪子不回头ぞ 提交于 2019-11-29 02:28:15
问题 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

WCF, Entity Framework & Data Contracts

喜欢而已 提交于 2019-11-29 02:23:24
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 avoid Entity Framework classes from being exposed in this manner? Note : This is not a duplicate of How

How to serialize a derived type as its base type with WCF

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 21:33:12
I have a common library with some objects in it. Then I have a service project that references the common library and creates some derived types from objects in the common library. I want my service to serialize the derived types as their base types defined in the common library. I cannot use KnownTypes on the objects in the common library because I don't want the common library referencing the service assemblies. So how can I have wcf serialize the derived types as their base types? I wish I could do something like... [DataContract(SerializeAsType = typeof(BaseType))] public class DerivedType

Generally accepted way to avoid KnownType attribute for every derived class

落花浮王杯 提交于 2019-11-28 21:20:30
Is there a generally accepted way to avoid having to use KnownType attributes on WCF services? I've been doing some research, and it looks like there are two options: Data contract resolver NetDataContractSerializer I'm not a big fan of having to statically add KnownType attributes every time I add a new type, hence wanting to avoid it. Is there a third option that should be used? If so, what is it? If not, which of the above two options are the right way to go? Edit - use a method A third option would be to use reflection [DataContract] [KnownType("DerivedTypes")] public abstract class

Naming Generic DataContracts in WCF

我只是一个虾纸丫 提交于 2019-11-28 20:29:50
I am using a Generic Class as a Response Data Contract. All is good and this is streamlining the design of my WCF service significantly. Each request is given a standard response object with the following signature: Status (Enum) Message (String) Result (T) Below is the Response Class: [DataContract] public class Response<T> { public Response() {} public Response(T result) { this.result = result; if (result != null) { this.status = Status.StatusEnum.Success; } else { this.status = Status.StatusEnum.Warning; } } public Response(T result, Status.StatusEnum status) { this.status = status; this

WCF Service returning 400 error: The body of the message cannot be read because it is empty

强颜欢笑 提交于 2019-11-28 11:00:01
问题 I have a WCF service that is causing a bit of a headache. I have tracing enabled, I have an object with a data contract being built and passed in, but I am seeing this error in the log: <TraceData> <DataItem> <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error"> <TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx</TraceIdentifier> <Description>Throwing an exception.</Description>

Write a webservice method with both URI and DataContract class parameters

纵然是瞬间 提交于 2019-11-28 05:42:40
问题 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