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.
Related
So, I've been looking at all kinds of examples of a WCF class that is to be serialized. I'd like to define all my DataContracts and DataMembers in interfaces and then allow implementation that suits the environment. Is this possible, and if so, are there drawbacks to this approach?
[DataContract]
public class Contact
{
[DataMember]
public int Roll { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public int Age { get; set; }
}
Can instead do this:
[DataContract]
public interface IContact
{
[DataMember]
int Roll { get; set; }
[DataMember]
string Name { get; set; }
[DataMember]
string Address { get; set; }
[DataMember]
int Age { get; set; }
}
public class Contact :IContact
{
public int Roll { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
I am using complex type as datamember of a datacontract. But the service is throwing an exception if i set [DataMember] to the complextype. What might be the reason. KIndly suggest. Below is the code snippet
[DataContract]
public class GetDetailsFromCardNumberResponseBody
{
public GetDetailsFromCardNumberResponseBody()
{
}
public GetDetailsFromCardNumberResponseBody
(PatronAccountCardValidation GetDetailsFromCardNumberResult)
{
this.GetDetailsFromCardNumberResult = GetDetailsFromCardNumberResult;
}
[DataMember]
public PatronAccountCardValidation GetDetailsFromCardNumberResult { get; set; }
}
[DataContract(Name="PatronAccountCardValidation")]
public class PatronAccountCardValidation
{
[DataMember]
public Patron Patron { get; set; }
[DataMember]
public Card Card { get; set; }
[DataMember]
public string BonusDollarLocal { get; set; }
[DataMember]
public string BonusDollarRemote { get; set; }
[DataMember]
public bool Valid { get; set; }
[DataMember]
public string MessageText { get; set; }
}
Check the parameter PatronAccountCardValidation.
Check property Card. It is causing an issue. Please help.
Error received:
An unhandled exception of type
'System.ServiceModel.CommunicationException' occurred in mscorlib.dll
Additional information: An error occurred while receiving the HTTP
response to localhost:9090/DoorAccessAndVendingMachineService. This
could be due to the service endpoint binding not using the HTTP
protocol. This could also be due to an HTTP request context being
aborted by the server (possibly due to the service shutting down). See
server logs for more details
Have you consider to apply Data Contract Know types? It allows you to specify, in advance, the types that should be included for consideration during deserialization. And by default during deserialization, an uninitialized object is first created, without calling any constructors on the type. Then all data members are deserialized. See more details here.
[DataContract]
[KnownType(typeof(PatronAccountCardValidation))]
public class GetDetailsFromCardNumberResponseBody
{
public GetDetailsFromCardNumberResponseBody()
{
}
public GetDetailsFromCardNumberResponseBody
(PatronAccountCardValidation GetDetailsFromCardNumberResult)
{
this.GetDetailsFromCardNumberResult = GetDetailsFromCardNumberResult;
}
[DataMember]
public PatronAccountCardValidation GetDetailsFromCardNumberResult { get; set; }
}
[KnownType(typeof(Card))]
[KnownType(typeof(Patron))]
[DataContract(Name="PatronAccountCardValidation")]
public class PatronAccountCardValidation
{
[DataMember]
public Patron Patron { get; set; }
[DataMember]
public Card Card { get; set; }
[DataMember]
public string BonusDollarLocal { get; set; }
[DataMember]
public string BonusDollarRemote { get; set; }
[DataMember]
public bool Valid { get; set; }
[DataMember]
public string MessageText { get; set; }
}
I'm using json.net to perform handle my json deserialization in a IIS hosted restful service.
First, here are the objects I'm trying to work with.
[DataContract]
public class CreateSamplesRequest
{
[DataMember] public Guid SessionId { get; set; }
[DataMember] public SampleTemplateDTO Template { get; set; }
}
[DataContract]
public class SampleTemplateDTO
{
[DataMember] public int NumberOfSamples { get; set; }
[DataMember] public int CompanyId { get; set; }
[DataMember] public int SampleTypeId { get; set; }
[DataMember] public HmisDTO Hmis { get; set; }
[DataMember] public List<AttributeValueDTO> AttributeValues { get; set; }
}
[DataContract]
public class AttributeValueDTO
{
[DataMember] public int AttributeId { get; set; }
[DataMember] public string AttributeName { get; set; }
[DataMember] public string Value { get; set; }
}
[DataContract]
public class HmisDTO
{
[DataMember] public string Health { get; set; }
[DataMember] public string Flammability { get; set; }
[DataMember] public string Reactivity { get; set; }
[DataMember] public string Equipment { get; set; }
}
The help page asks for json in this format for the CreateSamplesRequest
{
"SessionId":"1627aea5-8e0a-4371-9022-9b504344e724",
"Template":{
"NumberOfSamples":2147483647,
"CompanyId":2147483647,
"SampleTypeId":2147483647,
"Hmis":{
"Health":"String content",
"Flammability":"String content",
"Reactivity":"String content",
"Equipment":"String content"
},
"AttributeValues":[{
"AttributeId":2147483647,
"AttributeName":"String content",
"Value":"String content"
}]
}
}
And this is what I'm actually sending:
{
"SessionId":"17aaec11-be28-4536-b5df-d98fbda91e91",
"Template":{
"NumberOfSamples":1,
"CompanyId":1,
"SampleTypeId":9,
"Hmis":{
"Health":"2",
"Flammability":"0",
"Reactivity":"0",
"Equipment":"E",
},
"AttributeValues":[
{"AttributeId":1,"AttributeName":"Item No.","Value":"MN0002NG"},
{"AttributeId":2,"AttributeName":"Lot No.","Value":"C4526"}
]
}
}
The problem I'm having is that the AttributeValues property of the SampleTemplateDTO object always ends up being ignored. With the above code, it will be null. If I instanciate it to a empty List<AttributeValueDTO> it will be an empty list. I've been banging my head against this for a few hours.
I've tried creating a service that just takes a List<AttributeValueDTO> and it works fine. I've tried creating a wrapper class for the AttributeValues and it still ends up as null. I'm completely stumped. Any ideas?
MOTHER OF GOD, I JUST WASTED 5 FREAKING HOURS ON A FREAKING COMMA. The trailing comma in the HMIS section was apparently telling json.net to stop parsing at that point.
When I submit this json:
{
"SessionId":"17aaec11-be28-4536-b5df-d98fbda91e91",
"Template":{
"NumberOfSamples":1,
"CompanyId":1,
"SampleTypeId":9,
"Hmis":{
"Health":"2",
"Flammability":"0",
"Reactivity":"0",
"Equipment":"E"
},
"AttributeValues":[
{"AttributeId":1,"AttributeName":"Item No.","Value":"MN0002NG"},
{"AttributeId":2,"AttributeName":"Lot No.","Value":"C4526"}
]
}
}
Everything works great.
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 composite object set up Project->Appraisal, My appraisal object has a ApprovedMentor object which is not required but when i go to save project Nhib throws and error to say that ApprovedUser has not been set. but its not set because its not a required field. How do i set up this using fluent auto mapping, is it possible?
public class MentoringProject : BaseEntity
{
public MentoringProject()
{
Appraisal = new Appraisal();
}
[NotNullNotEmpty]
[Length(Min=25, Max=1000)]
public virtual string Description { get; set; }
[Length(Min=25, Max=1000)]
public virtual string SupportRequired { get; set; }
[NotNullNotEmpty]
public virtual System.DateTime? DateSubmitted { get; set; }
[NotNullNotEmpty]
public virtual System.DateTime? ClosingDate { get; set; }
[NotNullNotEmpty]
[Size(Min=1)]
public virtual short Duration { get; set; }
[NotNullNotEmpty]
public virtual string Skills { get; set; }
public virtual Appraisal Appraisal { get; set; }
}
public class Appraisal : BaseEntity
{
public Appraisal()
{
ShortlistedMentors = new List<User>();
ApprovedMentor = new User();
College = new RefData();
}
#region Primitive Properties
public virtual bool Decision { get; set; }
public virtual System.DateTime? ApprovedDate { get; set; }
public virtual System.DateTime? AcceptedDate { get; set; }
public virtual System.DateTime? CompletionTargetDate { get; set; }
public virtual string RejectionReason { get; set; }
#endregion
#region Navigation Properties
public virtual IList<User> ShortlistedMentors { get; set; }
public virtual User ApprovedMentor { get; set; }
public virtual RefData College { get; set; }
#endregion
}
It looks to me that you just want to ignore the ShortlistedMentors property which you need to do in your mapping class like this:
map.IgnoreProperty(p => p.ShortlistedMentors);
This answer was posted in this question.
I think i have solved this, when binding the UI to the controller in MVC, MVC creates an empty User object and because that object has required fields set on it using nhib validator and nhib was trying to create a new user object, I got round this by checking if there is a user realtionship to add, if not I set the Appraisal.ApprovedMentor==null