none
WCF Cast error "Unable to cast object of type X to type X

    Question

  •  

    Hi,

     

    I getting an error when accessing a Typed dataset from a WCF service. There error message is;

     

    Unable to cast object of type 'ModelSysLLC.Africare.Reporting.ReportingService.BudgetRpt' to type 'ModelSysLLC.Africare.Reporting.BudgetRpt'.

     

    The Type Dataset "BudgetRpt" is the same in the service as well in the client component.

     

    What could be causing this.

     

    The Service is written in C# and the client is VB.NET. If this helps at all.

     

    Please help.

     

    Thanks

    Sunday, June 22, 2008 1:16 AM

Answers

  • Hi James,

     

    >The strongly typed dataset is in both Libraries.  Could this be a problem?

     

    Yes, this is the problem.

     

    There is one copy of the typed dataset in the client project.  But when generating the proxy on the client side, svcutil generates a second copy of this typed dataSet under the service namespace.  Hence, you have two copies of this typed dataset.  As far as .Net runtime is concern, you have two different types because of the difference in namespaces.  As Dan pointed out, this causes the converstion to failed.

     

    Put the typed dataset in a shared library and reference it from both your client and service code.

     

    HTH,

    An

    Monday, July 21, 2008 11:42 PM

All replies

  • This usually happens when the generated proxy is using a different namespace than what the service proxy return value is actually returning.   Do you have 2 proxy references for any reason?  Or a shared library?  You might try removing all service references, re-added it, and make sure you use teh same namespace.

     

    Sunday, June 22, 2008 2:13 AM
    Moderator
  • Sorry about the late reply.

     

    I removed the reference and re-added it. I don't have 2 proxy references either. I did change the namespaces in both the client Dll and the WCF service to be as simular as possible. The library is not shareed, the services Dll is completely seperate from the client Dll. The name space in the message looks like this. The strongly typed dataset is in both Libraries. Could this be a problem?

     

     

    Error Message:

    Unable to cast object of type 'ModelSysLLC.Africare.Reporting.ReportServices.BudgetRpt' to type 'ModelSysLLC.Africare.Reporting.BudgetRpt'.

     

     

    Thanks,

     

    James

    Thursday, July 03, 2008 11:13 AM
  • Obviously the client sees 2 types.  Are you using a shared library at all or is this just a generated proxy?  In the client project do you see both of these types?

     

    Thursday, July 03, 2008 11:20 AM
    Moderator
  • I only have the generated proxy. the reference is to the WCF Web Service with Mex turned on.

     

    Friday, July 04, 2008 2:58 AM
  • Hi!
    I have the same problem, but a little bit more general. The problem:
    I'd like to use a construction with proxy pattern, and depending on the circumstances i use an instantiated Service object, or a ServiceClient object. I want to hide this under an IService interface, and call its methods. The problem is, that I get an exception when trying to cast the ServiceClient to IService.

    Monday, July 21, 2008 3:06 PM
  • Hi James,

     

    >The strongly typed dataset is in both Libraries.  Could this be a problem?

     

    Yes, this is the problem.

     

    There is one copy of the typed dataset in the client project.  But when generating the proxy on the client side, svcutil generates a second copy of this typed dataSet under the service namespace.  Hence, you have two copies of this typed dataset.  As far as .Net runtime is concern, you have two different types because of the difference in namespaces.  As Dan pointed out, this causes the converstion to failed.

     

    Put the typed dataset in a shared library and reference it from both your client and service code.

     

    HTH,

    An

    Monday, July 21, 2008 11:42 PM
  • Hi Daniel,

     

    This does not sounds like the same problem as what James is facing.  Perhaps you should create a separate post?

     

    Also, the issue is not clear to me.  From what you describe above, it seems that your proxy class is named ServiceClient and your service contract is IService. 

     

    If you look at the cs file that is generated, you should see

     

    ServiceClient : System.ServiceModel.ClientBase<IService>, IService

     

    So, the casting like the one below should work unless ServiceClient is not generated and is a custom class created by you.

     

    IService service = new ServiceClient();

     

    Thanks,

    An

     

     

    Monday, July 21, 2008 11:57 PM
  • The definition of the generated Client class is:

    ServiceClient : System.ServiceModel.ClientBase<Project.ServiceReference.IService>, Project.ServiceReference.IService

    The strange thing is, that this IService interface is not the same what i used to define the contract. That interface is in an other namespace (lets say Project.Common.Interfaces.IService).

    How could i make the WCF Service Reference's code generator to reuse the existing interfaces when generating the client code?
    Tuesday, July 22, 2008 7:55 AM
  • An,

     

    Thanks a lot for the solution. I'll give that a try and also I used the merge method on the dataset to move the data across as well. Just to get past my problem. Your solution is a lot cleaner though, so I will be using that.

     

    Thanks again,

     

    James

     

    Sunday, September 07, 2008 2:57 PM
  • Daniel,

    That's my problem exactly!  Did you find a work around?

    Kevin
    Tuesday, March 17, 2009 9:10 PM