如何扩展WCF返回类正确? [英] How to extend WCF returned class properly?

查看:94
本文介绍了如何扩展WCF返回类正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用WCF服务。此服务返回一个名为Store的类。我创建了一个新的本地类继承自Store。我的类被称为ExtendedStore。
我的ExtendedStore类似于:

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;
    ....
}



现在我使用WCF服务使用以下代码转换我的类:

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



我不能将返回的Store类从服务转换到我的ExtendedStore类。
我得到以下错误消息:

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


无法转换类型为
的对象ConsoleApplication1.StoreManagerService.Store'
to type
'ConsoleApplication1.ExtendedStore'。

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?

推荐答案

您不应继承从WCF返回的代理类型。考虑该类型不属于你!

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

您可以使用C#的部分类功能来做一些扩展,因为代理类是作为部分类生成的。不要使用 Id 属性创建类 ExtendedStore ,请尝试:

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;}
}

这会为 Store 类添加一个Id属性。您还可以以这种方式添加方法事件等。

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.

考虑一个带有根命名空间Project的项目。您有一个名为Commerce的服务引用到返回Store对象的Web服务。这意味着有一个名为 Project.Commerce.Store 的类:

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
    }
}


b $ b

您将在您的项目根目录下创建一个名为Commerce的文件夹。这样,在那里创建的类的命名空间将是Project.Commerce。然后创建你的部分类:

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);
        }
    }
}

这篇关于如何扩展WCF返回类正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆