WCF控制台客户端提示错误:没有终结点在侦听可以接受消息的

kenriy 2012-09-15 09:18:59
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();
}

}
}
...全文
301 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧