Interface segregation in WCF for JSON - wcf

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; }
}

Related

The new ASP.NET Core 3.0 Json serializer is leaving out data

I'm porting a web application to ASP.NET Core 3, and after a bit of a battle, I'm almost at the finish line. Everything seems to work, but all of a sudden my JSON data returned from the api is missing some levels.
It seems the options.JsonSerializerOptions.MaxDepth is default at 64 levels, so it can be that. Some other places where an option can be playing tricks on me?
This is the code (and a quickview of the value):
And this is the JSON I get in the browser:
So the ParticipantGroups property/collection is completely missing in the generated output.
Any ideas where this happens?
EDIT:
I've added a repo on Github that showcases the issue. Standard ASP.NET Core 3.0 solution, created from the template, with a change to the result returned from the Weatherforecast controller:
https://github.com/steentottrup/systemtextjsonissue
For now I've gone back to using Newtonsoft.Json, with the Microsoft.AspNetCore.Mvc.NewtonsoftJson package. Then when I have some time, I'll try finding out what the solution is, without Newtonsoft.Json.
The problem seems to be an error in the new version 3.0. At least it seems like an error to me.
It seems System.Text.Json will convert the class mentioned in the hierarchy, not the actual class. So if you are using an abstract class in the hierarchy, you're in trouble. The second I removed the base class, and used the actual class I'm returning, the problem goes away it seems.
So this doesn't work:
public class SurveyReportResult {
public Guid Id { get; set; }
public String Name { get; set; }
public Int32 MemberCount { get; set; }
public IEnumerable<OrganisationalUnit> OrganisationalUnits { get; set; }
}
public abstract class OrganisationalUnit {
public Guid Id { get; set; }
public String Name { get; set; }
public Int32 MemberCount { get; set; }
}
public class OrganisationalUnitWithParticipantGroups : OrganisationalUnit {
public IEnumerable<ParticipantGroup> ParticipantGroups { get; set; }
}
public class ParticipantGroup {
public Guid Id { get; set; }
public String Name { get; set; }
public Int32 MemberCount { get; set; }
}
This will only return the properties of the OrganisationalUnit class, not the additional property of the OrganisationalUnitWithParticipantGroups.
This works:
public class SurveyReportResult {
public Guid Id { get; set; }
public String Name { get; set; }
public Int32 MemberCount { get; set; }
public IEnumerable<OrganisationalUnitWithParticipantGroups> OrganisationalUnits { get; set; }
}
public class OrganisationalUnitWithParticipantGroups /*: OrganisationalUnit*/ {
public Guid Id { get; set; }
public String Name { get; set; }
public Int32 MemberCount { get; set; }
public IEnumerable<ParticipantGroup> ParticipantGroups { get; set; }
}
public class ParticipantGroup {
public Guid Id { get; set; }
public String Name { get; set; }
public Int32 MemberCount { get; set; }
}

how to create a multiple datacontract class using wcf services

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; }
}

Consuming WCF Service with DataContract

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?

Serializing DTO's over WCF

I have a problem with NHibernate for a longtime which I solved by non-optimal ways/workarounds.
First of all, I'm using WCF REST to communicate with my client application. As you know, serializing persisted entities is not a best practise and always causes other problems. Thus, I always map my entities to DTO's with NHibernates Transformers. The problem is that I have entities which are more complex to use Transformers to convert them.
How can I map sub entities to sub dto's by using transformers or any other nhibernate feature?
Note: I don't want to use 3rd parties like Automapper.
These are the Entities and DTO's which I want to map. Variable names are exactly same with each other.
Entity Classes:
EntityType
public class crmEntityType : EntityModel<crmEntityType>
{
public crmEntityType()
{
Association = new List<crmEntityType>();
Fields = new List<crmCustomField>();
}
public virtual int ID { get; set; }
public virtual string Title { get; set; }
public virtual ICollection<crmEntityType> Associations { get; set; }
public virtual ICollection<crmCustomField> Fields { get; set; }
}
CustomFields
public class crmCustomField : EntityModel<crmCustomField>
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual crmEntityType EntityType { get; set; }
}
DTO's
EntityTypeDTO
[DataContract]
public class EntityTypeDTO
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public IList<CustomFieldDTO> Fields { get; set; }
[DataMember]
public int[] Associations { get; set; }
}
CustomFieldDTO
[DataContract]
public class CustomFieldDTO
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int EntityType { get; set; }
[DataMember]
public int FieldType { get; set; }
}
I found my solution by spending my day and night to work it out. Finally, I've got the best solution I could find. I hope it helps someone in my condition some day.
This linq query works with just one database round-trip. I think it maps the classes in memory.
return (from entityType in Provider.GetSession().Query<crmEntityType>()
.Fetch(x => x.Association)
.Fetch(x => x.Fields)
.AsEnumerable()
select new EntityTypeDTO()
{
ID = entityType.ID,
Title = entityType.Title,
Association = entityType.Association.Distinct()
.Select(asc => asc.ID).ToArray<int>(),
Fields = entityType.Fields.Distinct()
.Select(fi => new CustomFieldDTO
{ ID = fi.ID,
Name = fi.Name,
Value = fi.Value,
EntityType = fi.EntityType.ID,
Type = fi.Type
}).ToList()
}).ToList();

WCF - DataMember as DataContract

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.