类型或命名空间" MyServiceClient"无法找到

时间:2015-09-29 17:34:25

标签: c# wcf namespaces visual-studio-2015 using

我刚刚完成this wcf duplex tutorial,但在查看我的代码时遇到了这个错误:

Severity    Code    Description   Line
Error   CS0246  The type or namespace name 'MyServiceClient' could not be found (are you missing a using directive or an assembly reference?) ClientSide 15

我的使用似乎都井井有条,而且我已经多次用Google搜索这个错误而没有运气。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace ClientSide
{
    class Program
    {
        static void Main(string[] args)
        {
            InstanceContext callback = new InstanceContext(new ClientCallback());
            MyServiceClient client = new MyServiceClient(callback);
            client.Open();
            client.Register();
            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
            client.Close();
        }
    }
}

MyServiceClient是类型还是命名空间?

我非常感谢任何帮助,我使用的是Visual Studio 2015。

1 个答案:

答案 0 :(得分:0)

在这种情况下,MyServiceClient应该是您在执行"添加服务参考"时生成的客户端类的名称。

要验证生成的类的名称,请在Windows资源管理器中导航到包含服务引用的目录(通常为<ProjectName>\Service References\<ServiceReferenceName>,然后打开文件Reference.cs

在Reference.cs中,查找一个继承自System.ServiceModel.ClientBase的类。派生类是您应该用来与Web服务交互的服务客户端类。

因此,例如,如果Reference.cs包含

public partial class BlahServiceSoapClient : System.ServiceModel.ClientBase<global::MyProject.MyService.BlahServiceSoap>, global::MyProject.MyService.BlahServiceSoap

注意,根据引用,它可能是您在Reference.cs文件中看到的System.ServiceModel.ClientBase的不同子类。由于此问题特别针对双工客户端,因此您会看到System.ServiceModel.DuplexClientBase

然后你的服务类将是BlahServiceSoapClient,所以你会做

BlahServiceSoapClient client = new BlahServiceSoapClient(callback);