Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:03:59+00:00 2026-05-30T15:03:59+00:00

I am getting the following exception when I call the WCF Service from ASP.NET

  • 0

I am getting the following exception when I call the WCF Service from ASP.NET website. How can we overcome it?

Note: By applying break point in service project, I have verified that the service is returning two valid objects.

Note: In service, I am returing List of IBankAccount. [OperationContract]
List<IBankAccount> GetDataUsingDataContract(int userId);
The IBankAccount is an interface.

The exception says “The underlying connection was closed: The connection was closed unexpectedly”. Detailed stack trace is available in the following picture.

enter image description here

//Website

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;

}
}

//Service Interface

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; }
}
}

//Service Implementation

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;
    }

}
}

//Business Layer

 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;
    }

}
}

//Data Access Layer

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;

    }
}
}

READING:
1. WCF Object Design – OOP vs SOA

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:04:00+00:00Added an answer on May 30, 2026 at 3:04 pm

    As per this post:
    http://social.msdn.microsoft.com/Forums/eu/wcf/thread/31102bd8-0a1a-44f8-b183-62926390b3c3
    You cannot return an interface – you must return a class. However, you can create an abstract base type – BankAccount and apply the KnownType attribute to it. For example:

    [DataContract]
    [KnowType(typeof(FixedAccount))]
    [KnowType(typeof(SavingsAccount))]
    abstract class BankAccount { ... }
    
    [DataContract]
    class FixedAccount : BankAccount { ... }
    
    [DataContract]
    class SavingsAccount : BankAccount { ... }
    

    The KnownType attribute let’s WCF know that some other type, not explicitly referenced in the service contract will be a part of the contract.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting the following exception when I run my application in Release mode from
Hi i am getting the following exception while fetching the image from server. I
After upgrading to Android 2.3.4, I am getting following exception: javax.net.ssl.SSLException: Read error: ssl=0x*:
I'm getting the following exception when I try to call var log = LogManager.GetLogger(this.GetType());
I'm getting the following exception when calling 'GetProjectItemSummariesQuery' against my RIA Service. The weird
Folks -- I am getting the following exception and can't explain why. The number
I am getting the following exception on a call to Html.RenderPartial : The model
I'm getting the following error when trying to connect to a WCF web service:
I'm getting the following error in my WCF project: An unhandled exception of type
I am getting the following exception when trying to call: ManagementServiceV1X0 managementService = (ManagementServiceV1X0)factory.getPort(ManagementServiceV1X0.class,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.