错误从客户端访问WCF服务 [英] Error accessing a WCF service from a client
问题描述
我创建了一个简单的WCF服务,增加了两个整数。该服务主机开始完美。但在客户端,我得到以下编译错误 Reference.cs
:
类型名称'ServiceReference1'中不存在类型
WcfServiceClient.ServiceReference1.WcfServiceClient
块引用>客户端code:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;命名空间WcfServiceClient
{
公共部分类WebForm1的:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ } 保护无效的button1_Click(对象发件人,EventArgs的发送)
{
ServiceReference1.WcfServiceClient客户端=新ServiceReference1.WcfServiceClient(BasicHttpBinding_IWcfService);
INT结果= client.Add(Convert.ToInt32(TextBox1.Text),Convert.ToInt32(TextBox2.Text));
Label1.Text = result.ToString();
}
}
}
解决方案有在你的错误提示:
类型名称
'ServiceReference1
中不存在的类型
'WcfServiceClient.ServiceReference1.WcfServiceClient
块引用>请注意,生成的类名
WcfServiceClient
相同的命名空间的第一部分的名称:WcfServiceClient.ServiceReference1.WcfServiceClient
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
产生第一个部件
命名空间类名这是导致无法解析
WcfServiceClient
类。 (在.NET中,是一般建议,以确保一个类名是不一样的命名空间组件的名称。)请注意,你不专门提供自动生成的代理类的名称;名字是由Visual Studio为您创建。我认为,Visual Studio创建代理类的名字是从它实现合同接口派生。具体来说,代理类的名称似乎创建人:
- 删除龙头
I
从合同接口的名称,- 追加
客户端
。从code你张贴,它出现在您的合同接口被命名为
IWcfService
。所以从这一点,Visual Studio创建名称WcfServiceClient
为它生成的代理类。分辨率::要避免
Reference.cs
编译错误,在您的客户端code,命名空间的其他东西比WcfServiceClient
。I have created a simple WCF service that adds two integers. The service host started perfectly. But on the client-side, I am getting the following compilation error in
Reference.cs
:The type name 'ServiceReference1' does not exist in the type 'WcfServiceClient.ServiceReference1.WcfServiceClient'
Client-Side Code:
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(); } } }
解决方案There is a hint in your error:
The type name
'ServiceReference1'
does not exist in the type'WcfServiceClient.ServiceReference1.WcfServiceClient'
Note that the generated class name
WcfServiceClient
is the same as the name of the first component of your namespace:WcfServiceClient.ServiceReference1.WcfServiceClient ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ 1st component generated of namespace class name
This is leading to the inability to resolve the
WcfServiceClient
class. (In .NET, is it generally advisable to make sure that a class name is not the same as the name of a namespace component.)Note that you do not specifically provide a name for the auto-generated proxy class; the name is created for you by Visual Studio. I believe that the name of the proxy class that Visual Studio creates is derived from the contract interface that it implements. Specifically, the name of the proxy class appears to be created by:
- Dropping the leading
I
from the name of the contract interface, and- Appending
Client
.From the code you posted, it appears your contract interface is named
IWcfService
. So from that, Visual Studio creates the nameWcfServiceClient
for the proxy class that it generates.Resolution: To avoid the compilation error in
Reference.cs
, in your client-side code, name your namespace something other thanWcfServiceClient
.这篇关于错误从客户端访问WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!