【发布时间】:2016-05-10 22:16:25
【问题描述】:
我想创建 WCF 服务并通过 Windows 服务托管它。
我已经按照教程https://msdn.microsoft.com/en-us/library/ff649818.aspx创建了所有东西
这是我的WCF services library App.config。我已将其更改为使用 TCP 协议
<system.serviceModel>
<services>
<service name="WCFServiceLibrary.Service1">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="WCFServiceLibrary.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:3000/Service1" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
然后我将 App.config 复制到 WindowsService 项目。 我还添加了 WindowsService 的安装程序,然后更改了属性:
StartType - Automatic
Account - NetWorkService
然后我建立了这个项目并
使用Installutil WindowsService.exe成功安装它
但是,当我尝试在我的 WPFClientProject
中添加服务 (net.tcp://localhost:3000/Service1) 引用时它出现了:
URI 前缀无法识别。元数据包含一个引用 无法解决:“net.tcp://localhost:3000/Service1”。不能 连接到 net.tcp://localhost:3000/Service1。连接尝试 持续了 00:00:02.0641450 的时间跨度。 TCP错误代码10061:
看来我没有主机。 我做错了什么?
Windows 服务代码
public partial class Service1: ServiceBase
{
internal static ServiceHost myServiceHost = null;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(Service1));
myServiceHost.Open();
}
protected override void OnStop()
{
if (myServiceHost != null)
{
myServiceHost.Close();
myServiceHost = null;
}
}
}
【问题讨论】:
-
当您尝试添加服务引用时,Windows 服务是否正在运行?您是否已验证在端口 3000 上有监听?能否贴出启动服务主机的windows服务代码?
-
我的 WCF 客户端放置在另一个解决方案中。也许这是那个问题的原因?我将添加 Windows 服务代码。
-
我个人的偏好是从代码中配置WCF,我在输入和执行时也感觉更好的控制。
标签: c# .net wcf windows-services