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

标签 c# asp.net .net wcf iis

我已经搜索了几个小时,但找不到解决方案。我将简要说明。

我正在学习 WCF 服务。我刚刚创建了一个服务并浏览了它。这是配置文件:

<?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>

从 Visual Studio 浏览时似乎没有问题。效果很好。

enter image description here

enter image description here

其次,我试图在 IIS 上发布它。我正在做的是:

我将服务发布到文件夹并将此服务添加到 IIS。

enter image description here

我选择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>

我正在等待顺利运行但是: enter image description here

enter image description here

IIS 从 Chrome 给我一个空白页面

enter image description here

来自资源管理器的 HTTP 400 错误请求

enter image description here

最后,如果我从配置文件中删除地址部分,一切正常。但另一个令人困惑的事情是,在上述情况(提供的地址)之后我的另一台计算机上我可以访问该服务。所以,我真的厌倦了寻找为什么这在一台计算机上工作而不在另一台计算机上工作。谁能给我解释一下?

我知道它有点长,但我必须解释清楚。 谢谢

最佳答案

根据我的搜索,我不应该提供地址。

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

You must always use relative endpoint addresses for IIS-hosted service endpoints. 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. Using relative endpoint addresses for hosted services avoids these potential conflicts.

关于c# - IIS 托管的 WCF 服务返回 HTTP 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24855068/

相关文章:

c# - 导航时刷新 ViewModel 列表

c# - 是否有像哈希表这样的 3 个链接值的预定义类?

asp.net - WebScriptServiceHostFactory 与 WebServiceHostFactory

jquery - 通过使用 jQuery 解析 sessionID 从 WCF 服务访问 session 变量

.net - 如何强制针对 .NET Framework 4 编译的应用程序在 .NET Framework 4.6.1 下运行?

c# - 如何调试 "The caller does not have permission"

c# - 有没有一种简单的方法可以通过名称(来自文本文件)来处理应用程序的变量?

c# - 将 doc 文件插入 SQL Server 中的列

.net - Entity Framework 的 SQL 列默认值

c# - 如何确保我的软件连接到正确的服务器?