none
WCF ServiceReference Namespace Problem RRS feed

  • Question

  • Hi there. I have problem that I believe should be fairly common. Hopefully there is an easy solution.

    I have created a WCF project. In that project are two services:

    1) SecurityService (ISecurityService)
    2) LookupService (ILookupService)

    In my client project, I have two ServiceReferences:

    1) SecurityServiceReference
    2) LookupServiceReference

    In the Security Service, there is a "User" data contract. In the Lookup service there is "Role" data contract, which is a foreign key to User in the database. So the User data contract looks like:

    [DataContract]
    public class User
    {

    [DataMember]
    public Role Role {get;set;}
    }

    The problem is, in my client, when I try to assign the Role object to the user, I get a Invalid Cast exception doing this:

    SecurityServiceReference.User user = new SecurityServiceReference():
    user.Role = new LookupServiceReference.Role().GetRole(1);

    By now, you can probably see what is happening... "cannot cast LookupServiceReference.Role to SecurityServiceReference.Role". Is there a way around this other than manually changing the Role namespace in the User class in the Reference.cs file? 

    Thanks.


    Friday, December 13, 2013 10:21 PM

Answers

  • Would the "as" work if where an explicit would not?

    No, if it can't be cast explicitly, then it wouldn't work with "as" either (it would return a null). I didn't know if you had tried an explicit cast or not. Sorry.

    Perhaps you should have a separate Role class that is used in both your SecurityService and your LookupService. It would be in it's own namespace, thereby eliminating the namespace conflicts.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, December 15, 2013 6:02 PM
    Moderator

All replies

  • Off the top of my head, you could try this:

    user.Role = new LookupServiceReference.Role().GetRole(1) as SecurityServiceReference.Role;
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, December 15, 2013 5:49 PM
    Moderator
  • Thank for your reply.

    I have tried casting the one type to the other, and it would not work. My understanding is that the "as" keyword is the same as casting, but returns null if the cast is not compatible. Would the "as" work if where an explicit would not?

    Sunday, December 15, 2013 5:53 PM
  • Would the "as" work if where an explicit would not?

    No, if it can't be cast explicitly, then it wouldn't work with "as" either (it would return a null). I didn't know if you had tried an explicit cast or not. Sorry.

    Perhaps you should have a separate Role class that is used in both your SecurityService and your LookupService. It would be in it's own namespace, thereby eliminating the namespace conflicts.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, December 15, 2013 6:02 PM
    Moderator
  • I moved the DataContracts to an outside assembly that is shared with the services and the client(s).

    Works great. Thanks for your help.

    Friday, December 20, 2013 1:19 PM
  • You're welcome, KobeeDog! Glad I could help!   =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, December 20, 2013 2:40 PM
    Moderator