【发布时间】:2018-07-11 02:03:20
【问题描述】:
我试图创建基本的 WCF 服务并将其托管在控制台应用程序上。
这是我的 WCF 项目代码。
ISampleService.cs
using System.ServiceModel;
namespace MultipleSeviceContractAppl
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISampleService" in both code and config file together.
[ServiceContract]
public interface ISampleService
{
[OperationContract]
string DoWork();
}
}
SampleService.cs
namespace MultipleSeviceContractAppl
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SampleService" in both code and config file together.
public class SampleService : ISampleService
{
public string DoWork()
{
return "Message from WCFservice";
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="MultipleSeviceContractAppl.SampleService" behaviorConfiguration="mexBehaviour">
<endpoint address="SampleService" binding="netTcpBinding" contract="MultipleSeviceContractAppl.ISampleService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/"/> <!--For metadata exchange-->
<add baseAddress="net.tcp://localhost:8737/" /> <!--Endpoint, netTCP binding, For data exchange-->
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
控制台应用程序上的 WCF 托管 - Program.cs
using System;
using System.ServiceModel;
namespace ConsumeWCFApplicationAppl
{
class Program
{
static void Main()
{
using (ServiceHost host = new ServiceHost(typeof(MultipleSeviceContractAppl.SampleService)))
{
host.Open();
Console.WriteLine("Host started @" + DateTime.Now.ToString());
Console.ReadKey();
}
}
}
}
在 host.Open(); 行的控制台应用程序中,引发了以下异常。
“System.InvalidOperationException”类型的未处理异常 发生在 System.ServiceModel.dll 附加信息:服务 'MultipleServiceContractAppl.SampleService' 的应用程序为零 (非基础设施)端点。这可能是因为没有配置 为您的应用程序找到了文件,或者因为没有服务元素 可以在配置文件中找到匹配的服务名称,或者 因为在服务元素中没有定义端点。帮我 找出我的错误。谢谢
【问题讨论】:
-
尝试在
ServiceHost构造函数中指定基地址:ServiceHost host = new ServiceHost(typeof(MultipleServiceContractApp1.SapmleService), new Uri[] { "http://localhost:8733/", "net.tcp://localhost:8737/" });此外,您可能需要使用提升的权限运行才能注册 net.tcp 地址。 -
添加上面的行会引发新的错误严重性无法将类型'string'隐式转换为'System.Uri'
-
您的应用程序的问题是,它无法找到您的 app.config 文件。删除并重新添加它或创建一个新的解决方案,检测 app.config 有问题。跨度>
-
@LearnAvid - 试试
ServiceHost host = new ServiceHost(typeof(MultipleServiceContractApp1.SapmleService), new Uri[] { new Uri("http://localhost:8733/"), new Uri("net.tcp://localhost:8737/") });