WHY this does not work for the ContactData[] array property !? the received array is always empty!!!
WCF can serialize ContactData whithout any problem, but not a simple array of ContactData !?!? this is crazy o_O
What is the simplest and fastest way to send correctly this collection of ContactData through a wcf call ??
[DataContract]
public class MessageData
{
[DataMember]
public ContactData From { get; set; }
[DataMember]
public ContactData[] To { get; set; }
[DataMember]
public string MessageText { get; set; }
}
[DataContract]
public class ContactData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
}
It turned out to be my service reference that was not up to date, i deleted and recreated the service reference and it just worked fine, sorry for my stupidity...
I misunderstood something I had read about serialization of collections and thought there was something different to do here.
try
public List<'ContactData> To { get; set; }
remove ' in <'
Related
Im working on ASMX service that allows me to work with databases and their tables. The schema looks like this
[DataContract]
public class DataBase
{
[DataMember]
public string Name { get; set; }
[DataMember]
public List<Table> Tables { get; set; }
...
}
[DataContract]
public class Table
{
[DataMember]
public string Name { get; set; }
[DataMember]
public List<Column> Columns { get; set; }
[DataMember]
public List<List<object>> Data { get; set; }
...
}
[DataContract]
public class Column
{
[DataMember]
public string Name { get; set; }
[ DataMember]
public string Type { get; set; }
...
}
"Data" field is meant to keep a table of all values of different types. The problem is - i have to work with a few of custom types:
public class Email
{
[DataMember]
public string address { get; set; }
...
}
public class Strings : List<string>
{
public Strings(IEnumerable<string> collection) : base(collection) { }
...
}
And the problem is that web service does not create references to these types. I do not use them in methods explicitly, but store in table. Using KnownType and DataContract did not help me, and when i created same classes in a client app, i have exceptions. Please, help?
I fixed it: basically i created dummy methods that returned object of nedded types, deleted the method references from client app and it worked.
I have an object called Organization that represents different rows in a database table called ORGANIZATION. Some organizations represent merchants and the merchant can have a "MerchantAux" record.
The Organization class looks like this:
[DataContract]
public class Organization : Core.Framework.BaseEntity
{
[Key]
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public virtual MerchantAux MerchantAuxRecord { get; set; }
public Organization()
{
}
}
The MerchantAux class looks like this:
[DataContract]
public class MerchantAux : BaseEntity
{
[Key]
[DataMember]
public int Code { get; set; }
[DataMember]
public int SettlementDelay { get; set; }
[DataMember]
public Nullable<System.Guid> WalletSiteId { get; set; }
[DataMember]
public bool? AddCardNotPresent { get; set; }
[DataMember]
public bool? BatchEmailNotification { get; set; }
[DataMember]
public string CsvEmailRecipients { get; set; }
[IgnoreDataMember]
public virtual ICollection<Organization> Organizations { get; set; }
public MerchantAux()
{
Organizations = new List<Organization>();
}
}
These objects are defined in a Models assembly. The WCF Service Assembly has a ServiceContract and OperationContracts to Get and Save Organizations.
Getting an organization works without issue. Saving an organization is failing with a VERY strange result.
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter boardingservice:organization. The InnerException message was 'The use of type 'MerchantAux' as a get-only collection is not supported with NetDataContractSerializer. Consider marking the type with the CollectionDataContractAttribute attribute or the SerializableAttribute attribute or adding a setter to the property.'. Please see InnerException for more details.
And there is no inner exception.
Oh yeah... Soap UI has no problem calling the Save method, but a .Net client throws the above error. Has anyone else seen this behavior where the serializer thinks that an object is a collection?
For the curious, I did try changing it to CollectionDataContract instead, but of course it fails straight away with an exception saying that MerchantAux doesn't implement IEnumerable (duh). Anyway... any ideas would be great.
[Edit]: I added the TokenType enum, what caused the whole issue...
I have an issue using WCF and unfortunately I didn't find any useful help.
I am creating a WCF based application. When the server responds to the client's request, I want the send back the following class:
[DataContract]
public enum TokenType
{
User,
Device
}
[DataContract]
public class AuthenticationResponse
{
[DataMember]
public LogonStatus Status { get; set; }
[DataMember]
public AccessToken Token { get; set; }
}
[DataContract]
public struct AccessToken
{
[DataMember]
public string TokenID
{
get;
set;
}
[DataMember]
public TokenType Type
{
get;
set;
}
[DataMember]
public string Uid
{
get;
set;
}
[DataMember]
public string Name
{
get;
set;
}
[DataMember]
public DateTime ExpirationTime
{
get;
set;
}
[DataMember]
public DateTime GenerationTime
{
get;
set;
}
[DataMember]
public bool IsExpired
{
get
{
return DateTime.Now > this.ExpirationTime;
}
}
}
When I send the AuthenticationResponse back to the client, it always fails.
My qusetion: Is there any chance to use class/struct objects within DataContract object or do I have to replace the AccessToken object with basic types (e.g. string) in the AuthenticationResponse object?
Thanks all your helps!
Best regards
Gabor
The problem is your public bool IsExpired has no setter and thus cause problems while serializing the object.
A workaround is to set a protected/private setter to your property with an empty body (or replace it by a method)
[DataMember]
public bool IsExpired
{
get
{
return DateTime.Now > this.ExpirationTime;
}
set
{
/* Dummy setter for serialization fix */
}
}
You can find more information about Serialization here : https://msdn.microsoft.com/en-us/library/182eeyhh.aspx
More specifically :
Items That Can Be Serialized
The following items can be serialized using the XmLSerializer class:
Public read/write properties and fields of public classes
Ahh...
Sorry for that. I was really stupid... I forgot to paste the TokenType enum in my original question what is part of AuthenticationResponse class, and this was the problem... I forget the set the [EnumMember] attributes...
After I added, everything worked well.
Sorry for this stupid and really beginner problem...
Thanks all your helps!!!
How can I serialize the given object into JSON but only include properties with the [DataMember] attribute.
User MyUser = new User();
string MessageJson = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(MyUser);
public class User
{
[DataMember]
public string username { get; set; }
public string password { get; set; }
}
You need to use DataContractJsonSerializer for that.
Note that, I think you'll also need DataContract attribute on the class.
You can use JSON.Net.
If a class has many properties and you only want to serialize a small subset of them then adding JsonIgnore to all the others will be tedious and error prone. The way to tackle this scenario is to add the DataContractAttribute to the class and DataMemberAttributes to the properties to serialize. This is opt-in serialization, only the properties you mark up with be serialized, compared to opt-out serialization using JsonIgnoreAttribute.
[DataContract]
public class Computer
{
// included in JSON
[DataMember]
public string Name { get; set; }
[DataMember]
public decimal SalePrice { get; set; }
// ignored
public string Manufacture { get; set; }
public int StockCount { get; set; }
public decimal WholeSalePrice { get; set; }
public DateTime NextShipmentDate { get; set; }
}
You can place the [ScriptIgnore] attribute on the properties that you do not want to include in your result.
I'm using Openrasta framework. I've simple POCO which is used in my API and this will be sent as ResponseResource to client. It looks like below:
Public class User
{
Public int Id { get; set; }
Public string Name { get; set; }
Public string Code { get; set; }
}
When sending response to user I dont want to send property "Id" back to the user. How can I make openrasta serialzers to ignore this property? I tried putting XmlIgnore attribute for this property but it didn't work.
Any ideas?
Since [XmlIgnore] isn't working, I am guessing you are using either the Json or XmlDataContract codecs. These are based on DataContractSerializer, in which case the mechanism to control the serialization is to mark the type as [DataContract], at which point inclusion becomes opt in rather than automatic, i.e.
[DataContract]
public class User
{
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Code { get; set; }
}