WCF method returning a strongly typed collection to a .NET 2.0 client
Ok I have a WCF method defined which returns a strongly typed collection.
[ServiceKnownType(typeof(XXX.Platform.RoleAssignment))]
XXX.Platform.RoleAssignmentCollection GetRoleAssigments(int UserID);
RoleAssignmentCollection class can add objects of type XXX.Platform.RoleAssignment.
Whenever the above method is invoked from a .NET 2.0 client using the proxy generated by WSDL, it returns an Object array. So on trying to iterate through the object array in rebuilding the collection as shown below
proxy cs=new proxy();
Object[] t1 = cs.GetRoleAssigments(3);
XXX.Platform.RoleAssignmentCollection r1 = new XXX.Platform.RoleAssignmentCollection();
foreach (Object o in t1)
{
r1.Add((XXX.Platform.RoleAssignment)o);
}
return r1;
I get the following exception
System.InvalidCastException: Unable to cast object of type 'RoleAssignment' to type 'XXX.Platform.RoleAssignment'. at proxy.GetRoleAssigments(Int32 UserID) ....................
It would be great if anybody has any idea as to what changes I would need to correct this issue. I thought by placing the
[ServiceKnownType(typeof(XXX.Platform.RoleAssignment))] attribute
when defining the method in the contract should fix the issue, however thats not the case. The object within the object array returned is just "RoleAssignment" and not "XXX.Platform.RoleAssignment" and hence the error.
Thanks
Kiran.
Answers
wsdl.exe /?
/namespace:<namespace>
The namespace for the generated proxy or template. The default namespace
is the global namespace. Short form is '/n:'.If you want to control the namespace, you can use /namespace above.
If you want to control the collection, use [CollectionDataContract]. Here is more on the doc:
All Replies
Add this attribute to your service contract:
[ServiceKnownType(typeof(XXX.Platform.RoleAssignmentCollection))]
and this to the Implementation Class:
[KnownType(typeof(XXX.Platform.RoleAssignmentCollection))]
Regards,
Thanks Ronald for the answer, I did exactly as you mentioned, however I am still getting the same error. Placed both
[ServiceKnownType(typeof(XXX.Platform.RoleAssignmentCollection))]
[ServiceKnownType(typeof(XXX.Platform.RoleAssignment))]
to the Service contract and the following attribute to the Implementation class
[KnownType(typeof(XXX.Platform.RoleAssignmentCollection))]
[KnownType(typeof(XXX.Platform.RoleAssignment))]
The WCF service is hosted within a windows service. I have used wsdl to generate the proxy. So from a ASP.Net client application I invoke the method through the proxy which returns "XXX.Platform.RoleAssignmentCollection", the result as mentioned before is an Object[] with each individual object of type "RoleAssignment" and not "XXX.Platform.RoleAssignment" . As a result I am getting the same error as I mentioned in the first post. Am I missing any other attribute?.
Because of the error, on the client side after declaring a XXX.Platform.RoleAssignmentCollection variable, I cannot add the objects within the Object[] as they are of type "RoleAssignment" and not "XXX.Platform.RoleAssignment".
Thanks
Kiran.
Anybody has any idea as to the cause of the error?
Thanks
Kiran.
This issue has been bugging me for some time now. Has anybody encountered this issue before.If I can get this solved, it will be great.
Thanks
Kiran.
Try returning a List<MyDataContract> type instead of custom collections
Regards,
wsdl.exe /?
/namespace:<namespace>
The namespace for the generated proxy or template. The default namespace
is the global namespace. Short form is '/n:'.If you want to control the namespace, you can use /namespace above.
If you want to control the collection, use [CollectionDataContract]. Here is more on the doc:
Hi,
Even I am getting the same problem for returning the collection from the WCF service to the .net 2.0 asmx client, that When I am returning a 'ThingCollection' from WCF Service it is giving me the array of 'Thing [ ] '..and still not able to figure out.. can you please correct me where I am wrong in my below code .. I tried every possibility to use ServiceKnownType and KnownType.. Also I am not able to figure out '
/namespace:<namespace>
The namespace for the generated proxy or template. The default namespace
is the global namespace. Short form is '/n:'.If you want to control the namespace, you can use /namespace above.
' ????....
I am referring the below code ..pls correct it ..
using
System;using
System.ServiceModel;using
System.Runtime.Serialization;using
System.Collections;using
System.Collections.Generic;using
System.ComponentModel;using
System.Collections.ObjectModel;[
ServiceContract(Namespace = "http://WCF.Sample/AsmxSample")][
ServiceKnownType(typeof(ThingCollection))][
ServiceKnownType(typeof(Thing))]public
interface IMyService{
[
OperationContract] ThingCollection GetThingsCollection();}
[
ServiceKnownType(typeof(ThingCollection))][
ServiceKnownType(typeof(Thing))]public
class MyService : IMyService{
public ThingCollection GetThingsCollection(){
ThingCollection thingsCollection = new ThingCollection(); Thing thing = new Thing();thing.Name =
"ASMX";thing.Description =
"WEB SERVICE";thingsCollection.Add(thing);
return thingsCollection;}
}
[
CollectionDataContract(Name = "MyCollection", Namespace = "http://WCF.Sample/AsmxSample",ItemName="Thing")][
KnownType(typeof(BindingList<Thing>))]public
class ThingCollection : BindingList<Thing>{
}
[
DataContract( Namespace= "http://WCF.Sample/AsmxSample")]//[ServiceKnownType(typeof(Thing))]
public
class Thing{
[
DataMember] public string Name;[
DataMember] public string Description;}
*************************************************************************************************************************************************
Web.config
*************************************************************************************************************************************************
<
configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"><
system.serviceModel><
services><!--
Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages --><
service name="MyService" behaviorConfiguration="returnFaults"><
endpoint contract="IMyService" binding="basicHttpBinding"/></
service></
services><
behaviors><
serviceBehaviors><
behavior name="returnFaults" ><
serviceDebug includeExceptionDetailInFaults="true" /><
serviceMetadata httpGetEnabled="true" /></
behavior></
serviceBehaviors></
behaviors></
system.serviceModel><
system.web><
compilation debug="true"/></
system.web></
configuration>**************************************************************************************************************************************************
Hi,
I am facing the same problem of Array generation for a collection in the WCF 2.0 client.
If you have found the solution please post or mail answer me at
thanks in advance
<bump/>
Same here! -- Experiencing the exact same issue. This topic is SIGNIFICANTLY LACKING in support and documentation.... The consumption of Collection-containing WCF DataContracts by non-WCF clients (i.e. ASMX, JAVA).
Help!
-Darius


