0

我需要将 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

}
4

2 回答 2

0

你应该只声明Drive_Resulttest.ServiceReference1.ReturnClass,而不是ReturnClass(你的命名空间有些混乱)。

于 2012-08-17T22:27:28.650 回答
0

您必须将 datacontract 设置为您的 returnClass 和 datamember [DataContract] public class ReturnClass { //private members private int _errorCode; 私有字符串_errorMessage;私人字符串_exMessage;私有 ArrayList _drives; 私人字符串[] _文件夹;私有字符串[] _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
于 2012-08-17T22:28:54.770 回答