【问题标题】:Insert method working but GET method does not插入方法有效,但 GET 方法无效
【发布时间】:2019-04-18 12:26:52
【问题描述】:

我正在使用 WCF 对 Northwind 数据库进行 CRUD 操作。

首先我创建了 POST 方法,当我尝试使用 WCF 测试客户端时该方法有效,但 get 方法显示此错误:

调用服务失败。可能原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。有关更多详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理、恢复到默认配置或刷新服务来恢复。

我不知道是否需要让我的 ViewModel 具有与Employees 类相同的属性,然后对其进行迭代并显示结果?

这里是配置文件

<?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>

这里是get方法:

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

更新

好的,我解决了,问题是 Employee 类有外键,客户端无法“读取”它,并且显示错误,因为他不知道如何读取该属性。

我所做的只是创建了 EmployeeView 类并插入了我想要显示的属性。

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 Rest?
  • 我不知道 - 第一次创建 WCF 我只是使用 WCF 服务应用程序创建了一个项目

标签: c# .net wcf


【解决方案1】:

如果员工有另一个表的外键,则会出现解析错误。您需要为员工类创建另一个模型 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

【讨论】:

    【解决方案2】:

    WCF 通过属性公开所谓的契约,将以下属性添加到您的 Get 方法以使其对服务可见

    [运营合同]

    您可以查看https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.operationcontractattribute?view=netframework-4.7.2 了解更多信息。

    它的要点是这样的

    表示方法定义了作为服务一部分的操作 Windows Communication Foundation (WCF) 应用程序中的合同。

     [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,这应该是默认的。

    【讨论】:

    • 但是即使我在我的接口方法中设置了属性,我在编写方法时是否必须重复它们?由于 Insert 方法在没有它的情况下工作
    • 对不起,我的错误,你不必在服务和合同上这样做,让我稍微编辑一下我的答案
    • 一定要慢慢来
    猜你喜欢
    • 2019-03-14
    • 2019-03-31
    • 2020-05-22
    • 2017-05-10
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多