【问题标题】:No endpoints found when adding a Custom ServiceHostFactory添加自定义 ServiceHostFactory 时未找到端点
【发布时间】:2011-12-23 04:22:43
【问题描述】:

我创建了一个 wcf 应用程序。我没有改变任何东西。使用了 Service1.GetData(int)。它工作正常。我可以在浏览器中点击 wsdl 和一切。然后我创建了一个自定义服务主机工厂,它只返回一个新的服务主机并且服务永远不会出现。我无法再在浏览器中访问 wsdl。我尝试添加一个自定义 ServiceHost,以便进行一些调试,但似乎没有找到端点(即使显式调用 AddDefaultEndpoints()。即使我显式将端点添加到 web.config 也是如此。

有没有人知道问题可能是什么?

如果有人愿意看一下,我将代码放在 github 上:https://github.com/devlife/Sandbox/tree/master/WcfService1

【问题讨论】:

  • 谢谢你把代码放上来,我去看看。
  • 当您浏览到您的 wsdl 并且它不起作用时,您得到的实际结果是什么?例如,您得到的是 404 还是 500,还是一无所获?如果您的浏览器没有,Fiddler 会告诉您。

标签: wcf wcf-hosting


【解决方案1】:

您为什么使用 ServiceHostFactory? 你打算使用 AppFabric/IIS 吗?还是自托管服务?

我认为您需要添加一个 MEX 端点。

    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />

【讨论】:

  • 我认为这与这个问题没有任何关系。我打 wsdl 的唯一原因是启动服务。我遇到的问题是没有找到端点。在 CustomServiceHost 中调试,看看我在说什么。
【解决方案2】:

这是我在我正在处理的项目中定义 CustomHost 的方式,

<%@ ServiceHost Language="C#" Debug="true" Service="Servicename.Servicename" CodeBehind="Service1.svc.cs" Factory="WcfService1.CustomServiceHostFactory"%>

还有这个,

public class CustomServiceHostFactory : ServiceHostFactory
{
    public CustomServiceHostFactory()
    {

    }

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return new CustomServiceHost(serviceType, baseAddresses);
    }
}

public class CustomServiceHost : ServiceHost
{
    public CustomServiceHost()
    {
    }

    public CustomServiceHost(Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    {
    }

    protected override void OnOpening()
    {
        base.OnOpening();
    }

    protected override void OnClosing()
    {
        base.OnClosing();
    }

    protected override void ApplyConfiguration()
    {
        base.ApplyConfiguration();
    }
}

请注意,CustomServiceHost 看起来很裸露,但这是因为我的解决方案在此 CustomServiceHost 中有很多日志记录和配置,我已将其删除并且不合适。

我可以看到的另一个区别是我的 CustomServiceHost 没有添加端点。端点在配置文件中是这样定义的,

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Servicename.Servicename" behaviorConfiguration="ServiceBehavior">
    <endpoint address="http://*******.svc" binding="wsHttpBinding" contract="Namespace.IContract" bindingConfiguration="BindingConfig">
    </endpoint>
  </service>
</services>
<bindings>
  <wsHttpBinding>
    <binding name="BindingConfig" maxReceivedMessageSize="9291456">
      <security mode="None">
      </security>
      <readerQuotas maxArrayLength="6291456" />
    </binding>
  </wsHttpBinding>
</bindings>

【讨论】:

  • 很抱歉。我在使用工厂和不使用工厂之间来回走动。当我不使用工厂时,我可以联系localhost/WcfService1/Service1.svc?wsdl。但是,当我使用工厂时,我无法访问 wsdl。
  • 好的,那么当您在 ApplyConfiguration 代码中没有任何内容并将您的配置放在 web.config 文件中时会发生什么?更像我上面展示的。
  • 当我注释掉 ApplyConfiguration 方法并放入您指定的配置时,我无法访问 wsdl。即使我添加了一个 mex 端点。它说元数据已关闭。但是,如果删除自定义服务主机工厂,那么它会按预期工作。如果你想看一下,我更新了 github 中的代码。我对此感到非常困惑。
  • 我很困惑。我下载了你的新版本,它对我来说很好用。当您说 wsdl 时,您的意思是这个地址localhost:39506/Service1.svc?wsdl,它说“您已经创建了一个服务”。这就是我得到的。
  • 我可以看到一些不同之处,您正在使用 IIS 来调试您的服务,具体取决于您的项目设置。尝试使用“Visual Studio 开发服务器”,看看是否有效?你也可以试试 IIS express。如果您也使用不同的浏览器浏览到您的 wsdl 会怎样。您还可以使用 fiddler 查看浏览到 svc 时会发生什么,它会告诉您来自 Web 服务器的响应是什么。让我们知道您得到了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
相关资源
最近更新 更多