【问题标题】:ASP.NET Compiler Error must declare a body because it is not marked abstractASP.NET 编译器错误必须声明一个主体,因为它没有被标记为抽象
【发布时间】:2009-07-27 22:03:26
【问题描述】:

在 ASP.NET 3.5 中,当我尝试编译 Silverlight 应用程序的 Web 服务项目时出现上述错误

产生错误的代码行在'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]
  }
}

这是它引用的方法:

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

我已经看过几种解释并尝试了建议的修复方法,但都没有奏效。

任何帮助将不胜感激。 非常感谢, 迈克·托马斯

【问题讨论】:

    标签: c# asp.net silverlight


    【解决方案1】:

    你要么需要:

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

    或者,如果您打算覆盖子类中的功能,则这样做:

    public abstract List<Accounts> GetAccounts();
    

    基本上,如果编译器没有要编译的内容或不让编译器知道您稍后将实现该方法,您就不能有一个空方法。

    【讨论】:

      【解决方案2】:

      你有两件事混合在一起,一个接口和实际的服务。接口定义了服务契约并且实际的服务实现了该契约。您需要阅读如何实现服务契约(即定义一个接口),它为您的服务定义端点(即可用方法),然后如何实际实现该契约。

      在 MSDN 上查看这 2 个页面:

      Designing Service Contracts

      Implementing Service Contracts

      示例取自第二页:

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

      或者,直接在服务类上定义:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多