2017-01-01 19 views
0

我有一個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語句:

客戶:

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; 
+0

我可以在該文件中看到所有使用語句嗎? – CodingYoshi

+0

這裏是客戶端的使用語句: – Leth

+0

using System; using System.Collections.Generic;使用System.Linq的 ; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; 使用ExamOpg.ServiceReference2; – Leth

回答

0

根據你使用的語句:

using ExamOpg.ServiceReference2; 

類型VareWS會該名稱空間中的類型爲VareWS。但是您試圖將類型VareWSServiceReference1.VareWS轉換爲該類型。這顯然是不可行的,所以你得到了這個例外。

將您的使用語句更改爲ExamOpg.ServiceReference1或任何名稱空間是ServiceReference1所在的位置。

相關問題