简体   繁体   English

IIS托管的WCF服务返回HTTP 400错误请求

[英]IIS hosted WCF Service return HTTP 400 Bad Request

I have been searching for hours, but I could not find the solution. 我一直在寻找几个小时,但我找不到解决方案。 I will explain briefly. 我将简要解释一下。

I am learning WCF Services. 我正在学习WCF服务。 I have just created a service and browse it. 我刚刚创建了一个服务并浏览它。 Here is the config file: 这是配置文件:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="EmployeeServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="EmployeeServiceBehaviour" name="EmployeeConfiguration">
        <endpoint address="http://localhost:2005/EmployeeService.svc" binding="basicHttpBinding"
          bindingConfiguration="" contract="IEmployeeConfiguration" />
      </service>
    </services>
  </system.serviceModel>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.webServer>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

When browse it from Visual Studio there seems no problem. 从Visual Studio浏览它似乎没有问题。 It works perfectly. 它完美地运作。

在此输入图像描述

在此输入图像描述

Second, I am trying to publish it on IIS. 其次,我试图在IIS上发布它。 What I am doing is this: 我在做什么是这样的:

I publish the service to a folder and add this service to IIS. 我将服务发布到一个文件夹,并将此服务添加到IIS。

在此输入图像描述

I select port 3006 as a port. 我选择端口3006作为端口。

Below its config file. 在它的配置文件下面。 Note that I also changed port inside config to 3006 请注意,我还将配置中的端口更改为3006

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="EmployeeServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="EmployeeServiceBehaviour" name="EmployeeConfiguration">
        <endpoint address="http://localhost:3006/EmployeeService.svc" binding="basicHttpBinding"
          bindingConfiguration="" contract="IEmployeeConfiguration" />
      </service>
    </services>
  </system.serviceModel>
  <system.web>
    <compilation/>
  </system.web>
  <system.webServer>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

And I am waiting to run smoothly but: 我正在等待顺利运行但是: 在此输入图像描述

在此输入图像描述

IIS gives me a blank page from Chrome IIS为我提供了Chrome的空白页面

在此输入图像描述

And HTTP 400 Bad Request from Explorer 来自资源管理器的HTTP 400 Bad Request

在此输入图像描述

Lastly, if I remove address part from config file everything works well. 最后,如果我从配置文件中删除地址部分一切正常。 But other confused thing is that, on my other computer after above scenario(address provided) I can reach the service. 但其他困惑的是,在我的另一台计算机上面的情况(提供地址)后,我可以到达该服务。 So, I really tired of searching why this is working on one computer and not working on another one. 所以,我真的厌倦了为什么这个在一台计算机上工作而不在另一台计算机上工作。 Could someone explain it to me? 有人可以向我解释一下吗?

I know it is a bit longer, but I have to explain it clearly. 我知道它有点长,但我必须清楚地解释它。 Thanks 谢谢

According my searches I should not provide an address. 根据我的搜索,我不应该提供一个地址。

msdn.microsoft.com/en-us/library/aa751792(v=vs.110).aspx msdn.microsoft.com/en-us/library/aa751792(v=vs.110).aspx

You must always use relative endpoint addresses for IIS-hosted service endpoints. 您必须始终为IIS托管的服务端点使用相对端点地址。 Supplying a fully-qualified endpoint address (for example, localhost/MyService.svc) can lead to errors in the deployment of the service if the endpoint address does not point to the IIS-application that hosts the service exposing the endpoint. 如果端点地址未指向承载公开端点的服务的IIS应用程序,则提供完全限定的端点地址(例如,localhost / MyService.svc)可能导致服务部署中的错误。 Using relative endpoint addresses for hosted services avoids these potential conflicts. 使用托管服务的相对端点地址可以避免这些潜在的冲突。

I think this will solve your problem: 我认为这将解决您的问题:

Add this endpoint to your service: 将此端点添加到您的服务:

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

And change the name attribute of the service to your service class's full name: 并将servicename属性更改为服务类的全名:

<service behaviorConfiguration="EmployeeServiceBehaviour" 
    name="Namespace.EmployeeConfigurationClass">

Hope that is enough 希望就够了

This may be of help. 这可能有所帮助。 ive just spent over 2 hours trying to get this working. 我只花了2个多小时试图让这个工作。 i use FF and its set as the default browser. 我使用FF及其设置作为默认浏览器。

in FF it was adding a / on the end of my URL 在FF中,它在我的URL末尾添加了一个/

http://services.tester.dev/VehicleFeedService.svc/

which returned a NetworkError: 400 Bad Request 它返回了一个NetworkError: 400 Bad Request

however in IE or chrome, it doesnt put the / on the end and it works fine. 但是在IE或Chrome中,它并没有把/放在最后,它工作正常。

one thing to note.. even in FF which was giving me a 400 bad request, the ?wdsl did work 有一点需要注意..即使在FF给了我400个不良请求, ?wdsl确实有效

http://services.tester.dev/VehicleFeedService.svc?wsdl

it appears that the / was causing the issue 似乎/正在导致问题

You can try fiddler and also try the svcTracer which may give you lot of debugging information on the top of it you can also use includeExceptionDetailInFaults=true flag on the server but its important to flag that its not always right to send this information to the client specially if client is an external entity. 您可以尝试fiddler并尝试svcTracer,它可以为您提供大量的调试信息,您也可以在服务器上使用includeExceptionDetailInFaults=true标志,但重要的是标记它并不总是正确地将此信息发送给客户端特别是如果客户是外部实体。 With this warning following is the hint how to use it. 有了这个警告,下面是提示如何使用它。

<serviceBehaviors>
    <behavior name="ServiceBehavior">
    ....
        <serviceDebug includeExceptionDetailInFaults="true" />
    ....
    </behavior>
</serviceBehaviors>

Happy debugging :) 快乐调试:)

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

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