【问题标题】:Simple WCF Service [closed]简单的 WCF 服务 [关闭]
【发布时间】:2015-06-09 16:30:56
【问题描述】:

我尝试编写我的第一个 WCF 服务,但我遇到了一些问题,

首先,我创建了一个 WCF 项目,
然后我添加了实体模型。
之后我添加了 IEmpService.svc 文件。然后我将获得一个客户列表。

我关注THIS BLOG POST

IEmpService

   [ServiceContract]
public interface IEmpService
{

    [OperationContract]
    List<Customer> GetAllCustomers();

}

企业服务

   public class EmpService : IEmpService
{
    public EmpDBEntities dbent = new EmpDBEntities(); // I can't create thisone inside GetAllCustomer method.

    public List<Customer> GetAllCustomers
    { 
     //var x = from n in dbent.Customer select n; // This is what i need to get but in here this program not recognize `var` also.
     //return x.ToList<Customer>();
    }
}

谁能告诉我我错过了哪一点?或者为什么会出现这个问题?如何解决这个问题?

【问题讨论】:

  • 我看不出问题出在哪里。您能否更具体地说明您遇到问题的地方?
  • @jp2code 在我的 GetAllCustomers 方法内的 EmpService 文件中,无法识别注释行
  • GetAllCustomers() 缺少括号
  • 我也不是刚刚投反对票的人。我不认为反对是本网站的有用功能。
  • 你使用的是什么版本的框架?

标签: c# asp.net asp.net-mvc wcf


【解决方案1】:

不太确定您的问题是什么,但您是否将“客户”定义为 DataContract?如果这是您的服务返回的对象,您需要定义它以便客户端可以使用它。

【讨论】:

    【解决方案2】:

    我仍然对你的问题感到困惑,但我会尝试回答。

    如果您想返回 Customer 类,则需要有一个带有 DataMembersDataContract

    你可能看过这个例子:

    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";
    
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
    
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
    

    另外,不要返回列表。 Microsoft 定义了 List,但这是一个 Web 服务 - 世界其他地方(Apple、android、linux、php 等)将不知道如何解释 List。

    改为将函数的签名更改为字符串数组。

    [OperationContract]
    string[] GetAllCustomers();
    

    【讨论】:

      【解决方案3】:

      如果你想在 GetAllCustomer 方法中创建它,你应该删除 public 关键字。像这样:

      public List<Customer> GetAllCustomers()
      {
          EmpDBEntities dbent = new EmpDBEntities();
          var x = from n in dbent.Students select n; 
          return x.ToList<Student>();
      }
      

      【讨论】:

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