views:

562

answers:

4

What is the bare minimum I need to put in web.config to get WCF working with REST? I have annotated my methods with [WebGet], but they are not getting the message.

A: 

I hate to link dump but, Rick Strahl had a good article on configuring WCF REST for ASP.NET AJAX and plain old REST:

http://west-wind.com/weblog/posts/310747.aspx

What you're looking for is down near the end of the article.

HTH
Kev

Kev
+1  A: 

Ensure that you use a webHttpBinding on your endpoint (and not an httpBinding or wsHttpBinding). Here's my endpoint config...

    <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
      contract="WcfCore.ICustomer">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
willem
I tried that, but got an error message: "...AddressFilter mismatch at the EndpointDispatcher." It turns out that you also need to add WebHttpBehavior to your behaviors, but I don't know how to do that in web.config.
Kurt
+4  A: 

I discovered that you can add the following to the ServiceHost directive in the *.svc file, and it will automatically setup WebHttpBinding and WebHttpBehavior for you:

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

Note that the namespace is a little different from what is mentioned elsewhere on the web (such as in this MSDN article).

After doing this, I was able to delete the entire section from web.config and everything still worked!

Kurt
+1  A: 

You need to ensure that you have an address for your service host e.g

<services>
      <service name="SomeLib.SomeService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/somebase"/&gt;
          </baseAddresses>
        </host>
<!-- And one EndPoint **basicHttpBinding** WILL WORK !!! -->

        <endpoint 
          address="basic"
          binding="basicHttpBinding"
          contract="SomeLib.SomeContract"/>
</service>
</services>

So now, if you are self hosting via a console app for e.g...you can invoke your host via:

WebChannelFactory<IServiceContract> factory =
        new WebChannelFactory<IServiceContract>(
            new Uri("http://localhost:8080/somebase"));

When the console app starts up, the address will be browsable even if its self hosted and you should be able to invoke your actions based on your webget uri templates.

This minimum config will let you invoke WCF RestFULLY via selfhosting. If you're hosting in IIS it would essentially work the same way, except the svc file replaces our custom host.

RandomNoob