不能调用托管 Windows 服务中承载的 WCF 服务

本文关键字:服务 WCF 不能 Windows 调用 | 更新日期: 2023-09-27 18:34:00

所以我对托管Windows服务中承载的WCF服务有一些问题。

基本上我所做的是以下几点:

我使用一个简单的测试创建了一个 WCF 服务库(WCF 服务模板(,如下所示

[ServiceContract]
public interface IExample
{
    [OperationContract]
    string HelloWorld();
}
public class Example : IExample
{
    public string HelloWorld()
    {
        return "HelloWorld";
    }
}

我还创建了一个相应的应用程序.config,它是这个

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <!-- This section is optional with the new configuration model introduced in .NET Framework 4. -->
            <service name="Peripherie.WCFService" behaviorConfiguration="ServiceBehavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8067/PeripherieService"/>
                    </baseAddresses>
                </host>
                <endpoint address="" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IExample" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

之后,我添加了一个 Win Service 项目(再次使用 Win Service 模板(,该项目引用了上述库和其他所需的库。

在服务类中,我做了创建服务主机的基本工作

public partial class Service : ServiceBase
{
    public ServiceHost serviceHost = null;
    public Service()
    {
       InitializeComponent();
    }
   protected override void OnStart(string[] args)
   {
       if(serviceHost!=null)
        serviceHost.Close();
       serviceHost = new ServiceHost(typeof(Service));
       serviceHost.Open();
   }
   protected override void OnStop()
   {
       if(serviceHost!=null)
       {
        serviceHost.Close();
        serviceHost = null;
       }
   }
}

我还为服务添加了所需的安装程序,并将帐户设置为本地系统。

整个项目编译得很好,我也能够安装该服务(使用 installutil 方法(并启动它。但是,当我尝试在浏览器中打开服务时,我收到无法加载端的错误,我也无法使用 WCF 测试客户端,因为它告诉我没有要检索的元数据。

我真的不明白为什么整个想法不起作用,因为似乎一切都设置正确。

所以任何建议都会很好。

编辑:

修复了SouthShoreAK指出的错误后,我还在配置中发现了一个错误,其中:

<service name="Peripherie.WCFService" behaviorConfiguration="ServiceBehavior">

应该是这样的:

<service name="Peripherie.WCFService.Services.Example" behaviorConfiguration="ServiceBehavior">

现在我收到无法注册网址的错误,

System.ServiceModel.AddressAccessDeniedException: HTTP konnte URL "http://+:8067/PeripherieService/" nicht registrieren. Der Prozess weist keine Zugriffsrechte für diesen Namespace auf 

我已经尝试了此处描述的工具,但是没有解决错误。由于错误,仍然可以启动服务。

编辑:

好的,这个问题也解决了,我将服务进程安装程序仍设置为网络服务。将其设置为本地系统后,我现在可以启动该服务。

但是通过IE调用URL时,我现在仍然收到错误400。

最终编辑:

好的,现在它可以工作了,最后一个错误是因为基址末尾缺少/。所以应该是

<add baseAddress="http://localhost:8067/PeripherieService/"/>

由于SouthShoreAK几乎指出了我在配置中犯的错误,我将接受他的回答,因为它让我走上了正轨。

不能调用托管 Windows 服务中承载的 WCF 服务

你的合同应该是IExample,而不是IMain

 <endpoint name="ServiceHttpEndpoint" address="http://localhost:8067/PeripherieService" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IMain" />

应改为:

 <endpoint name="ServiceHttpEndpoint" address="http://localhost:8067/PeripherieService" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IExample" />

另外,这个:

serviceHost = new ServiceHost(typeof(Service));

应该是这样的:

serviceHost = new ServiceHost(typeof(Example));

你正在尝试在服务主机中注册 Windows 服务并实例化。您应该注册 WCF 服务。

有时我发现即使服务主机遇到错误,您的 Windows 服务也会启动并运行。您可能需要检查Windows事件日志(只需在开始菜单中键入"事件查看器"(以查看是否有任何问题。