简体   繁体   中英

VS2012 WCF REST Service - Error: Cannot obtain metadata

I have built a WCF REST web service (WCF Service Application) and when I debug with Visual Studio 2012, it will spin up the WCF Test Client app and try to add my web service. I am getting the following error:

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 

I have visited the MSDN document linked above and I believe I have set up my web.config correctly.

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>

I still continue to receive the same error message. I am new to WCF / VS 2012 and .Net 4.0, but I am fluent in VS2008 .Net 2.0.

WCF REST services do not expose metadata, which is why you were getting that message (see more details in this blog post ). If you're using the WCF Test Client, it's talking to a SOAP service (endpoint), not a REST one.

If you want to enable a RESTful endpoint, you can define the service element in configuration and define an endpoint inside of it which uses the webHttpBinding , and the endpoint also needs a behaviorConfiguration pointing to an endpoint behavior with the <webHttp/> behavior.

As it turns out I created a new WCF project and looked at what was going on here. VS2012/.Net 4.0 does not like the service configurations with endpoints defined. Here is my new web.config that works fine. I will need to look into why this is but at least it answers my question.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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