1、"没有终结点在侦听可以接受消息的 http://localhost:8084/Service1.svc。这通常是由于不正确的地址或者 SOAP 操作导致的。如果存在此情况,请参见 InnerException 以了解详细信息"

一般是地址写错了,此处的重点在InnerException,发现是连不上"127.0.0.1:8084",所以我的WCF服务端有问题

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

 

WCF报错 随笔 第1张

 

正确写法如下(控制台作为宿主):

            //创建宿主的基地址
            Uri baseAddress = new Uri("http://localhost:8084/Service1.svc");         

            //创建宿主
            using (ServiceHost host = new ServiceHost(typeof(Service1), baseAddress))
            {                
                //向宿主中添加终结点
                //host.AddServiceEndpoint(typeof(IService1), new WSHttpBinding() ,"");
                System.ServiceModel.Channels.Binding httpbinding = new BasicHttpBinding();

                host.AddServiceEndpoint(typeof(IService1), httpbinding, "");
                
                //将HttpGetEnabled属性设置为true
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;               

                //将行为添加到Behaviors中
                host.Description.Behaviors.Add(smb);
             
                //打开宿主
                host.Open();
                Console.WriteLine("WCF中的HTTP监听已启动....");
                Console.ReadLine();                
                //host.Close();
            }

 

 

 

2、"服务 http://localhost:8082/Service1.svc 不支持内容类型 text/xml; charset=utf-8。客户端和服务绑定可能不匹配。"

 

WCF报错 随笔 第2张

 

      根据上面那句搜的解决方法都不行,重点还是在InnerException里,说希望类型是'text/xml; charset=utf-8',此时异常的类型是'application/soap+xml; charset=utf-8'

 远程服务器返回错误: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.。

//写WSHttpBinding报错
host.AddServiceEndpoint(typeof(IService1), new WSHttpBinding() ,"");


//当前场景正确调用方式
System.ServiceModel.Channels.Binding basicHttpBinding = new BasicHttpBinding();
host.AddServiceEndpoint(typeof(IService1), basicHttpBinding , "");

 

WCF报错 随笔 第3张

 

 

参考:

解决"415 Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'"

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄