Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error accessing a WCF service from a client

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();
        }
    }
}
like image 971
Mangrio Avatar asked Dec 29 '25 22:12

Mangrio


1 Answers

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:

  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.

like image 162
DavidRR Avatar answered Jan 01 '26 11:01

DavidRR