【问题标题】:Can not call WCF Service hosted in a Managed Windows Service无法调用托管在托管 Windows 服务中的 WCF 服务
【发布时间】:2013-10-03 19:26:09
【问题描述】:

所以我有一些关于托管在托管 Windows 服务中的 WCF 服务的问题。

基本上我所做的如下:

我使用一个简单的测试创建了一个 WCF 服务库(WCF 服务模板),就像这样

[ServiceContract]
public interface IExample
{
    [OperationContract]
    string HelloWorld();
}

public class Example : IExample
{
    public string HelloWorld()
    {
        return "HelloWorld";
    }
}

我还创建了一个对应的app.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;
       }
   }
}

我还为服务添加了所需的安装程序并将帐户设置为 localSystem。

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

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

所以任何建议都会很好。

编辑:

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

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

应该是这样的:

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

现在我得到了无法注册url的错误,

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

我已经尝试过HERE 中描述的工具,但是并没有解决错误。由于报错,仍然可以启动Service。

编辑:

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

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

最终编辑:

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

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

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

【问题讨论】:

  • 两点:(1) &lt;service name="...." &gt; 属性必须完全匹配实现的类(包括其命名空间) b> 服务,(2) 该类也需要在new ServiceHost(typeof(....)) 中使用
  • 是的,我已经这样做了,它确实修复了我之前遇到的错误,现在剩下的似乎是关于我的用户帐户的一些权利问题。
  • 好的解决了用户帐户的问题(请参阅 las 编辑),但仍然收到 400 错误:/
  • 终于成功了,基地址后面少了一个/...

标签: c# .net wcf web-services service


【解决方案1】:

您的合同应该是 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 事件日志(只需在开始菜单中键入“事件查看器”),看看是否有任何问题。

【讨论】:

  • 是的,我看到了,只是我发布的一个旧配置文件。新的有正确的链接抱歉。但问题仍然存在。
  • 好吧,看来您指出的第二个错误确实是一个错误。我修复了它,如果我现在启动服务,它说它启动了服务然后停止了它。 OnStart 中可能有异常吗?
  • 您的事件日志是怎么说的?
  • 是的,它说我找不到端点,我犯了一个错误,只是将配置放在 lib 中,因此服务的实际配置没有任何端点。
  • 好吧奇怪我现在把config内容放到服务的app.config里面了,这个内容也在peripherie.winservice.exe.config里面。但是日志仍然说有异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
相关资源
最近更新 更多