.NET Framework Developer Center > .NET Development Forums > Windows Communication Foundation > WCF method returning a strongly typed collection to a .NET 2.0 client
Ask a questionAsk a question
 

AnswerWCF method returning a strongly typed collection to a .NET 2.0 client

  • Friday, January 19, 2007 10:46 PMKiran Pillai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Tuesday, January 23, 2007 7:35 PMda hai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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:

    http://msdn2.microsoft.com/en-us/library/aa347850.aspx

All Replies

  • Saturday, January 20, 2007 12:11 AMRonald Ricardo Ramirez Moran Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Add this attribute to your service contract:

    [ServiceKnownType(typeof(XXX.Platform.RoleAssignmentCollection))]

    and this to the Implementation Class:

    [KnownType(typeof(XXX.Platform.RoleAssignmentCollection))]

    Regards,

  • Sunday, January 21, 2007 5:10 PMKiran Pillai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

     

  • Monday, January 22, 2007 10:44 AMKiran Pillai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Anybody has any idea as to the cause of the error?

     

    Thanks

    Kiran.

  • Monday, January 22, 2007 12:22 PMKiran Pillai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Tuesday, January 23, 2007 5:45 PMRonald Ricardo Ramirez Moran Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Try returning a List<MyDataContract> type instead of custom collections

    Regards,

  • Tuesday, January 23, 2007 7:35 PMda hai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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:

    http://msdn2.microsoft.com/en-us/library/aa347850.aspx

  • Wednesday, July 11, 2007 9:28 AMFey __ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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>

     

    **************************************************************************************************************************************************

  • Wednesday, May 21, 2008 2:58 PMSandeep Bhutani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

    sandeep.bhutani@igt.in

     

    thanks in advance

     

  • Saturday, October 04, 2008 1:35 AMHydrogen92 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    <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