【发布时间】:2014-02-03 06:39:15
【问题描述】:
我不是 WCF 方面的专家,我希望有人能指出为什么我非常基本的“hello world”WCF 服务在使用简单的交叉电缆在单独的联网计算机上进行测试时不起作用。但是,在 1 台 PC 上测试时,它可以完美运行。两台 PC 互相交谈没有问题,因为两台 PC 可以相互 ping 通,并且两台 PC 的 windows7 防火墙都已禁用。客户端和服务器应用程序均使用管理员权限运行。
服务器 PC 的 ip 为 10.0.0.25
。我还测试了使用 adsl 调制解调器/交换机将 PC 联网,但使用以太网和 wifi 连接也有相同的结果。但是,当我在公司的无线网络上尝试完全相同的场景时,它确实有效,这可能会提供线索。 在服务器应用程序运行的情况下,我从客户端 PC 打开 Web 浏览器并输入以下 URL。这表明服务正在运行。
http://10.0.0.25:1235/MySecondService
当我在 1 台 PC 上运行客户端和服务器测试相同的场景时,它可以工作。
错误:
在http://10.0.0.25:1235/MySecondService 没有可以接受消息的端点侦听。这通常是由不正确的地址或 SOAP 操作引起的。有关详细信息,请参阅 InnerException(如果存在)。
客户端程序.cs
class Program
{
static void Main(string[] args)
{
string serviceURL = "http://10.0.0.25:1235/MySecondService";
ChannelFactory<IMySecondService> channelFactory1 =
new ChannelFactory<IMySecondService>(
new BasicHttpBinding(),
new EndpointAddress(serviceURL));
IMySecondService mySecondService = channelFactory1.CreateChannel();
Console.WriteLine("This test console application is a client for the MyFirstService WCF service");
Console.WriteLine(string.Format("It tries to use the service at address: {0}", serviceURL));
Console.WriteLine();
Console.Write("Write text to get number of words: ");
string s = Console.ReadLine();
int numberWords = mySecondService.GetNumberWords(s);
Console.WriteLine(string.Format("Number of words: {0}", numberWords));
s = Console.ReadLine();
}
}
客户端 App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMySecondService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://10.0.0.25:1235/MySecondService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IMySecondService" contract="ServiceReference1.IMySecondService"
name="BasicHttpBinding_IMySecondService" />
</client>
</system.serviceModel>
</configuration>
服务器程序.cs
class Program
{
static void Main(string[] args)
{
ServiceHost serviceHost1 = new ServiceHost(typeof(MySecondService));
serviceHost1.Open();
Console.WriteLine("This console application hosts the MySecondService WCF service at address " + serviceHost1. BaseAddresses[0]. ToString());
Console.WriteLine("Press ENTER to finish hosting");
Console.ReadLine();
serviceHost1.Close();
}
}
服务器应用程序.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="SecondService.MySecondService"
behaviorConfiguration="">
<host>
<baseAddresses>
<add baseAddress="http://10.0.0.25:1235/MySecondService"/>
</baseAddresses>
</host>
<endpoint address=""
binding="basicHttpBinding"
contract="SecondService.IMySecondService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
【问题讨论】:
标签: wcf endpoint basichttpbinding