RIA Service exposed with SOAP Endpoint. Cant get entity's members - wcf

There is a WCF RIA service exposed via SOAP endpoint. I added this service as a service reference to an another project. When I invoke service's method, i get pleased entity but without nested entites. In other words, there are no [DataMember] attributes on nested members of the entity.
I tried to add [DataMember] attribute on entity's members, but it doesnt work.
Entity:
[MetadataType(typeof(PlnCheckMeta))]
public partial class PlnCheck : IEntityChangeLogable
{
}
public partial class PlnCheckMeta
{
[Composition]
[Display(AutoGenerateField = false)]
public IList<PlnCheck> PlnCheckList { get; set; } // can't get this member
// in service reference
// generated entity
}
Is there any idea how to expose nested members?

Add an [Include] attribute to PlnCheckList

Related

How to serialize complex model to xml format wcf

I am developing a wcf application which is taking c# request model as an input and returning complex response c# model after populating data in it from database. I am using SOAP UI client tool to test my service.
Earlier i had decorated my contract with XmlSerializerFormat attribute and after submitting request i was able to get the response in Result tab of SOAP UI tool. Below is the code snippet of it :
[ServiceContract]
[XmlSerializerFormat]
public interface IService1
{
[OperationContract]
Employee GetData(Employee value);
}
[DataContract]
public class Employee
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public string Address { get; set; }
}
But now due to some reasons i have removed XmlSerializerFormat attribute from contract and i want default serializer to take care of it. But when i am submitting the request from client , my response model is populating with results but Response tab of SOAP UI shows empty response.
During Response model population when i am removing certain properties from response model, i am able to see result in response tab of SOAP UI tool.
Is Default serializer in facing some issues while serializing some of property of my model. Any other attribute apart from XmlSerializerFormat ?
Please let me know where i am lacking or is there any alternative of XmlSerializerFormat attribute.
Thnx in Advance
Try adding DataContract attribute to the Employee on the class level and DataMember attribute to its properties that you want to be serialized. If it has complex property types of its own they also may need to be decorated similarly. Default serializer in WCF is DataContract serializer FYI.

WCF generic property is not mapped in derived class

I have:
[DataContract]
public class A
{
[DataMember]
public int X { get; set; }
}
[DataContract]
[KnownType(typeof(Derived<A>))]
public class Base {}
[DataContract]
public class Derived<T>: Base {
[DataMember]
public T Data {get; set; }
}
Service method returns IEnumerable of Base class containing object of Derived class.
Auto-generated WCF client retrieves data and correctly detects type and creates instances of the Derived class, but Derived.Data.X is not mapped, i.e. has 0. The soap message received is correct.
Any ideas why the generic property of the derived class is not correctly mapped from the soap message in the client?
I strongly recommend you to avoid generics in service contracts. SOA world is not friendly with generic type in the service contract. Please look problems reported by others:
Error when updating service reference.The URI prefix is not recognized
WCF. Service generic methods
WCF generic property is not mapped in derived class
http://social.msdn.microsoft.com/Forums/vstudio/en-US/2a9a2fc6-9e01-4112-8948-4192c516c6e7/how-to-use-generics-in-wcf-service

WCF DataContract Attribute

I have a question about the [DataContract] attribute.
I have written my code like below: here I am not using [DataContract] attribute for my test class.
class test
{
[Datamember]
public string Strproperty
{
get;
set;
}
[Datamemer]
public string Strproperty2
{
get;
set;
}
}
class checktotal:Iservice
{
public string testmethod(test obj)
{
return obj.Strproperty+Strproperty2;
}
}
For that I am sending data from client I am getting the values correctly.
Here is it necessary to use [DataContract] attribute for that test class?
If I removed [Datamember] for test class property is getting error while sending from client. But I am not getting any errors even if I am not using the [DataContract] attribute.
Please give me a brief explanation with example so that I can understand when to give that attribute and when do not give that attribute.
Thanks,
Satya Pratap.
The DataContractSerializer can deal with classes that do not have the DataContract attribute if they provide a default constructor. See the MSDN documentation for more details.
As of .NET 3.5 Service Pack 1, you can omit (not use) the [DataContract] and [DataMember] attributes. If you do that, then the DataContractSerializer in WCF will behave just like the XML serializer - it will serialize all public properties only.
I prefer to use [DataContract] and [DataMember] explicitly anyway - it gives me the opportunity to specify options (like the data contract's XML namespace, the order of the [DataMember]) and it lets me e.g. also exclude certain properties from serialization.
As soon as you start using [DataMember] on one property, then only those properties decorated with a [DataMember] will be looked at for the WCF serialization.

Problem with sending object via wcf service with private fields

I have a class Car
public class Car
{
private Member _owner;
public string OwnerName
{
get { return _owner.Name; }
}
public Car(Member owner)
{
_owner = owner;
}
}
I'm using it both at Silverlight application and wcf service
So, at application I call WCF service to give me instance of car class, but when I get it at application, I see that _owner is empy.
I know that it is empty because of private, but how can I deal with it?
I'm using this class in my app as model (MVVM) if it could helps :/
For a start none of your properties are marked as DataMembers. The class isn't marked as a DataContract. If this is getting returned from a WCF service I would expect to see:
[Serializable]
[DataContract]
public class Car
{
private Member _owner;
[DataMember]
public string OwnerName
{
//getter
//setter
}
etc..
}
Does Member have to be private? Could it be converted into a property?
Keep in mind that a [DataMember] property needs both a set and a get (so that WCF can read into and from the object).
http://msdn.microsoft.com/en-us/library/ms733127.aspx
From a WCF serialization point of view, your Car class actually looks something like this to the WCF service:
public class Car
{
public string OwnerName { get; set; }
//other public properties here....
}
The WCF serializer uses the .NET class definition as a template for serializing its contents as a simple data transfer object. When the WCF service sends back a Car instance, only the public properties will contain values. The serializer ignores all methods in the class. Also, later versions of WCF don't require the DataContract/DataMember attribute markup.
The _owner variable is never initialized because it is not part of the public properties of the Car class. You'll need to modify the structure of the Car class (maybe add a public Owner property of type Member) to get all the data sent from the WCF service to your client.
When you are using the default Data Contract Serializer with WCF services it serializes and deserializes only the public properties of the class. Also another thing to note is that while deserializing the object graph the constructor is not called. You can have a public property with getter and setter.
Here is a very nice article by Jeremy Likeness explaining the problem similar to yours. From Architecture as well as best practices point of view you can use a POCO class generally called as DTO (Data Transfer Object) when transferring objects between the service layer and the clients.

WCF Ria Services and Cyclic references

I'm using a legacy database that has some cyclic references. When I consume my Ria service from a SL4 client. (generated entities through my ORM mapper) I get the following error:
There was an error while trying to serialize parameter http://tempuri.org/:GetPublicationPagesResult. The InnerException message was 'Object graph for type 'xxx.Entities.TblPublicationPage' contains cycles and cannot be serialized if reference tracking is disabled.
[Query]
public IQueryable<TblPublicationPage> GetPublicationPages(int publicationId)
{
return this.PublicationLogic.Value.GetPublicationPages(publicationId);
}
I know how to enable it for plain WCF through CyclicReferencesAware attribute or IsRefence=true. But I can't figure out how to do this with WCF Ria Services.
I now understand WCF Ria Services better, I just tried to fix it like I would do it in plain WCF and added a metadataclass to my generated entities:
[DataContract(IsReference = true)]
[DataServiceKey("PublicationPageID")]
[DebuggerDisplay("PublicationPageID: {PublicationPageID}")]
[MetadataType(typeof(TblPublicationPageMetadata))]
public partial class TblPublicationPage
{
internal sealed class TblPublicationPageMetadata
{
[DataMember]
public int PublicationPageID { get; set; }
}
}
Only disadvantages at this point is I have to decorate every property in the metadata class with a [DataMember] attribute...
Use [CyclicReferencesAware(true)]