views:

194

answers:

1

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

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"/&gt;
  </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" %>
+1  A: 

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

marc_s
Thanks I changed to: <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />But still gets the same error, do I need to change something in my web service file?
I just want a simple json web service to be called from my iphone, isn't that possible with asp.net? is there something I'm missing here?
Yes it's possible - you just can't have any metadata associated with it (REST services don't know that concept)
marc_s