Pages

Tuesday, June 1, 2010

Fixing the WCF 'The remote server returned an error: (415) Unsupported Media Type.HTTP GET' Error

Recently I was building a typical WCF service hosted in IIS on Windows7 using VS.NET 2010 (I have done this several times). Every time I either tried to start the service within VS.NET locally, or actually consume the installed IIS instance on my local machine I would get the following error:

"Error: Cannot obtain Metadata from http://localhost:3509/MyNetworkingService.svc The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error"

I thought I had tried everything with the configuration; had all my "i" 's dotted and my "t" 's crossed. Plus the (415) Unsupported Media Type portion of the message was really throwing me off the proper path to fix the issue. The problem and the fix? A spelling mistake in the name of the service within the .config file. Fixing it was as easy as copying the properly assigned service name from the .svc Markup and into the .config file.

I had the word 'Networking' in my service name, and I spelled it 'Netwoking'. They were so close visually that it didn’t present itself immediately. As many services as I have created and still made a rookie mistake. A nice "Your service name and configuration service name don't match" message would have been nicer than a (415) Unsupported Media Type error, but no big deal it was my mistake.

The real lesson learned for the future - just be safe and copy the name used in the markup of the .svc file (you can get to this by right-clicking the .svc file in Solution Explorer and selecting "View Markup") and paste it into the .config. Below is a working template for a barebones WCF service hosted in IIS using "basicHttpBinding":


<system.serviceModel>
<services>
<service name="MyNetworkingService.NetworkingService"
behaviorConfiguration="ServiceBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="MyNetworkingService.INetworkingService" />

<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<!--When hosting a WCF service in IIS, you don't need to specify a base address nor an endpoint,
because the Endpoint will simply be the Virtual Path that your service points to.-->

</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below
to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment to
avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

0 comments:

Post a Comment