I have a WCF service, have different type to objects(Person, Address classes) passed to service calls and these Person, Address objects returned from WCF call..
In order to make service reusable it makes sense to have service figure out model type OR object type passed in at run time and while making service call from Silverlight ability to package these objects(person class values) in to generic object/byte array
etc...
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace MyNamespace
{
[DataContract]
[KnownType(Class2)]
[KnownType(Class3)]
public class Class1
{
[DataMember]
public int Id { get; set; }
[DataMember]
public Object MyObjectProperty { get; set; }
}
}
Inthis
exampleyoucan
pass in "MyObjectProperty"
objects
of 'Class2' or'Class3'.
I want to have service recive/deliver any (class1, class2, class3) and perform same operation to fetch data from DB irrespective of Class it receives..so Service has Read and write, Update methods
[ServiceContract(Namespace = "")]
public interface IService
//GenericItem can be class1, class2, class3
{[OperationContract]
GenericItem Read(GenericItem item);
[OperationContract]
GenericItem Write(GenericItem item);
[OperationContract]
GenericItem Update(GenericItem item);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service:IService
{
public IAsyncResult BeginRead(GenericItem obj, AsyncCallback callback, object state)
{
}
}
so, GenericItem can be of type Class1, class2 or class3...
SerivceContract and service needs not to have knowledge earlier of which type..shuould be able to figure out at runtime that this is class1 or class2.
[KnownType(Class2)] [KnownType(Class3)]
[DataContract]
public class GenericItem
{
[DataMember]
public int Id { get; set; }
[DataMember]
public object ObjectOfOtherClass { get; set; }
}
and service:
[ServiceKnownType(typeof(Class1))]
[ServiceKnownType(typeof(Class2))]
[ServiceKnownType(typeof(byte[]))]
[ServiceContract(Namespace = "")]
public interface IService
//GenericItem can be class1, class2, class3
{[OperationContract]
GenericItem Read(GenericItem item);
[OperationContract]
GenericItem Write(GenericItem item);
[OperationContract]
GenericItem Update(GenericItem item);
}
archmt
Member
17 Points
96 Posts
pass generic class(Model) to wcf service call
Feb 01, 2011 06:58 PM | LINK
I have a WCF service, have different type to objects(Person, Address classes) passed to service calls and these Person, Address objects returned from WCF call..
In order to make service reusable it makes sense to have service figure out model type OR object type passed in at run time and while making service call from Silverlight ability to package these objects(person class values) in to generic object/byte array etc...
Is there a way to achieve this..
MF_MiEK
Participant
1380 Points
279 Posts
Re: pass generic class(Model) to wcf service call
Feb 01, 2011 07:16 PM | LINK
Hi,
You have to use KnowType attribute:
using System; using System.ComponentModel; using System.Runtime.Serialization; namespace MyNamespace { [DataContract] [KnownType(Class2)] [KnownType(Class3)] public class Class1 { [DataMember] public int Id { get; set; } [DataMember] public Object MyObjectProperty { get; set; } } }In this example you can pass in "MyObjectProperty" objects of 'Class2' or 'Class3'.
Find more examples in MSDN for [KnownTypes ()].
archmt
Member
17 Points
96 Posts
Re: pass generic class(Model) to wcf service call
Feb 02, 2011 12:32 PM | LINK
Thanks.
1)What about when I want to pass class 1 as an object while making service calls.
How to pass this object in a serivce call froom SL...byte array?
MF_MiEK
Participant
1380 Points
279 Posts
Re: pass generic class(Model) to wcf service call
Feb 02, 2011 01:34 PM | LINK
Hi,
I haven't ever tried it with byte [], but I think it's the same :
And for service:
http://thzero.blogspot.com/2009/03/wcf-silverlight-and-known-types_21.html
archmt
Member
17 Points
96 Posts
Re: pass generic class(Model) to wcf service call
Feb 02, 2011 04:48 PM | LINK
So I have:
1)
[DataContract]
public class Class1
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
}
2)
[DataContract]
public class Class2
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string City{ get; set; }
}
3) Some other object Class3...
I want to have service recive/deliver any (class1, class2, class3) and perform same operation to fetch data from DB irrespective of Class it receives..so Service has Read and write, Update methods
[ServiceContract(Namespace = "")]
public interface IService
//GenericItem can be class1, class2, class3
{[OperationContract]
GenericItem Read(GenericItem item);
[OperationContract]
GenericItem Write(GenericItem item);
[OperationContract]
GenericItem Update(GenericItem item);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service:IService
{
public IAsyncResult BeginRead(GenericItem obj, AsyncCallback callback, object state)
{
}
}
so, GenericItem can be of type Class1, class2 or class3...
SerivceContract and service needs not to have knowledge earlier of which type..shuould be able to figure out at runtime that this is class1 or class2.
archmt
Member
17 Points
96 Posts
Re: pass generic class(Model) to wcf service call
Feb 02, 2011 04:53 PM | LINK
ALSO, how would service know the type of object it received here i.e. for MyObjectProperty
MF_MiEK
Participant
1380 Points
279 Posts
Re: pass generic class(Model) to wcf service call
Feb 02, 2011 05:53 PM | LINK
Hi,
and service:
[ServiceKnownType(typeof(Class1))] [ServiceKnownType(typeof(Class2))] [ServiceKnownType(typeof(byte[]))] [ServiceContract(Namespace = "")] public interface IService //GenericItem can be class1, class2, class3 {[OperationContract] GenericItem Read(GenericItem item); [OperationContract] GenericItem Write(GenericItem item); [OperationContract] GenericItem Update(GenericItem item); }archmt
Member
17 Points
96 Posts
Re: pass generic class(Model) to wcf service call
Feb 02, 2011 06:57 PM | LINK
Hi
how to access say class2 and it's properties(modify it as well) in the service code..
For example =Class2.LastName
And Say SL client app uses class2...how to pass that info from client to service so that service code knows that it is class2 to it has recieved etc.
Thanks.
Daoping Liu...
All-Star
15960 Points
2045 Posts
Microsoft
Re: pass generic class(Model) to wcf service call
Feb 03, 2011 08:08 AM | LINK
Hi,
You can ask for more help from Asp.net forum.
Hope that will help.
Regards
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework