WCF客户端无法连接到错误

时间:2016-05-05 21:22:17

标签: c# wcf

我无法将我的WCF客户端连接到Localhost网络上的WCF服务器。我得到的错误如下:

  

连接到Localhost服务器时出错:找不到默认值        引用合同的端点元素' MyServiceReference.IMyService'        在ServiceModel客户端配置部分中。这可能是因为        找不到您的应用程序的配置文件,或者没有找到        匹配此合同的端点元素可以在客户端元素中找到。

服务器连接如下。这是代码:

 _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();

我没有设置我的客户端以正确连接到服务器。有人知道为什么吗?这是代码:

 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();

有人知道为什么我的客户端没有连接到服务器吗?

1 个答案:

答案 0 :(得分:0)

我已经复制了问题并修复了问题。 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. 在服务器中,您可以这样注册:
  2. 代码:

                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();
    

    我使用以下接口作为示例:

      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();
        }