从客户端访问WCF服务时出错
问题描述:
我创建了一个添加了两个整数的简单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();
}
}
}
答
。在你的错误提示:
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创建的代理类的名称是从它实现的契约接口派生而来的。具体而言,代理类的名称似乎是由创建:
- 删除从合同接口的名称领先
I
和 - 追加
Client
。
从您发布的代码看,您的合同界面显示为IWcfService
。因此,Visual Studio为其生成的代理类创建名称WcfServiceClient
。
分辨率:为了避免编译错误Reference.cs
,在您的客户端代码,名称不是WcfServiceClient
其他命名空间的东西。
更新参考 – Sajeetharan
@Sajeetharan更新了几次。它正在更新。仍然出现错误 – Mangrio
您提供的参考名称是什么? – Sajeetharan