简体   繁体   English

WCF客户端无法连接到WCF服务器并出现错误

[英]WCF client unable to connect to WCF server with error

I am unable to connect my WCF client to my WCF server on a Localhost network. 我无法将WCF客户端连接到Localhost网络上的WCF服务器。 The error that I am getting is the following: 我收到的错误如下:

Error while connecting to the Localhost server: Could not find default endpoint element that references contract 'MyServiceReference.IMyService' in the ServiceModel client configuration section. 连接到Localhost服务器时出错:在ServiceModel客户端配置部分中找不到引用合同'MyServiceReference.IMyService'的默认终结点元素。 This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该协定匹配的端点元素。

The server connection is the following. 服务器连接如下。 Here is the code: 这是代码:

 _host = new ServiceHost(typeof(MyService));

 //Create Metadata exchange for the service
 ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();
 _host.Description.Behaviors.Add(mexBehavior);

 //Add service endpoints for the service and mex
 _host.AddServiceEndpoint(typeof(IMyService), new NetNamedPipeBinding(), "net.pipe://localhost/WFC_Server/MyService.svc");
 _host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), "net.pipe://localhost/WFC_Server/MyService.svc/mex");
 _host.Open();

I am not setting up my client to connect to the server properly. 我没有设置我的客户端以正确连接到服务器。 Does anybody know why? 有人知道为什么吗? Here is the code: 这是代码:

 var callback = new MyServiceCallback();
 var instanceContext = new InstanceContext(callback);
 var client = new MyServiceReference.MyServiceClient(instanceContext);
 client.Endpoint.Address = new EndpointAddress("net.pipe://localhost/WFC_Server/MyService.svc");
            client.OpenSession();

Does anybody know why my client does not connect to the server? 有人知道为什么我的客户端不连接服务器吗?

I have replicated the problem and fixed the issue. 我已经复制了问题并解决了问题。 1. Specify the endpoint address while creating the client. 1.在创建客户端时指定端点地址。

 var callback = new MyServiceCallback();
                var instanceContext = new InstanceContext(callback);
                EndpointAddress remoteAddress = new EndpointAddress("net.pipe://localhost/WFC_Server/MyService.svc");
                var client = new MyServiceClient(instanceContext,new NetNamedPipeBinding(),remoteAddress);
                client.Hello(); // call the function
  1. In the server, you can register like this: 在服务器中,您可以这样注册:

Code: 码:

            Uri address1 = new Uri("http://localhost/WFC_Server/");
            Uri address2 = new Uri("net.pipe://localhost/WFC_Server/");
            ServiceHost _host = new ServiceHost(typeof(MyService), address1,address2);

            //Create Metadata exchange for the service
            ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();
            mexBehavior.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(mexBehavior);


            //Add service endpoints for the service and mex
           _host.AddServiceEndpoint(typeof(IMyService), new NetNamedPipeBinding(), "MyService.svc");

            _host.AddServiceEndpoint(
              ServiceMetadataBehavior.MexContractName,
              MetadataExchangeBindings.CreateMexHttpBinding(),
              "mex"
            );
            _host.Open();

I have used the following interfaces as an example: 我以以下接口为例:

  class MyService : IMyService
    {
        public void Hello()
        {
         Console.WriteLine("Hello From Server..");
        }
    }

    [ServiceContract(CallbackContract = typeof(ICallbackService))]
    internal interface IMyService
   {
        [OperationContract]
        void Hello();
    }


    [ServiceContract]
    public interface ICallbackService
    {
        [OperationContract]
        void CallClient();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM