返回自定义对象错误

时间:2012-08-17 22:23:54

标签: c# wcf

我需要将WCF服务中的自定义对象返回给调用的asp.net应用程序。 我收到了错误 无法将类型'test.ServiceReference1.ReturnClass'隐式转换为'test.ReturnClass' 这是我在asp.net中的调用函数(错误我粗体) 我能回复我的对象吗?

// Get the drive list from the client machine
ServiceReference1.JobsClient Client = new ServiceReference1.JobsClient();
Client.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://" + DropDownListSystems.SelectedValue + ":8732/test/");
ReturnClass Drive_Result = new ReturnClass(); // Declare an instance of the return object that will contain all the results
**Drive_Result = Client.FindDrives();**

这是我的WCF代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Collections;
namespace WCFJobsLibrary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IJobs" in both code and config file together.
    [ServiceContract]
    public interface IJobs
    {
         //Directoy Manager
         [OperationContract]
        ReturnClass FindDrives();
         //Directoy Manager
         [OperationContract]
         ReturnClass FindSubfolders(String Folder_To_Search);
         //Directoy Manager
         [OperationContract]
         ReturnClass FindSubFiles(String Folder_To_Search);


    }
}

//Directoy Manager
public ReturnClass FindDrives()
{

    try
    {
        DriveInfo[] allDrives = DriveInfo.GetDrives();
        ArrayList Drives = new ArrayList();
        foreach (DriveInfo d in allDrives)
        {
            Drives.Add(d.Name);
        }
        return new ReturnClass(1, String.Empty, String.Empty, Drives, null, null);
    }
    catch (Exception ex)
    {
        return new ReturnClass(-1, ex.Message.ToString(), ex.InnerException.ToString(), null, null, null);
    }

}



public class ReturnClass
{
    //private members
    private int _errorCode;
    private string _errorMessage;
    private string _exMessage;
    private ArrayList _drives;
    private string[] _folders;
    private string[] _filePaths;


    #region Constructors

    //constructor 1
    public ReturnClass()
    {

    }

    //constructor 2
    public ReturnClass(int iErr, string sErrMsg, string ExMsg, ArrayList arrDrives, string[] sfolders, string[] sFilePaths)
    {
        ErrorCode = iErr;
        ErrorMessage = sErrMsg;
        ExMessage = ExMsg;
        Drives = arrDrives;
        Folders = sfolders;
        FilePaths = sFilePaths;
    }

    #endregion

    #region methods

    //Error Code
    public int ErrorCode
    {
        get { return this._errorCode; }
        set { this._errorCode = value; }
    }

    //error message
    public string ErrorMessage
    {
        get { return this._errorMessage; }
        set { this._errorMessage = value; }
    }

    //exception message
    public string ExMessage
    {
        get { return this._exMessage; }
        set { this._exMessage = value; }
    }

    //drives
    public ArrayList Drives
    {
        get { return this._drives; }
        set { this._drives = value; }
    }

    //folders
    public string[] Folders
    {
        get { return this._folders; }
        set { this._folders = value; }
    }

    //File Paths
    public string[] FilePaths
    {
        get { return this._filePaths; }
        set { this._filePaths = value; }
    }

    #endregion

}

2 个答案:

答案 0 :(得分:0)

您应该将Drive_Result声明为test.ServiceReference1.ReturnClass,而不是ReturnClass(有些内容与您的命名空间混淆)。

答案 1 :(得分:0)

您必须将datacontract设置为returnClass和datamember [DataContract] 公共类ReturnClass {     //私人会员     private int _errorCode;     private string _errorMessage;     private string _exMessage;     private ArrayList _drives;     private string [] _folders;     private string [] _filePaths;

#region Constructors

//constructor 1
public ReturnClass()
{

}

//constructor 2
public ReturnClass(int iErr, string sErrMsg, string ExMsg, ArrayList arrDrives, string[] sfolders, string[] sFilePaths)
{
    ErrorCode = iErr;
    ErrorMessage = sErrMsg;
    ExMessage = ExMsg;
    Drives = arrDrives;
    Folders = sfolders;
    FilePaths = sFilePaths;
}

#endregion

#region methods

//Error Code
[DataMember]
public int ErrorCode
{
    get { return this._errorCode; }
    set { this._errorCode = value; }
}

//error message
[DataMember]
public string ErrorMessage
{
    get { return this._errorMessage; }
    set { this._errorMessage = value; }
}

//exception message
[DataMember]
public string ExMessage
{
    get { return this._exMessage; }
    set { this._exMessage = value; }
}

//drives
[DataMember]
public ArrayList Drives
{
    get { return this._drives; }
    set { this._drives = value; }
}

//folders
[DataMember]
public string[] Folders
{
    get { return this._folders; }
    set { this._folders = value; }
}

//File Paths
[DataMember]
public string[] FilePaths
{
    get { return this._filePaths; }
    set { this._filePaths = value; }
}

#endregion