tags:

views:

46

answers:

3

I am using a WCF service in my project. This service returns a class called "Store". I created a new local class which inherits from "Store". My class is called "ExtendedStore". My ExtendedStore looks like this:

class ExtendedStore : StoreManagerService.Store
{
    public int Id;
    ....
}

Now I am using the WCF service to cast to my class using the following code:

StoreManagerService.StoreClient client = new StoreManagerService.StoreClient();
ExtendedStore store = (ExtendedStore) client.GetStore(); // bombs here

I am not able to cast the returned Store class from the service to my ExtendedStore class. I get the below error message:

Unable to cast object of type 'ConsoleApplication1.StoreManagerService.Store' to type 'ConsoleApplication1.ExtendedStore'.

Shouldn't I be able to cast it? If not, is there a workaround?

+4  A: 

Check Data Contract KnownTypes it gives you ability to work with inheritance. Mainly gives you ability to assign object of derived class to object of parent class and more... Check KnownType and ServiceKnownType it will help you.

Incognito
Also see http://stackoverflow.com/questions/560218/wcf-configuring-known-types on how to do this purely in App.config configuration. This would be necessary if you can't change `StoreManagerService.Store` or supply `ServiceKnownType` in the service contract.
Thorarin
+2  A: 

It looks like you are trying to do the equivalent of:

 BaseType client = new BaseType();
 DerivedType store = (DerivedType) client.GetStore();

You're converting to a more derived type, instead of to a lesser derived type. That would never work.

Tejs
Oh... didnt think of that. Thanks :/
vikasde
@Thorarin: his example will never work. In effect, it's an example of the client returning a `Store`, and not an `ExtendedStore`. In the OP's case, the service will never return an `ExtendedStore`.
John Saunders
@John Saunders: It seems likely from the story around the code, but there's no code that actually shows what it is returning :)
Thorarin
@Thorarin: re-read the question. He says the service **is** returning a `Store`, and that he **created** a new _local_ class called `ExtendedStore`.
John Saunders
+3  A: 

You should not inherit from a proxy type returned from WCF. Consider that the type does not belong to you!

You can do some "extension" using the partial class feature of C#, since the proxy classes are generated as partial classes. Instead of creating class ExtendedStore with the Id property, try:

public partial class Store
{
    public int Id {get;set;}
}

This adds an Id property to the Store class. You can also add methods events, etc. in this manner.

The partial class will need to be defined in the same project tha contains the service reference.


Consider a project with root namespace "Project". You have a service reference named "Commerce" to a web service that returns a "Store" object. That means there is a class named Project.Commerce.Store:

// Proxy code generated by "Add Service Reference":
namespace Project.Commerce {
    [DataContract]
    public partial class Store {
        [DataMember]
        public string StoreName {get;set;}
        // More data members here
    }
}

You will create a folder under your project root named "Commerce". This is so that the namespaces of classes you create there will be "Project.Commerce". Then create your partial class:

// This is your code in Store.cs in the new "Commerce" folder:
namespace Project.Commerce {
    public partial class Store {
        public int Id {get;set;}
        public override string ToString() {
            return String.Format("Store #{0}: {1}", Id, StoreName);
        }
    }
}
John Saunders
Probably the easiest solution, if he's actually generating a proxy. I find it a bit odd that you have a problem with inheritance, but not with extending the class through the partial class mechanism. The reasons for not inheriting from the class are also valid for extending in this fashion, imo.
Thorarin
@john How would I use this partial class now?
vikasde
@john Thanks for the help.
vikasde