Run WCF methods from a browser

Yes, you can call those methods in a browser, if your service is configured properly, though you have the URL syntax wrong.

To call WCF methods from a browser, you need to do two things:

  • Use [WebGet] and [WebInvoke] attributes on your methods, which you have done.
  • Use a webHttpBinding for the endpoint of your service and enable the webHttp behavior. See http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx for a sample configuration, but the relevant parts are:

     <service> 
        <endpoint behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="MyServiceContract" /> 
    </service> 
    
    <endpointBehaviors> 
        <behavior name="webBehavior"> 
            <webHttp /> 
        </behavior> 
    </endpointBehaviors> 
    

Once that is done, WCF will start listening for URL request and route them to your appropriate web methods. You can set up URL templates in your WebGet or WebPost attributes that map URL segments to method parameters, if you want to make your URLs “cleaner”, but that’s optional. Otherwise, you pass parameters the same way you pass parameter to any other URL, using the parameter delimiter:

http://localhost:49815/MyService.svc/methodName?parameterName=value

Note that the default for a web-invoked method is a POST. Technically you can do these through a browser but it’s much harder (you’d have to make a local HTML form, or use your Javascript console, or something similar), but the WebGet methods can be invoked just by requesting the correct URL.

Also, if your methods return anything more complex than a string, WCF will try to serialize it as JSON; you may need to ‘view source’ on the resulting page to see it.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)