插入方法有效,但 GET 方法无效
Insert method working but GET method does not
我正在使用 WCF 对 Northwind 数据库进行 CRUD 操作。
首先,我创建了 POST 方法,当我尝试使用 WCF 测试客户端时该方法有效,但 get 方法显示此错误:
调用服务失败。可能原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。有关详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理、恢复默认配置或刷新服务来恢复。
我不知道是否需要让我的 ViewModel 具有与 Employees 相同的属性 class 然后迭代它并显示结果?
这是配置文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" sendTimeout="00:05:00" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:55658/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
这是获取方法:
public IEnumerable<Employee> GetEmployees()
{
List<Employee> list = new List<Employee>();
NorthwindContext db = new NorthwindContext();
list = db.Employees.ToList();
return list;
}
这是服务:
[ServiceContract]
public interface IService1
{
[OperationContract]
IEnumerable<Employee> GetEmployees();
[OperationContract]
void InsertEmployee(Employee e);
[OperationContract]
void UpdateEmployee(Employee e);
[OperationContract]
void DeleteEmployee(int id);
}
更新
好的,我解决了,问题是员工 class 有外键,而那个客户不能 "read" 它并且显示错误,因为他不知道如何阅读 属性.
我所做的只是创建了 EmployeeView class 并插入了我想要显示的属性。
Get 方法现在看起来像这样
public IEnumerable<EmployeeView> GetEmployees()
{
NorthwindContext db = new NorthwindContext();
IQueryable<EmployeeView> list = db.Employees.Select(e => new EmployeeView
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName
});
return list;
}
WCF 通过属性公开所谓的契约,将以下属性添加到您的 Get 方法以使其对服务可见
[操作合约]
您可以查看 https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.operationcontractattribute?view=netframework-4.7.2 了解更多信息。
它的要点是这个
Indicates that a method defines an operation that is part of a service
contract in a Windows Communication Foundation (WCF) application.
[OperationContract]
public IEnumerable<Employee> GetEmployees()
{
List<Employee> list = new List<Employee>();
NorthwindContext db = new NorthwindContext();
list = db.Employees.ToList();
return list;
}
然后下一步是浏览到该服务并检查它在浏览器中显示的内容(VS 中简单的 运行 服务就足够了)
或者,如果该服务已托管,则您可以从浏览器浏览到它。
更好的服务测试是使用 WCF 测试客户端,如果您安装了 visual studio,这应该是默认设置。
如果员工有另一个 table 的外键,将出现解析错误。您需要为员工 class
创建另一个模型 dto
型号:
public int EmployeeId {get;set;}
public ICollection<Order> Orders{get;set;} // this causes to parse error. Because this object have ICollection<Employee> and this causes infinite loop
ModelDto:
public int EmployeeId {get;set;}
或者如果你想发送订单,你可以创建另一个dto
我正在使用 WCF 对 Northwind 数据库进行 CRUD 操作。
首先,我创建了 POST 方法,当我尝试使用 WCF 测试客户端时该方法有效,但 get 方法显示此错误:
调用服务失败。可能原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。有关详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理、恢复默认配置或刷新服务来恢复。
我不知道是否需要让我的 ViewModel 具有与 Employees 相同的属性 class 然后迭代它并显示结果?
这是配置文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" sendTimeout="00:05:00" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:55658/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
这是获取方法:
public IEnumerable<Employee> GetEmployees()
{
List<Employee> list = new List<Employee>();
NorthwindContext db = new NorthwindContext();
list = db.Employees.ToList();
return list;
}
这是服务:
[ServiceContract]
public interface IService1
{
[OperationContract]
IEnumerable<Employee> GetEmployees();
[OperationContract]
void InsertEmployee(Employee e);
[OperationContract]
void UpdateEmployee(Employee e);
[OperationContract]
void DeleteEmployee(int id);
}
更新
好的,我解决了,问题是员工 class 有外键,而那个客户不能 "read" 它并且显示错误,因为他不知道如何阅读 属性.
我所做的只是创建了 EmployeeView class 并插入了我想要显示的属性。
Get 方法现在看起来像这样
public IEnumerable<EmployeeView> GetEmployees()
{
NorthwindContext db = new NorthwindContext();
IQueryable<EmployeeView> list = db.Employees.Select(e => new EmployeeView
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName
});
return list;
}
WCF 通过属性公开所谓的契约,将以下属性添加到您的 Get 方法以使其对服务可见
[操作合约]
您可以查看 https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.operationcontractattribute?view=netframework-4.7.2 了解更多信息。
它的要点是这个
Indicates that a method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.
[OperationContract]
public IEnumerable<Employee> GetEmployees()
{
List<Employee> list = new List<Employee>();
NorthwindContext db = new NorthwindContext();
list = db.Employees.ToList();
return list;
}
然后下一步是浏览到该服务并检查它在浏览器中显示的内容(VS 中简单的 运行 服务就足够了) 或者,如果该服务已托管,则您可以从浏览器浏览到它。
更好的服务测试是使用 WCF 测试客户端,如果您安装了 visual studio,这应该是默认设置。
如果员工有另一个 table 的外键,将出现解析错误。您需要为员工 class
创建另一个模型 dto型号:
public int EmployeeId {get;set;}
public ICollection<Order> Orders{get;set;} // this causes to parse error. Because this object have ICollection<Employee> and this causes infinite loop
ModelDto:
public int EmployeeId {get;set;}
或者如果你想发送订单,你可以创建另一个dto