在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;
}
我已经看过几个解释并尝试了建议的修复,但没有一个有效。
任何帮助将不胜感激。 非常感谢, 迈克托马斯
答案 0 :(得分:2)
您需要:
public List<Accounts> GetAccounts()
{
//just saw what you were doing above
return Accounts.GetAccounts();
}
或者如果您计划覆盖子类中的功能:
public abstract List<Accounts> GetAccounts();
基本上,如果没有编译器可以编译或者不让编译器知道你将在以后实现该方法,你就不能拥有一个空方法。
答案 1 :(得分:1)
你有两件事情,一个界面和实际的服务。接口定义了服务辅助,实际服务实现了该合同。您需要了解如何实现服务合同(即定义接口),定义服务的端点(即可用方法),然后如何实际实现该合同。
请参阅MSDN上的这两页:
Implementing Service Contracts
从第2页开始考试:
// 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; }
}