2015-07-11 48 views
1

我創建了一個添加了兩個整數的簡單WCF服務。服務主機完美啓動。但在客戶端,我得到以下編譯錯誤Reference.cs從客戶端訪問WCF服務時出錯

The type name 'ServiceReference1' does not exist in the type 'WcfServiceClient.ServiceReference1.WcfServiceClient'

客戶端代碼:

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

namespace WcfServiceClient 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      ServiceReference1.WcfServiceClient client = new ServiceReference1.WcfServiceClient("BasicHttpBinding_IWcfService"); 
      int result = client.Add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text)); 
      Label1.Text = result.ToString(); 
     } 
    } 
} 
+0

更新參考 – Sajeetharan

+0

@Sajeetharan更新了幾次。它正在更新。仍然出現錯誤 – Mangrio

+0

您提供的參考名稱是什麼? – Sajeetharan

回答

1

。在你的錯誤提示:

The type name 'ServiceReference1' does not exist in the type 'WcfServiceClient.ServiceReference1.WcfServiceClient'

請注意,生成的類名稱WcfServiceClient與名稱空間的第一個組件的名稱相同:

WcfServiceClient.ServiceReference1.WcfServiceClient 
^^^^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^^ 
1st component      generated 
of namespace      class name 

這導致無法解決WcfServiceClient類。 (在.NET中,通常建議確保類名與名稱空間組件的名稱不同。)

請注意,您並未專門爲自動生成的代理類提供名稱;該名稱是由Visual Studio爲您創建的。我相信Visual Studio創建的代理類的名稱是從它實現的契約接口派生而來的。具體而言,代理類的名稱似乎是由創建:

  1. 刪除從合同接口的名稱領先I
  2. 追加Client

從您發佈的代碼看,您的合同界面顯示爲IWcfService。因此,Visual Studio爲其生成的代理類創建名稱WcfServiceClient

分辨率:爲了避免編譯錯誤Reference.cs,在您的客戶端代碼,名稱不是WcfServiceClient其他命名空間的東西。

相關問題