【问题标题】:Self Hosted WCF Service throws Error Message "405 Method not allowed"自托管 WCF 服务抛出错误消息“405 Method not allowed”
【发布时间】:2016-09-07 23:38:26
【问题描述】:

我有一个自托管 WCF 服务,如下所示:

[ServiceContract]    
public interface IService {
    [OperationContract]     
    [WebGet]   
    List<Data> GetData();
    //...and much more Methods
}

我的 App.config 如下所示:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MetaInformation">
        <serviceMetadata httpGetEnabled="true"
                         httpGetUrl="http://localhost:8500/MetaInfo"
                         httpsGetBinding="" />
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="EndpointBehavior">
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <services>     
    <service behaviorConfiguration="MetaInformation" name="Library.WcfService.ServiceModel">
      <endpoint address="http://localhost:8500/Service"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBindingSettings"
                contract="Library.WcfService.IService"
                bindingName="BasicHttpBindingSettings"
               behaviorConfiguration="EndpointBehavior"/>                        
    </service>
  </services>
  <bindings>      
    <basicHttpBinding>
      <binding name="BasicHttpBindingSettings"
               closeTimeout="00:50:00"
               openTimeout="00:50:00"
               sendTimeout="00:50:00"
               maxBufferSize="524288"
               transferMode="Streamed"
               maxReceivedMessageSize="2147483647"
               maxBufferPoolSize="2147483647"
               messageEncoding="Text">
        <readerQuotas maxDepth="2147483647"
                      maxStringContentLength="2147483647"
                      maxArrayLength="2147483647"
                      maxBytesPerRead="2147483647"
                      maxNameTableCharCount="2147483647" />
      </binding>
    </basicHttpBinding>
  </bindings>
</system.serviceModel>

<system.web>
  <httpRuntime maxRequestLength="102400"/>    
</system.web>

当我在本地计算机上运行此服务器和客户端应用程序时,它运行良好。 但是当我尝试在另一台 PC 上运行服务器应用程序时,我无法在客户端添加服务引用,因为我得到了这个:

405 Method not allowed 元数据包含无法解析的引用:“http://192.168.178.54:8500/MetaInfo”。这不是一个 收听http://192.168.178.54:8500/MetaInfo端点在场谁 可以接受消息。这通常是由不正确的 地址或 SOAP 操作

我几乎尝试了我在互联网上找到的所有东西,但没有任何效果。 切换到 IIS 或使用其他协议应该是 B 计划,我想通过 http 保持它自托管。

请有人帮我解决这个问题。

【问题讨论】:

  • 您能否告诉我们另一台计算机上安装了哪个 .NET 版本,以及您是否将 .NET 未提供的所有必需 dll(来自每个类中的使用)复制到另一台计算机的 WCF-bin 文件夹中?可能是未安装 ether .NET(在所需的最小版本中),或者您的 GAC(全局程序集缓存)中有其他计算机没有的东西。
  • 我在两台机器上都使用 .NET 4.0。我刚刚将整个 VS 解决方案复制到另一台 PC 进行调试。当我在另一台 PC 上运行客户端时,它也可以工作。仅当我尝试通过 LAN 使用 WCF 服务时才会收到此错误。

标签: c# wcf


【解决方案1】:

您的操作合同用[WebGet] 属性修饰,这意味着您正试图将您的服务公开为REST。但是您的服务使用basicHttpBinding 作为构建不受支持的通信通道的手段,因为此绑定的内容类型是soap+xml。在这种情况下,您需要使用WebHttpBinding,这是唯一支持WCF Services 的restful 实现并同时支持Xml 和Json 数据类型的绑定。

【讨论】:

  • 我将 App.config 更改为 WebHttpBinding。仍然收到 405 错误:(
  • 是的,在我的回答中。
  • 终于找到错误了!我必须定义确切的 IP,而不是使用“localhost”或“0.0.0.0”。在本例中为 192.168.178.54。然后就可以了。
猜你喜欢
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 2017-03-25
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 2018-04-17
相关资源
最近更新 更多