How to configure WCF REST service hosted in IIS (properly)?

30 בדצמבר 2008

3 תגובות

Trying to configure WCF REST service hosted in IIS you may encounter an error message saying something like:

"The message with To '*' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree."

where '*' is the address you are trying to access. There are some answers here and there on the web but those are incomplete or hard to understand.

The Problem

When using the "*.svc" file to host a service within ASP.NET web site, regular WCF ServiceHost is created. WCF itself does not understand the REST address (as specified by the UriTemplate property of WebGet attribute), so we should configure the proper endpoint behavior "webHttp".

The Solution

So, here is how to configure WCF REST service to be hosted in IIS:

To host a service on IIS we are creating ".svc" file to map that file URL to the service type, for example:

<%@ ServiceHost Language="C#" Debug="true" Service="[ServiceFullTypeName]" %>

To enable this service as REST service, we will configure the required endpoint and behaviors in web.config:

<system.serviceModel>
<behaviors>
<endpointBehaviors>
          <behavior name="WebBehavior">
            <webHttp/>
          </behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="[ServiceTypeFullName]">
<endpoint address="js" binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="[ServiceContractName]"/>
</service>
</services>
</system.serviceModel>

Thats all!

Note that the "baseAddress" of this service is the URL to the ".svc" file, the "js" address is not required, but I prefer to distinguish between the "regular" address and the JSON address.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

3 תגובות

  1. Paul S15 בינואר 2009 ב 1:13

    Thanks, this solved my exact problem and I tried many other ideas over a couple of hrs.

    Good job sir

    הגב
  2. Raj16 בנובמבר 2010 ב 23:57

    Thanks for the solution.Was looking for it for a while.

    הגב
  3. Angelo Laris21 ביולי 2011 ב 5:18

    You can also fix it by
    Factory="System.ServiceModel.Activation.WebServiceHostFactory"

    הגב