WCF控制台客户端提示错误:没有终结点在侦听可以接受消息的
WCF控制台客户端提示错误:
没有终结点在侦听可以接受消息的 http://localhost:9090/WcfService。这通常是由于不正确的地址或者 SOAP 操作导致的。如果存在此情况,请参见 InnerException 以了解详细信息。
=============================================================
WCF服务:
IWcfService
namespace MyWcfService
{
[ServiceContract]
public interface IWcfService
{
[OperationContract]
string HelloWorld();
[OperationContract]
string IMethod(string value);
}
}
WcfService文件:
namespace MyWcfService
{ public class WcfService : IWcfService
{
public string HelloWorld()
{
return "HelloWorld_wcf";
}
public string IMethod(string value)
{
return "这是WCF端:" + value;
}
}
}
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<!--添加服务-->
<service name="MyWcfService.WcfService" behaviorConfiguration="MyWcfService.WcfServiceBehavior">
<!--name 必须与代码中的host实例初始化的服务一样
behaviorConfiguration 行为配置 -->
<host>
<baseAddresses>
<!--添加调用服务地址-->
<add baseAddress="http://localhost:9090/WcfService"/>
</baseAddresses>
</host>
<!--添加契约接口 contract="WcfService.IService1" WcfService.IService1为契约接口 binding="wsHttpBinding" wsHttpBinding为通过Http调用-->
<endpoint address="http://localhost:9090/WcfService" binding="wsHttpBinding" contract="WcfService.IWcfService">
<!--
部署时,应删除或替换下列标识元素,以反映
在其下运行部署服务的标识。删除之后,WCF 将
自动推导相应标识。
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<!--定义CalculatorServiceBehavior的行为-->
<behaviors>
<serviceBehaviors>
<behavior name="MyWcfService.WcfServiceBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
服务器端:
namespace MyServiceHost
{
class Program
{
static void Main(string[] args)
{
//由于MyServiceHost实现了IDisposable 接口,所以使用using
using (ServiceHost host = new ServiceHost(typeof(WcfService), new Uri("http://localhost:9090/WcfService") ))
//using (ServiceHost host = new ServiceHost(typeof(WcfService)))
{
host.AddServiceEndpoint(typeof(IWcfService), new BasicHttpBinding(), "WcfService"); //这里typeof(IWcfService)是接口
host.Open();
Console.WriteLine("Hello Service Is Running...");
Console.ReadLine();
host.Close();
}
}
}
}
客户端:
namespace MyServiceClient
{
[ServiceContract]
public interface IWcfService
{
[OperationContract]
string HelloWorld();
[OperationContract]
string IMethod(string value);
}
class ClientProgram
{
static void Main(string[] args)
{
EndpointAddress epAddr = new EndpointAddress("http://localhost:9090/WcfService"); //--http://localhost:9090/MyWcfService/WcfService
IWcfService proxy = ChannelFactory<IWcfService>.CreateChannel(new BasicHttpBinding(), epAddr);
Console.WriteLine(proxy.HelloWorld());
Console.WriteLine("Client Is Running...");
Console.ReadLine();
}
}
}