【问题标题】:Error accessing a WCF service from a client从客户端访问 WCF 服务时出错
【发布时间】:2015-07-11 13:07:26
【问题描述】:

我创建了一个简单的 WCF 服务,该服务将两个整数相加。服务主机完美启动。但在客户端,我在Reference.cs 中收到以下编译错误:

类型中不存在类型名称“ServiceReference1” '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();
        }
    }
}

【问题讨论】:

  • 更新参考
  • @Sajeetharan 更新了几次。它正在更新。仍然出现错误
  • 您输入的参考名称是什么?
  • @Sajeetharan 默认名称 ServiceReference1
  • 你在 WCFtestclient 中试过了吗?

标签: c# asp.net web-services wcf proxy-classes


【解决方案1】:

你的错误中有提示:

类型名称'ServiceReference1'在类型中不存在 '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 以外的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2015-04-30
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多