'Error when trying to create json web service in asp.net

I followed this to create a json web service in asp.net 3.5:

Creating JSON-enabled WCF services in .NET 3.5 (archive.org)

(Was at: http://www.pluralsight.com/community/blogs/fritz/archive/2008/01/31/50121.aspx)

It works fine if I want to use it internal but as I want to connect to it external I got an error saying: "Metadata publishing for this service is currently disabled."

So I tried enabling it but now I get the error: "Cannot add the 'serviceMetadata' behavior extension to 'MyServiceAspNetAjaxBehavior' endpoint behavior because the underlying behavior type does not implement the IEndpointBehavior interface.".

I know I'm doing something wrong in web.config, just can't figure it out, what am I doing wrong? Thanks!

This is in web.config:

<system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="MyServiceAspNetAjaxBehavior">
      <enableWebScript />
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </endpointBehaviors>
</behaviors>

//Needed to add this to be able to use the web service on my shared host
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
  <baseAddressPrefixFilters>
    <add prefix="http://www.domain.com"/>
  </baseAddressPrefixFilters>
</serviceHostingEnvironment>

<services>
  <service name="MyService">
    <endpoint address="" behaviorConfiguration="MyServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="MyService" />
    <endpoint contract="MyService" binding="mexHttpBinding" address="mex" />

  </service>
</services>

In MyService.cs:

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
    [OperationContract]

    public string GetForecast(string str)
    {
        return "Hello World";
    }
}

In MyService.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/MyService.cs" %>


Solution 1:[1]

Your MEX endpoint (the one for the metadata exchange) needs to have a specific, system-given contract IMetadataExchange (which you got wrong in your config):

<services>
  <service name="MyService">
    <endpoint 
        address="" 
        behaviorConfiguration="MyServiceAspNetAjaxBehavior" 
        binding="webHttpBinding"   
        contract="MyService" />
    <endpoint 
        address="mex"
        binding="mexHttpBinding" 
        contract="IMetadataExchange"   />
  </service>
</services>

With that contract, you should be able to see your metadata.

Word of warning though: the RESTful services typically don't expose any metadata such as a WSDL or XSD - that's a SOAP concept, really.

Marc

Solution 2:[2]

  <endpointBehaviors>
    <behavior name="EndPointBehaviorLarge">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </endpointBehaviors>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 aslanpayi