【问题标题】:Converting from Service reference to client?从服务引用转换为客户端?
【发布时间】:2017-01-01 19:20:55
【问题描述】:

我有一个带有 EF(实体框架)的 WCF Web 服务,用于访问数据库中的产品表。我在我的项目中添加了一个测试客户端,用于在输入 ID 时显示退货产品。

这是我在服务接口中的方法:

[OperationContract]
    VareWS VareWSGet(String barcode);

这是服务中实现的方法:

public VareWS VareWSGet(string barcode)
    {
        ExamDBEntities3 context = new ExamDBEntities3();
        var vareEntity = (from v
                             in context.Vare
                             where v.barcode == barcode
                             select v).FirstOrDefault();
        if (vareEntity != null)
            return TranslateProductEntityToProduct(vareEntity);
        else
            throw new Exception("Invalid product id");
    }

这是用于翻译产品实体的私有方法:

private VareWS TranslateProductEntityToProduct(
              Vare vareEntity)
    {
        VareWS vare = new VareWS();
        vare.navn = vareEntity.navn;
        vare.pris = (int)vareEntity.pris;
        return vare;
    }

在我的客户端中,我使用此代码在我的 Web 服务中调用该方法:

sc = new Service1Client();
        string inputID = TextBox1.Text;
        VareWS v = sc.VareWSGet(inputID);

服务引用已成功添加,但我收到一条错误消息,提示“无法将类型 'ServiceReference1.VareWS' 隐式转换为 'VareWS'” 我不确定如何更正此错误,因为我已指定与我的服务客户端的连接。如果您需要更多信息,请告诉我,谢谢。

使用语句:

客户:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamOpg.ServiceReference2;

服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

【问题讨论】:

  • 我能在那个文件中看到所有的 using 语句吗?
  • 这里是客户端的使用语句:
  • 使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;使用 ExamOpg.ServiceReference2;
  • 对不起,我的格式化好像失败了,我会在原帖中添加。
  • 你在ExamOpg.ServiceReference2 中也有VareWS 吗?

标签: c# entity-framework web-services wcf


【解决方案1】:

根据你的使用说明:

using ExamOpg.ServiceReference2;

类型 VareWS 将是该命名空间中的类型 VareWS。但是您正在尝试将类型 VareWSServiceReference1.VareWS 转换为该类型。这显然是不可行的,所以你得到了那个例外。

将您的 using 语句更改为 ExamOpg.ServiceReference1ServiceReference1 所在的任何名称空间。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多