Mike Thomas
Mike Thomas

Reputation:

ASP.NET Compiler Error must declare a body because it is not marked abstract

IN ASP.NET 3.5 I am getting the above error when I try to compile the web service project of a Silverlight application

The code line generating the error is below ' public List GetAccounts();'

namespace BegSilver.Web
{
  [ServiceContract(Namespace = "")]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class Service1
  {
    [OperationContract]
   public List<Accounts> GetAccounts();

    // Add more operations here and mark them with [OperationContract]
  }
}

This is the method it is referencing:

  public class Accounts
  {
    private  Int32 acpk {get; set;}
    private Decimal acctnumber { get; set; }
    private String name { get; set; }
    private String type { get; set; }


    public static List<Accounts> GetAccounts()
    {
      List<Accounts> a = new List<Accounts>();

      a.Add(
        new Accounts()
        {
          acpk = 1,
          acctnumber = 332.3m,
          name = "Test",
          type = "CASH"
        });

     return a;
    }

I've seen several explanations and tried the suggested fixes, but none of them have worked.

Any help would be appreciated. Many thanks, Mike Thomas

Upvotes: 1

Views: 1877

Answers (2)

Colin
Colin

Reputation: 10638

You have 2 things mixed up, an Interface and the actual service. The Interface defines the Service COntract and the actual service implements that contract. You need to read up on how to implement Service Contract (i.e. define an interface), which defines the endpoints (i.e. available methods) for your service and then how to actual implement that contract.

See these 2 pages on MSDN:

Designing Service Contracts

Implementing Service Contracts

Examle taken from the 2nd page:

// Define the IMath contract.
[ServiceContract]
public interface IMath
{
    [OperationContract] 
    double Add(double A, double B);

    [OperationContract]
    double Multiply (double A, double B);
}
// Implement the IMath contract in the MathService class.
public class MathService : IMath
{
    public double Add (double A, double B) { return A + B; }
    public double Multiply (double A, double B) { return A * B; }
}

or, defining it directly on the service class:

[ServiceContract]class MathService
{
    [OperationContract]
    public double Add(double A, double B) { return A + B; } 
    // notice the body of the method is filled in between the curly braces
    [OperationContract]
    public double Multiply (double A, double B) { return A * B; }
}

Upvotes: 1

Relster
Relster

Reputation: 447

You either need:

public List<Accounts> GetAccounts() 
{
    //just saw what you were doing above
    return Accounts.GetAccounts();
}

or this if you plan on overriding the functionality in a sub-class:

public abstract List<Accounts> GetAccounts();

Basically, you just can't have an empty method without the compiler having something to compile or without letting the compiler know you're going to implement the method later.

Upvotes: 2

Related Questions