简单的 WCF 服务
Simple WCF Service
我尝试编写我的第一个 WCF 服务,但我遇到了一些问题,
首先,我创建了一个 WCF 项目,
然后我添加了实体模型。
之后我添加了 IEmpService.svc file.then 我将得到一个客户列表。
IEmpService
[ServiceContract]
public interface IEmpService
{
[OperationContract]
List<Customer> GetAllCustomers();
}
EmpService
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>();
}
}
谁能告诉我我漏掉了哪一点?或者为什么会出现这个问题?如何解决这个问题?
不太确定您的问题是什么,但是您是否将 "Customer" 定义为 DataContract?如果那是您的服务 returns 的对象,您需要定义它以便客户端可以使用它。
我仍然对你的问题感到困惑,但我会尝试回答。
您的 Customer class 需要 DataContract 和 DataMembers 如果你想要return它。
你可能见过这个例子:
[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; }
}
}
此外,不要 return List。 Microsoft 定义了 List,但这是一个 Web 服务 - 世界其他地方(苹果、android、linux、php 等)将不知道如何解释 List。
相反,将函数的签名更改为字符串数组。
[OperationContract]
string[] GetAllCustomers();
如果你想在 GetAllCustomer
方法中创建它,你应该删除 public
关键字。像这样:
public List<Customer> GetAllCustomers()
{
EmpDBEntities dbent = new EmpDBEntities();
var x = from n in dbent.Students select n;
return x.ToList<Student>();
}
我尝试编写我的第一个 WCF 服务,但我遇到了一些问题,
首先,我创建了一个 WCF 项目,
然后我添加了实体模型。
之后我添加了 IEmpService.svc file.then 我将得到一个客户列表。
IEmpService
[ServiceContract]
public interface IEmpService
{
[OperationContract]
List<Customer> GetAllCustomers();
}
EmpService
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>();
}
}
谁能告诉我我漏掉了哪一点?或者为什么会出现这个问题?如何解决这个问题?
不太确定您的问题是什么,但是您是否将 "Customer" 定义为 DataContract?如果那是您的服务 returns 的对象,您需要定义它以便客户端可以使用它。
我仍然对你的问题感到困惑,但我会尝试回答。
您的 Customer class 需要 DataContract 和 DataMembers 如果你想要return它。
你可能见过这个例子:
[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; }
}
}
此外,不要 return List。 Microsoft 定义了 List,但这是一个 Web 服务 - 世界其他地方(苹果、android、linux、php 等)将不知道如何解释 List。
相反,将函数的签名更改为字符串数组。
[OperationContract]
string[] GetAllCustomers();
如果你想在 GetAllCustomer
方法中创建它,你应该删除 public
关键字。像这样:
public List<Customer> GetAllCustomers()
{
EmpDBEntities dbent = new EmpDBEntities();
var x = from n in dbent.Students select n;
return x.ToList<Student>();
}