本文介绍了错误从客户端访问WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的WCF服务,增加了两个整数。该服务主机开始完美。但在客户端,我得到以下编译错误 Reference.cs

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:

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:

  1. Dropping the leading I from the name of the contract interface, and
  2. Appending Client.

From the code you posted, it appears your contract interface is named IWcfService. So from that, Visual Studio creates the name WcfServiceClient 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 than WcfServiceClient.

这篇关于错误从客户端访问WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 04:25
查看更多