Exception WCF lors du retour de la liste des objets d'interface
-
13-11-2019 - |
Question
J'obtiens l'exception suivante lorsque j'appelle le service WCF à partir du site Web ASP.NET. Comment pouvons-nous le surmonter?
Remarque: En appliquant Break Point in Service Project, j'ai vérifié que le service renvoie deux objets valides.
Remarque: En service, je retire la liste d'IbankAccount. [OperationContract]
List<IBankAccount> GetDataUsingDataContract(int userId);
L'IbankAccount est une interface.
L'exception indique "la connexion sous-jacente a été fermée: la connexion a été fermée de façon inattendue". Une trace de pile détaillée est disponible dans l'image suivante.
//Site Internet
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;
}
}
// Interface de service
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; }
}
}
// Implémentation de service
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;
}
}
}
// Couche commerciale
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;
}
}
}
// couche d'accès aux données
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;
}
}
}
Lecture: 1. Conception d'objets WCF - OOP vs SOA
La solution
Selon ce post:http://social.msdn.microsoft.com/forums/eu/wcf/thread/31102bd8-0a1a-44f8-b183-62926390b3c3Vous ne pouvez pas renvoyer une interface - vous devez renvoyer une classe. Cependant, vous pouvez créer un type de base abstrait - Compte bancaire et appliquer le Connu attribut à lui. Par exemple:
[DataContract]
[KnowType(typeof(FixedAccount))]
[KnowType(typeof(SavingsAccount))]
abstract class BankAccount { ... }
[DataContract]
class FixedAccount : BankAccount { ... }
[DataContract]
class SavingsAccount : BankAccount { ... }
La Connu Attribut, WCF, sachez qu'un autre type, non explicitement référencé dans le contrat de service fera partie du contrat.