Is the [DataMember] attribute needed on virtual properties?
[DataMember]
public string Title { get; set; }
[DataMember]
public virtual ApplicationUser User { get; set; }
or is this the correct way:
[DataMember]
public string Title { get; set; }
public virtual ApplicationUser User { get; set; }
This is not related to the property being virtual or not. If you want a property to be serialized (i.e. visible for a WCF client) you mark it as DataMember.
Keep in mind that if you do that, ApplicationUser should also be a DataContract, otherwise you get an exception like
Type 'ApplicationUser' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. ...
Related
I have create a simple user login WCF Services.I have using large number of datamember properties in one datacontract class.so i have split the datamember properties in different class. for example (Userdetails class with using userid,usertype,username,password,address,city,state,phoneno,mobileno,country,pincode,etc...)so i have split on the two Datacontract classes into userdetails as Userinfo and contactdetails class.The userinfo class only define the userdetails only and contactdetails class defined contact details as. how to use on the multiple datacontact class in WCF services can you please explain them.
I'm not sure what exactly you want. but i hope you could use something like this:
[DataContract]
public class User
{
[DataMember]
public userdetails Userinfo { set; get; }
[DataMember]
public contactdetails contactdetails { set; get; }
}
[DataContract]
public class userdetails
{
[DataMember]
public long userid { set; get; }
[DataMember]
public string usertype { set; get; }
}
[DataContract]
public class contactdetails
{
[DataMember]
public long mobileNumber { set; get; }
[DataMember]
public string phoneNumber { set; get; }
}
I do have a simple WCF service in which If I put the method with simple Data Type then I can access that service in the MVC project which is in same Solution. But if I change the Data Type of the Service method even to array or list of string or any other simple Data Type, I cannot access the service. Do I need to make any config changes.
[DataContract]
public class Property
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string PropertyPost { get; set; }
[DataMember]
public string PropertyType { get; set; }
[DataMember]
public string DealType { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public string ProjectName { get; set; }
}
I actually want to return the List from the WCF service for which I have created the Datacontract, but it is not working even with simple List Type.
Do we need to specify anything in Service like WebInvoke?
Can any one help?
I have a ServiceContract as such :
bool CreateSlideshow(Slideshow current, string path, string name);
When I run my program calling the service, I get the following exception :
There was an error while trying to serialize parameter
http://tempura.org/:current. The InnerException message was 'Type
'System.DelegateSerializationHolder+DelegateEntry' with data contract
name
'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System'
is not expected. Consider using a DataContractResolver or add any
types not known statically to the list of known types.....
Apparently the problem lies in Slideshow. My class Slideshow has the following members :
private String name;
private String path;
private List<Slide> slides;
and the class Slide has the following members :
private String title;
private ContentTypeEnum contentType;
private String textContent;
private String pictureContextPath;
where ContentTypeEnum is an enumeration.
Any idea how to resolve the exception? Hope for some advice/suggestions. Thanks in advance.
Your used types have to be marked with the [DataContract] attribute. The properties have to be marked with the [DataMember] attribute.
Furthermore your fields should be public properties, because a datacontract doesn't make any sense with private fields only.
[DataContract]
public class Slideshow
{
[DataMember]
public String Name { get; set; }
[DataMember]
public String Path { get; set; }
[DataMember]
public List<Slide> Slides { get; set; }
}
[DataContract]
public class Slide
{
[DataMember]
public String Title { get; set; }
[DataMember]
public ContentTypeEnum ContentType { get; set; }
[DataMember]
public String TextContent { get; set; }
[DataMember]
public String PictureContextPath { get; set; }
}
I am a newbie in WCF. I was wondering if we can retrive properties from base interface in the REST output.
Please consider following structure. Product includes IVenueView not Venue. Is it possible to only have Venue.Id in Product JSON response?
[DataContract]
public class Product {
[DataMember]
public Guid? Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public IVenueView Venue { get; set; }
}
public interface IVenueView {
[DataMember]
Guid? Id { get; set; }
}
[DataContract]
public class Venue : IVenueView
{
[DataMember]
public Guid? Id { get; set; }
[DataMember]
public string Name { get; set; }
}
Data contracts are all about data - interfaces define behaviors, so they don't really mix up well. The data contract that you have likely will not work (because the serializer doesn't "know" about the Venue type (it only knows about IVenueView), it won't be able to serialize / deserialize instances of Product.
No it is not possible because serialization and deserialization works with the implementation (actual data) not with interfaces. Moreover for pure serialization you will have to use something like:
[DataContract]
[KnownType(typeof(Venue))]
public class Product
{
[DataMember]
public Guid? Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public IVenueView Venue { get; set; }
}
I have a [DataContract] class Template declared as follows:
[DataContract]
public abstract class Template
{
[DataMember]
public virtual int? Id { get; set; }
[DataMember]
public virtual byte[] TemplateDoc { get; set; }
[DataMember]
public virtual string Title { get; set; }
[DataMember]
public virtual TemplateStatusInfo TemplateStatus { get; set; }
}
[DataContract]
public class TemplateStatusInfo
{
[DataMember]
public virtual List<string> ValidCodes { get; set; }
[DataMember]
public virtual List<string> InvalidCodes { get; set; }
[DataMember]
public virtual string TemplateError { get; set; }
}
As you can see Template has a [DataMember] child object 'TemplateStatusInfo'.
I just want to make sure if it is valid in WCF to declare child object with both attributes- [DataMember] as well as [DataContract].
I am not getting any compile time error.
Please guide.
It is correct what you have done. DataContract is applied to types, and DataMember to properties to specify if it should be included.