【发布时间】:2013-08-30 23:02:13
【问题描述】:
我的解决方案中有 3 个项目。
- Test Client => 用于通过 tcp ip 添加引用和访问
- WcfServiceLibruary1 => 用于执行我的方法
- WindowsService1 => 用于作为 Windows 服务安装和运行(帐户:网络服务,启动类型:自动)
我在 msdn 示例中使用了所有相同的代码
http://msdn.microsoft.com/en-us/library/ff649818.aspx
我使用具有 2 个方法的 wcf 服务。我想在托管的 windows 服务中使用这个 wcf 服务。我在我的解决方案中添加了一个 windows 服务并设置了引用的东西。
我在我的 wcf - app.config 上使用此地址引用:
net.tcp://localhost:2023/Service1
现在的问题是:
我成功添加了对我的测试客户端项目的引用
net.tcp://localhost:2023/Service1:
但是这个参考地址不能作为windows服务安装!!!
当我将它安装为 Windows 服务时,我无法访问此地址,
我得到了这个错误:No connection could be made because the target machine actively refused it
WcfServiceLibruary app.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.Service1">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract="WcfServiceLibrary1.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:2023/Service1"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
我的 Windows 服务:
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(Service1));
myServiceHost.Open();
}
当我在 visualstudio 服务主机上启动时一切正常:
【问题讨论】:
-
我不太明白你的问题是什么。你是说
app.config文件一旦部署就没有配置设置吗?请注意,配置部分需要是 Windows 服务app.config文件的一部分,而不是服务库。 -
我将 wcf app.config 复制到 windows 服务项目。我的 wcf 主机服务无法作为 windows 服务安装到计算机。
-
当你说它不起作用时,你是什么意思?你有错误吗?服务没有启动吗?你不能连接到服务吗?如果服务正在运行,可以在DOS提示符下输入
netstat,查看2023端口是否建立。 -
当我将它安装为 Windows 服务时,我使用 telnet localhost 2023 检查它
-
我得到了这个错误:无法建立连接,因为目标机器主动拒绝了它
标签: c# asp.net wcf windows-services