我在 Windows 服务中托管 WCF 服务时遇到一些问题。 我可以在 VS2008 中启动我的 WCF 服务并导航到我的 app.config 中的基地址
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="WCF.IndexerBehavior"
name="WCF.Indexer">
<endpoint address="" binding="wsHttpBinding" contract="WCF.IIndexer">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/WCFService/Action/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCF.IndexerBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我可以看到它工作正常,我看到页面说我创建了一个服务,并显示了如何使用它的代码示例。
现在我的下一步是创建一个 Windows 服务来托管我上面显示的 WCF。
我刚刚使用了 te windows 服务模板,它给了我一个 Program.cs 和 Service1.cs,我将其重命名为 WindowsServiceHost.cs。我在里面有:
private ServiceHost host;
public WindowsServiceHost()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
var serviceType = typeof(Indexer.WCF.Indexer);
host = new ServiceHost(serviceType);
host.Open();
}
catch (Exception ex)
{
}
}
protected override void OnStop()
{
if (host != null)
{
host.Close();
}
}
一切编译正常,我可以运行 InstallUtil(我定义了一个安装程序)。 该服务过去常常立即启动和停止,但禁用 Windows Defender 后就摆脱了这种情况。 现在服务启动(作为网络服务)并保持运行(我认为),但是当我导航到基地址时,我得到了未找到的页面。 另一件奇怪的事情是,当我尝试停止服务(仍显示为正在运行)时,我得到:
错误 1061:服务此时无法接受控制消息
我已经尝试了所有方法,但还是不知所措。
最佳答案
不能 100% 确定真正的原因是什么 - 只是为了确认,我们一直在 Windows 服务中自行托管 WCF 服务,它通常运行良好。
您可以尝试两点 - 只是为了了解行为和问题的潜在线索:
1) 我注意到您只使用服务类型打开了 ServiceHost - 这有效,但您可能仍想添加一个基址,甚至是对 new ServiceHost()
的调用- 像这样:
host = new ServiceHost(serviceType,
new Uri("http://localhost:8181/WCFService/Action/");
你能导航到那个地址并获取服务页面吗??
2) 我注意到的另一件事是您的服务似乎被称为 Indexer.WCF.Indexer
在打开主机之前在 typeof() 中指定,但在配置文件中,name=
在 <service>
上标签只是“WCF.Indexer”。
您能否尝试将该标签更改为:
<service behaviorConfiguration="WCF.IndexerBehavior"
name="Indexer.WCF.Indexer">
这有帮助吗?在浏览器中导航到服务页面时,您现在可以看到它吗?
马克
关于c# - 在 Windows 服务中托管 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1182903/