简体   繁体   English

WCF DataContract类未在客户端应用程序中定义

[英]WCF DataContract Class is not defined in Client Application

I have a solution that contains a WCF Project and a Project for Data Classes, each class is owned as a "DT" class. 我有一个解决方案,其中包含WCF项目和数据类项目,每个类都作为“ DT”类拥有。 Each of these DT classes are marked as a DataContract() with each property labeled as a DataMember(). 这些DT类中的每一个都标记为DataContract(),而每个属性都标记为DataMember()。

    [DataContract()]
public class AttachmentDT : IAttachment
{

    private System.Int32 mAttachmentID;
    [DataMember()]
    public System.Int32 AttachmentID
    {
        get { return mAttachmentID; }
        set { mAttachmentID = value; }
    }

    private System.DateTime mCreateDate;
    [DataMember()]
    public System.DateTime CreateDate
    {
        get { return mCreateDate; }
        set { mCreateDate = value; }
    }

    private System.Byte[] mFileBytes;
    [DataMember()]
    public System.Byte[] FileBytes
    {
        get { return mFileBytes; }
        set { mFileBytes = value; }
    }

    private System.String mFileName;
    [DataMember()]
    public string FileName
    {
        get { return mFileName; }
        set { mFileName = value; }
    }
}

My WCF project has a class reference to the data classes project so i have access to these classes. 我的WCF项目具有对数据类项目的类引用,因此我可以访问这些类。 If i do not use any of them in the WCF interface, the client application that uses the WCF cannot see the data classes either. 如果我没有在WCF界面中使用它们中的任何一个,则使用WCF的客户端应用程序也不会看到数据类。 Why is this? 为什么是这样? Must i seriously have to create an empty method for each data class that i want the client to see? 我是否必须认真地为我希望客户端查看的每个数据类创建一个空方法? For example in my WCF interface, i create a useless method... 例如在我的WCF界面中,我创建了一个无用的方法...

    [OperationContract()]
    void AttachmentDT(AttachmentDT a) { }

If i have 20 data classes i want to give visibility too, i have to do this stupid crap for each? 如果我有20个数据类,我也想提供可见性,那么我必须对每个数据类都做这个愚蠢的废话吗? Please tell me there is a less ignorant way to get visibility to these DT classes. 请告诉我,有一种不太了解的方法可以使您了解这些DT类。

I agree with Rahul. 我同意拉胡尔的说法。 Your intentions does not make much sense to me, but if you still want to go there, i think you can achieve this by simply adding: 您的意图对我来说没有多大意义,但是如果您仍然想去那里,我想您可以通过简单地添加以下内容来实现:

[ServiceKnownType(AttachmentDT)]

attributes on top of your wcf interface. wcf界面顶部的属性。 That way you won't need those empty methods, and the service WSDL should contain those types, allowing any client who references your WCF service to generate proxies for them. 这样,您将不需要那些空方法,并且服务WSDL应该包含那些类型,从而允许任何引用您的WCF服务的客户端为其生成代理。

Aside from the usual issues like class visibility (public in your case) and missing the DataMember attribute (also not the case here), I usually run into this issue with duplicate types in referenced assemblies between client and server components. 除了常见的问题,例如类可见性(在您的情况下为public)和缺少DataMember属性(在此情况下也不是)之外,我通常还会在客户端和服务器组件之间的引用程序集中遇到重复类型的问题。

Assuming you're generating the client-side proxy in Visual Studio, have you tried checking the "re-use types in referenced assemblies" option? 假设您正在Visual Studio中生成客户端代理,是否尝试检查“引用程序集中的重用类型”选项? I sometimes run into issues unless I reuse System.Runtime.Serialization.dll 除非重新使用System.Runtime.Serialization.dll,否则有时会遇到问题

Here's the MSDN tutorial on it: http://msdn.microsoft.com/en-us/library/vstudio/bb628653(v=vs.100).aspx 这是关于它的MSDN教程: http : //msdn.microsoft.com/zh-cn/library/vstudio/bb628653(v=vs.100).aspx

That's obvious right? 那很明显吧? I mean that's what we call Abstraction-encapsulation . 我的意思是我们称之为Abstraction-encapsulation Those Data classes are visible (if that have to be) only through WCF service layer. 这些数据类仅在WCF服务层可见(如果必须)。 Client don't have (shouldn't have) any idea what those data classes are and what they do. 客户没有(应该没有)任何想法,这些数据类是什么以及它们做什么。

If you still want to explicitly have access to those Data classes and their members, you can always add a reference to those Data Class project DLL, to your client application; 如果您仍然想显式地访问那些数据类及其成员,则可以始终将对这些数据类项目DLL的引用添加到客户端应用程序中。 but which may not be recommended (OR) may not make much sense. 但可能不建议(OR),但意义不大。

我同意Roman将[ServiceKnownType(...)]到WCF服务的界面中,并向客户端正确公开我所有的数据协定类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM