【问题标题】:VS2012 WCF REST Service - Error: Cannot obtain metadataVS2012 WCF REST 服务 - 错误:无法获取元数据
【发布时间】:2012-11-26 03:04:23
【问题描述】:

我已经构建了一个 WCF REST Web 服务(WCF 服务应用程序),当我使用 Visual Studio 2012 进行调试时,它将启动 WCF 测试客户端应用程序并尝试添加我的 Web 服务。我收到以下错误:

Error: Cannot obtain Metadata from http://localhost:50925/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:50925/Service1.svc    Metadata contains a reference that cannot be resolved: 'http://localhost:50925/Service1.svc'.    Content Type application/soap+xml; charset=utf-8 was not supported by service 

我已经访问了上面链接的 MSDN 文档,并且我相信我的 web.config 设置正确。

Web.config:

<?xml version="1.0"?>
<configuration>
<system.web>
 <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>  
<system.serviceModel>   
<services>
  <service name="Service1" behaviorConfiguration="Service1Behavior">       
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex">     </endpoint>
  </service>
</services>  
<behaviors>
  <serviceBehaviors>
    <behavior name="Service1Behavior">
      <serviceMetadata httpGetEnabled="true"/>     
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/>   
</system.webServer>
</configuration>

我仍然继续收到相同的错误消息。我是 WCF / VS 2012 和 .Net 4.0 的新手,但我精通 VS2008 .Net 2.0。

【问题讨论】:

    标签: json wcf .net-4.0 visual-studio-2012


    【解决方案1】:

    事实证明,我创建了一个新的 WCF 项目并查看了这里发生的情况。 VS2012/.Net 4.0 不喜欢定义了端点的服务配置。这是我的新 web.config 工作正常。我需要研究为什么会这样,但至少它回答了我的问题。

    <?xml version="1.0"?>
    <configuration>
    <system.web>
     <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
     <behaviors>
       <serviceBehaviors>
         <behavior>
           <serviceMetadata httpGetEnabled="true"/>
         </behavior>
       </serviceBehaviors>
     </behaviors>
     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
    </configuration>
    

    【讨论】:

      【解决方案2】:

      WCF REST 服务不公开元数据,这就是您收到该消息的原因(请参阅this blog post 中的更多详细信息)。如果您使用的是 WCF 测试客户端,它会与 SOAP 服务(端点)对话,而不是 REST 服务。

      如果要启用 RESTful 端点,您可以在配置中定义服务元素并在其中定义一个使用 webHttpBinding 的端点,并且该端点还需要一个 behaviorConfiguration 指向具有&lt;webHttp/&gt; 行为。

      【讨论】:

        猜你喜欢
        • 2011-08-28
        • 2012-04-28
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2016-04-10
        • 2021-04-17
        • 2011-04-01
        • 1970-01-01
        相关资源
        最近更新 更多