【问题标题】:WCF configuration works on localhost, not on shared websiteWCF 配置在本地主机上工作,而不是在共享网站上
【发布时间】:2019-09-22 13:12:30
【问题描述】:

我有一个旧的 ASP.NET 网站,它有一个用于返回 JSON 有效负载的 WCF 服务。它在 localhost 中运行良好,但是当我尝试将其部署到 共享托管网站 时,出现异常:

IIS 指定了身份验证方案“IntegratedWindowsAuthentication, Basic, Anonymous”,但绑定只支持指定一个身份验证方案。有效的身份验证方案是 Digest、Negotiate、NTLM、Basic 或 Anonymous。更改 IIS 设置,以便只使用一个身份验证方案。

我无法进行此更改,因为它是共享环境。

关于服务,我需要在Web.config 中更改我的配置吗?这是我得到的:

<system.serviceModel>
  <services>
    <service name="BoggleService">
      <endpoint binding="webHttpBinding" contract="Solver" behaviorConfiguration="webHttp"/>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webHttp">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

我也有这个配置,但是被注释掉了(就像我说的,很久以前的一个项目,试图复活它):

<serviceHostingEnvironment>
  <baseAddressPrefixFilters>
    <add prefix="http://www.example.com/"/>
  </baseAddressPrefixFilters>
</serviceHostingEnvironment>

但如果我取消注释并包含它,我会在 localhost 和共享托管网站中收到以下异常:

索引超出范围。必须为非负数且小于集合的大小。

这是完整的堆栈跟踪:

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) +64
   System.ThrowHelper.ThrowArgumentOutOfRangeException() +15
   System.Collections.Generic.List`1.get_Item(Int32 index) +7515544
   System.ServiceModel.Web.WebServiceHost.AddAutomaticWebHttpBindingEndpoints(ServiceHost host, IDictionary`2 implementedContracts, String multipleContractsErrorMessage) +82
   System.ServiceModel.Web.WebServiceHost.OnOpening() +203
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +229
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +121
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +479

[ServiceActivationException: The service '/Solver.svc' cannot be activated due to an exception during compilation.  The exception message is: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index.]
   System.ServiceModel.AsyncResult.End(IAsyncResult result) +11667006
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +194
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, Boolean flowContext) +176
   System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +275
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

这来自 WCF 的内部 - 它不是来自我的代码(堆栈跟踪显示它永远不会到达我的代码,如果我运行调试器并在我的代码中设置断点,它永远不会到达) .

有趣的是,如果我将&lt;baseAddressPrefixFilters&gt; 设置为http://localhost,它可以在本地主机上运行!但不是在生产中。

谢谢!

【问题讨论】:

  • 该异常似乎与 WCF 无关。您确定您的 web.config 中没有其他内容吗?
  • 我使用 Stack Trace 更新了我的帖子,并添加了以下细节:如果我将 &lt;baseAddressPrefixFilters&gt; 设置为 http://localhost,它可以在 localhost 上运行!但不是在生产中。这可能与 .NET Framework 版本的更改有关吗?很多个月前,我在 .NET 2.0 上启动了这个项目,现在它托管在 4.x 上。配置语法有变化吗?

标签: asp.net wcf


【解决方案1】:

我认为问题可以简化,因为它使用 WebHttpBinding 来托管 Restful 样式服务。
在 .net4.5 中,我们可以使用以下简化配置来托管 Restful 风格的服务。

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="webHttpBinding" scheme="http" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

默认情况下,webhttpbinding 使用 WebHttpSecurityMode.None。所以不太可能发生上面的身份验证架构错误。如有必要,不要忘记在 IIS 身份验证模块中启用身份验证模式。
尝试用我的配置替换您的配置,并在 IIS 站点绑定模块中提供 http 绑定。
如果有什么我可以帮忙的,请随时告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2011-07-25
    相关资源
    最近更新 更多