简体   繁体   English

WCF应用程序在IIS7上托管时返回400错误请求

[英]WCF Application returning 400 Bad Request when hosted on IIS7

I have written a WCF web service using C# and Visual Studio 2008. 我已经使用C#和Visual Studio 2008编写了WCF Web服务。

When I run it using the built in development web server, I am able to view the results of the various methods by browsing to the URL specified in the [WebGet(UriTemplate = "..")] attribute of the service contract. 当使用内置的开发Web服务器运行它时,通过浏览到服务合同的[WebGet(UriTemplate =“ ..”)]属性中指定的URL,可以查看各种方法的结果。

Specifically, I have a method that matches the "/" URL, and returns a plain HTML file. 具体来说,我有一个匹配“ /” URL的方法,并返回一个纯HTML文件。

However when I deploy to IIS, running on Windows Server 2008, this method returns the standard "You have created a service" page. 但是,当我部署到运行在Windows Server 2008上的IIS时,此方法将返回标准的“您已创建服务”页面。

If I try to execute any of my other methods, using the URI templates I specified, for example: 如果尝试使用我指定的URI模板执行其他方法,例如:

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

The server returns 400 Bad Request. 服务器返回400错误请求。

The system.serviceModel section of my live web.config looks like this: 我的实时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>

I wondered if the request wasn't getting as far as the WCF service, and was being blocked by another handler in IIS but the fact that it's returning different content for the root URL leads me to suspect I'm doing something else wrong here. 我想知道请求是否没有到达WCF服务,并被IIS中的另一个处理程序阻止,但是事实是它为根URL返回了不同的内容,这使我怀疑我在这里做错了什么。

Update... 更新中...

I have managed to progress slightly with a revised web.config: 我已经通过修改后的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>

This is obviously significantly different from my first attempt, however the goal remains the same - I want to use a web browser to navigate to the URLs defined in the WebGet attributes of my service contract (IEventService). 这显然与我的第一次尝试有很大的不同,但是目标仍然是相同的-我想使用Web浏览器导航到服务合同(IEventService)的WebGet属性中定义的URL。

Using this web config, rather than getting a 400 error, I now recieve a 403 forbidden error for the methods I have defined. 使用此Web配置,而不是得到400错误,我现在收到我定义的方法的403禁止错误。 For methods that do not exist, I recieve, correctly, an 'Endpoint not found' message. 对于不存在的方法,我正确地收到“找不到端点”消息。 This makes me think that I have things running correctly but am just suffering from some sort of authentication issue at the application layer. 这使我认为我的东西运行正常,但是在应用程序层遇到某种身份验证问题。

I'm also getting the feeling that I'm making this too complicated. 我也感觉到这让我变得太复杂了。 This works without any kind of configuration in the Visual Studio web server after all. 毕竟,这无需在Visual Studio Web服务器中进行任何配置即可工作。

I think your problem is this: 我认为您的问题是这样的:

  • if you have service metadata enabled and have the httpGetEnabled=true in your config (and you do), then hitting the base address ( baseAddress="http://api.seetickets.com" ) will render out the metadata "you have a service" page. 如果您启用了服务元数据,并且在配置中具有httpGetEnabled=true (并且确实如此),则点击基址( baseAddress="http://api.seetickets.com"baseAddress="http://api.seetickets.com"元数据“您有一个服务”页面。 This is default WCF behavior. 这是默认的WCF行为。

  • you have an intro html page that "lives" at the same address --> you have a conflict 您有一个介绍HTML页面,该页面“居住”在同一地址->您有冲突

You can do two things, really: 您实际上可以做两件事:

  • disable the httpGetEnabled on your service metadata 在您的服务元数据上禁用httpGetEnabled

or: 要么:

  • move your intro page to another address, eg http://api.seetickets.com/intro or something (by setting the UriTemplate="....." on the service method) 将您的介绍页移动到另一个地址,例如http://api.seetickets.com/intro或其他内容(通过在服务方法上设置UriTemplate="....."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM