datacontract

DataContract, default DataMember value

自作多情 提交于 2019-12-21 06:59:44
问题 Is there a way to choose default values of attributes that are not in the xml file during deserialization? If the mAge attribute is not present in the xml file, I want to use a default value of 18. Is it possible ? [DataContract] public class Person { public Person () { } [DataMember(Name = "Name")] public string mName { get; set; } [DataMember(Name = "Age")] public int mAge { get; set; } [DataMember(Name = "Single")] public bool mIsSingle { get; set; } }; Edit to put the answer.

DataContracts with behavior

混江龙づ霸主 提交于 2019-12-21 04:21:38
问题 How bad is it? I have read countless articles and never created abstract DataContracts with behavior before, but it seems that doing so will solve an issue I am having that will prevent me from creating factories everywhere to determine a subclass implementation. My question is, will I be punished if I decide to add behavior to my data contracts? Of course they can't be consumed and are there to perform certain operations specific to that subclass type before invoking repository calls and

Returning XmlDocument from WCF service not working

天大地大妈咪最大 提交于 2019-12-20 06:17:05
问题 I am trying to update some WCF service methods that return strings to return XmlDocument objects. I've tried returning it as-is and encapsulating it in a datacontract object. Either way I'm hitting an error upon attempting to update the service reference. The error suggest encapsulating it in a datacontract with an operations contract which I am doing. Is there a trick to this? 回答1: There's a way to return a XmlDocument from WCF, but you need to use the XmlSerializer instead of the default

WCF DataMember List<> without enclosing element

拥有回忆 提交于 2019-12-20 02:15:18
问题 The following DataContract: [DataContract(Namespace = "http://namespace", Name = "Blarg")] public class Blarg { [XmlAttribute("Attribute")] public string Attribute{ get; set; } [DataMember(Name = "Record", IsRequired = false, Order = 4)] public List<Record> Record{ get; set; } } Serializes into this: <Blarg Attribute="blah"> <Record> <Record/> <Record/> <Record/> </Record> </Blarg> But I want this: <Blarg> <Record/> <Record/> <Record/> <Blarg/> The DataContractSerializer seems to be inserting

how to mark an interface as DataContract in WCF

十年热恋 提交于 2019-12-19 05:22:41
问题 i have two data classes which hold only data members(no functions). One is CallTask the other is SmsTask . These two classes have some common properties like ID , Tel . I put these common properties in a seperate interface class and i use this interface class in my project whenever appropriate. Now i added a WCFService to my project to share data between clients and server. Consider the following class design: public interface IGsmTask : IComparable { string TaskID { get; set; } string

Clone Whole Object Graph

只谈情不闲聊 提交于 2019-12-18 11:44:42
问题 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? 回答1: Simply use the constructor overload that accepts preserveObjectReferences , and set it to true: using System; using System.Runtime

Generally accepted way to avoid KnownType attribute for every derived class

 ̄綄美尐妖づ 提交于 2019-12-17 23:34:09
问题 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

Constructor in WCF DataContract not reflected on Client

本小妞迷上赌 提交于 2019-12-17 16:14:17
问题 I need to have some data members get some values when I create an instance of the DataContract on the client. This is not happening using constructors. I have searched through different forums and found we have to use [OnDeserializing] and [OnDeserialized] attributes. This is also not working. Can somebody suggest something here. The other alternative is creating constructors in the partial classes at the client side. I want to avoid that. Please find the code below: Server-side: Datacontract

Is it possible to use generic DataContract's from the client end?

二次信任 提交于 2019-12-13 14:32:58
问题 I know when you create a service you can create a generic DataContract: [DataContract(Name = "Get{0}Request") public sealed class GetItemRequest<T> where T : class, new() { ... } [DataContract(Name = "Get{0}Response") public sealed class GetItemResponse<T> where T : class, new() { ... } [ServiceContract] public void MyService : IMyService { [OperationContract] GetItemResponse<Foo> GetItem(GetItemRequest<Foo> request); } This generates a GetFooRequest and GetFooResponse definition for my WSDL.

How to pass an Excel file from a WinForms client to a WCF service and into an SQL Server table?

大憨熊 提交于 2019-12-13 09:37:32
问题 How to pass an Excel file from a WinForms client to a WCF service and into an SQL Server table? Can anyone provide any guidance, code, or advice? WCF service contract and implementation that will take an Excel file as a parameter Contract implementation should insert that Excel file into a varbinary(MAX) column in SQL Server. 回答1: Here is a post that addresses the WCF portion of your question. Once you get the file to the server you can use FileHelpers.net to parse that file into an object. I