感谢Abel,我能够弄清楚。下面是解决方案。我希望这对其他人有帮助。
请注意,您还必须右键单击 asp.net 中的服务引用,然后单击“配置服务引用”并将“集合类型”设置为 Systems.Collections.Generic.List
这是我在 asp.net 中调用函数的方式
Item NewItem = new Item();
foreach (GridViewRow PendingItemUnderControl in GridViewPendingList.Rows)
{
NewItem.Paramater = PendingItemUnderControl.Cells[0].Text.ToLower();
NewItem.Type = (String)Session["BrowseType"];
lstNewItems.Add(NewItem);
}
ControlFiles_Result = Client.FilesToControl(lstNewItems);
在asp.net中lstItems的定义如下
List<Item> lstNewItems = new List<Item>(); // Control Items
Abel 说我需要在我的 asp 项目顶部添加一个引用是正确的。另请注意,项目是在 WCF 应用程序中声明的,因此无需在 asp.net 中声明它,因为我们在下面的行中引用了它,如下所示:
using test.ServiceReference1;
现在在 WCF 中记录 IJobs.cs 中的代码。请注意私人和公共的测试,我将所有这些都公开,这造成了严重破坏。
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
[OperationContract]
ReturnClass FilesToControl(List<Item> ItemsToControl);
}
[DataContract]
// [KnownType(typeof(List<Item>))]
[KnownType(typeof(Item))]
public class Item
{
public Item(string Paramater, string Type)
{
_Paramater = Paramater;
_Type = Type;
}
private string _Paramater;
[DataMember]
public string Paramater
{
get { return _Paramater; }
set { _Paramater = value; }
}
private string _Type;
[DataMember]
public string Type
{
get { return _Type; }
set { _Type = value; }
}
}
}
还有 Jobs.cs - 通过一个简单的测试来确保它正常工作。
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;
using System.Web;
using Microsoft.Win32;
namespace WCFJobsLibrary
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Jobs" in both code and config file together.
public class Jobs : IJobs
{
#region IJobs Members
//Files Manager
public ReturnClass FilesToControl(List<Item> lstNewItems)
{
try
{
String ThisisAnItemToControl = "";
String ThisIsItsType = "";
for (int i = 0; i < lstNewItems.Count; i++) // Loop through List with for
{
ThisisAnItemToControl = lstNewItems[i].Paramater;
ThisIsItsType = lstNewItems[i].Type;
}
return new ReturnClass(1, ThisisAnItemToControl, String.Empty, null, null, null);
}
catch (Exception ex)
{
return new ReturnClass(-1, ex.Message.ToString(), ex.InnerException.ToString(), null, null, null);
}
}
}
#endregion
最后是ReturnClass.cs类中ReturnClass的定义
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;
using System.Web;
using Microsoft.Win32;
namespace WCFJobsLibrary
{
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
}
}