当我从 ASP.NET 网站调用 WCF 服务时,出现以下异常。我们怎样才能克服它?
注意:通过在服务项目中应用断点,我已经验证服务返回了两个有效对象。
注意:在服务中,我正在返回 IBankAccount 的列表。[OperationContract]
List<IBankAccount> GetDataUsingDataContract(int userId);
IBankAccount 是一个接口。
异常显示“底层连接已关闭:连接意外关闭”。下图中提供了详细的堆栈跟踪。
//网站
using System;
using ServiceReference1;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Service1Client client = new Service1Client();
string result = client.GetData(1);
Response.Write(result);
client.GetDataUsingDataContract(1);
int d = 0;
}
}
//服务接口
using System.Collections.Generic;
using System.ServiceModel;
using DTOProject;
namespace MyServiceApp
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
List<IBankAccount> GetDataUsingDataContract(int userId);
}
}
//DTO
using System.Runtime.Serialization;
namespace DTOProject
{
public interface IBankAccount
{
int Duration { get; set; }
int AmountDeposited { get; set; }
}
}
using System.Runtime.Serialization;
namespace DTOProject
{
[DataContract]
public class FixedAccount : IBankAccount
{
[DataMember]
public int Duration { get; set; }
[DataMember]
public int AmountDeposited { get; set; }
}
}
using System.Runtime.Serialization;
namespace DTOProject
{
[DataContract]
public class SavingsAccount : IBankAccount
{
[DataMember]
public int Duration { get; set; }
[DataMember]
public int AmountDeposited { get; set; }
}
}
//服务实现
using System.Collections.Generic;
using DTOProject;
using BusinessLayer;
namespace MyServiceApp
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public List<IBankAccount> GetDataUsingDataContract(int userId)
{
BusinessLayer.AccountManager accManager = new AccountManager();
List<IBankAccount> accounts = accManager.GetAllAccountsForUser(userId);
return accounts;
}
}
}
//业务层
using System.Collections.Generic;
using DTOProject;
using DataAccessLayer;
namespace BusinessLayer
{
public class AccountManager
{
public List<IBankAccount> GetAllAccountsForUser(int userID)
{
DataAccessLayer.AccounutManagerDAL accountManager = new AccounutManagerDAL();
List<IBankAccount> accountList = accountManager.GetAllAccountsForUser(userID);
return accountList;
}
}
}
//数据访问层
using System;
using System.Collections.Generic;
using DTOProject;
namespace DataAccessLayer
{
public class DatabaseRecordSimulation
{
public string AccountType { get; set; }
public int Duration { get; set; }
public int DepositedAmount { get; set; }
}
public class AccounutManagerDAL
{
List<DatabaseRecordSimulation> dbRecords = new List<DatabaseRecordSimulation>()
{
new DatabaseRecordSimulation{AccountType="Savings",Duration=6,DepositedAmount=50000},
new DatabaseRecordSimulation{AccountType="Fixed",Duration=6,DepositedAmount=50000}
};
public List<IBankAccount> GetAllAccountsForUser(int userID)
{
List<IBankAccount> accountList = new List<IBankAccount>();
foreach (DatabaseRecordSimulation dbRecrod in dbRecords)
{
IBankAccount acc = AccountFactory.GetAccount(dbRecrod);
accountList.Add(acc);
}
return accountList;
}
}
public static class AccountFactory
{
public static IBankAccount GetAccount(DatabaseRecordSimulation dbRecord)
{
IBankAccount theAccount = null;
if ( String.Equals(dbRecord.AccountType, "Fixed"))
{
theAccount = new FixedAccount();
}
if (String.Equals(dbRecord.AccountType, "Savings"))
{
theAccount = new SavingsAccount();
}
return theAccount;
}
}
}
阅读:1. WCF 对象设计 - OOP 与 SOA