我在控制台应用程序中托管了一个基本的WCF服务,我无法从另一个控制台应用程序中使用它。
我正在学习WCF所以我正努力让它跟着一本书一起工作。
当我尝试使用该服务时,我收到以下错误
Unhandled Exception: System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http://localhost:6599/HostCmdLineApp/HelloWorldService.svc/HelloWorldService that could accept the message. This is often caused by an incorrect address or SOAP action...
但服务运行良好:
我正在使用VS2012。
解决方案的一般结构是:
<system.serviceModel>
<services>
<service name="MyWCFServices.HelloWorldService"
behaviorConfiguration="HelloWorldServiceBehavior">
<endpoint address="HelloWorldService"
binding="basicHttpBinding"
contract="MyWCFServices.IHelloWorldService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="HelloWorldServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
从HostCmdLineApp发布服务的Main方法是:
static void Main(string[] args) {
var baseAddress = new Uri("http://localhost:6599/HostCmdLineApp/HelloWorldService.svc");
using (var host = new ServiceHost(typeof(MyWCFServices.HelloWorldService), baseAddress)) {
host.Open();
Console.WriteLine("HelloWorldService is ready to be used. ");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.ReadKey();
host.Close();
}
}
HelloWorldClient的App.config文件(服务使用者)是由SvcUtil工具生成的:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IHelloWorldService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:6599/HostCmdLineApp/HelloWorldService.svc
HelloWorldService"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IHelloWorldService"
contract="IHelloWorldService" name="BasicHttpBinding_IHelloWorldService" />
</client>
</system.serviceModel>
使用该服务的Main方法是:
static void Main(string[] args) {
var client = new HelloWorldServiceClient();
Console.WriteLine(client.GetMessage("Rafael Soteldo"));
}
我以管理员身份运行VS.
答案 0 :(得分:1)
您的客户地址不正确。 svc之后有一个正斜杠。
address="http://localhost:6599/HostCmdLineApp/HelloWorldService.svc/HelloWorldService"
如果您将地址留空在服务主机上,那么最后您将不需要“/ HelloWorldService”。