【问题标题】:WCF Application returning 400 Bad Request when hosted on IIS7WCF 应用程序在 IIS7 上托管时返回 400 Bad Request
【发布时间】:2010-07-20 10:08:44
【问题描述】:

我使用 C# 和 Visual Studio 2008 编写了一个 WCF Web 服务。

当我使用内置的开发 Web 服务器运行它时,我可以通过浏览到服务合同的 [WebGet(UriTemplate = "..")] 属性中指定的 URL 来查看各种方法的结果.

具体来说,我有一个匹配“/” URL 的方法,并返回一个纯 HTML 文件。

但是,当我部署到在 Windows Server 2008 上运行的 IIS 时,此方法会返回标准的“您已创建服务”页面。

如果我尝试使用我指定的 URI 模板执行任何其他方法,例如:

api.hostname.com/Service.svc/method/param

服务器返回 400 Bad Request。

我的 live web.config 的 system.serviceModel 部分如下所示:

 <system.serviceModel>
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
   <services>
       <service name="RootNamespace.Api.EventService"
           behaviorConfiguration="RootNamespace.Api.EventServiceBehaviour" >
        <endpoint address="" 
                  binding="wsHttpBinding" 
                  contract="RootNamespace.Api.IEventService" />
        <endpoint address="mex" 
                  binding="mexHttpBinding" 
                  contract="IMetadataExchange" />
        <host>
          <baseAddresses>
           <add baseAddress="http://api.hostname.com" />
         </baseAddresses>
       </host>
     </service>
   </services>
   <behaviors>
     <serviceBehaviors>
       <behavior name="RootNamespace.Api.EventServiceBehaviour">
         <serviceMetadata httpGetEnabled="true" />
         <serviceDebug includeExceptionDetailInFaults="true" />
       </behavior>
     </serviceBehaviors>
   </behaviors>
</system.serviceModel>

我想知道请求是否没有到达 WCF 服务,并且被 IIS 中的另一个处理程序阻止,但它为根 URL 返回不同内容的事实让我怀疑我正在做其他事情这里错了。

更新...

我已经设法通过修改后的 web.config 取得了一些进展:

<behaviors>
  <serviceBehaviors>
    <behavior name="RootNamespace.Api.EventServiceBehavior">
      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="RootNamespace.Api.EventServiceBehavior"
           name="RootNamespace.Api.EventService">
    <endpoint address="" 
              binding="webHttpBinding" 
              behaviorConfiguration="WebBehavior"                     
              contract="RootNamespace.Api.IEventService">
    </endpoint>
  </service>
</services>

这显然与我的第一次尝试有很大不同,但目标保持不变 - 我想使用 Web 浏览器导航到我的服务合同 (IEventService) 的 WebGet 属性中定义的 URL。

使用此 Web 配置,我现在收到我定义的方法的 403 禁止错误,而不是收到 400 错误。对于不存在的方法,我正确地收到“找不到端点”消息。这让我觉得我的东西运行正常,但只是在应用层遇到了某种身份验证问题。

我也觉得我把这件事弄得太复杂了。毕竟,这无需在 Visual Studio Web 服务器中进行任何类型的配置即可工作。

【问题讨论】:

  • IIS网站的实际地址和你配置的baseAddress是否有冲突?

标签: c# iis wcf


【解决方案1】:

我认为你的问题是这样的:

    1234563这是默认的 WCF 行为。
  • 您有一个“存在”在同一地址的介绍性 html 页面 --> 您有冲突

你可以做两件事,真的:

  • 在您的服务元数据上禁用httpGetEnabled

或:

  • 将您的介绍页面移动到另一个地址,例如http://api.seetickets.com/intro 什么的(通过在服务方法上设置UriTemplate="....."

【讨论】:

  • 感谢您的意见,马克。我已经用修改后的信息稍微更新了我的问题。我曾尝试禁用 httpGetEnabled 但不幸的是这似乎不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
相关资源
最近更新 更多