Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run WCF methods from a browser

Tags:

c#

wcf

I am creating a very basic WCF service with C# in Visual Studio 2010. I want to know if I can run my methods directly from a browser by typing something like: //localhost:49815/Service1.svc/methodName(parameterValue)?

Here is the essence of my code.

Interface:

using ... namespace WcfService1{        [ServiceContract]     public interface IService1{         [OperationContract]         [WebGet]         string echoWithGet(string s);         [OperationContract]         [WebInvoke]         string echoWithPost(string s);     } } 

Methods:

 public string echoWithGet(string s ){             return "Get: "+s;         }   public string echoWithPost(string s){             return "Post: " + s;         } 
like image 909
javaNinja Avatar asked Sep 24 '13 18:09

javaNinja


People also ask

Can we use WCF in web application?

Example: I have created WCF Services and now I want to use it in a real requirement so I used it in an ASP.Net Web Application. In other words, I am consuming the WCF Service in an ASP.Net web application. Similarly, you can use the same WCF Service in Windows, console application, Java and other applications.

How do I run a WCF service?

To open WCF Test Client, open Developer Command Prompt for Visual Studio and execute WcfTestClient.exe. Select Add Service from the File menu. Type http://localhost:8080/hello into the address box and click OK. Make sure the service is running or else this step fails.

How do I access SVC files?

Solution: Open IIS Manager (Start > Run > search "inetmgr"). Browse from the top level server to Sites and expand the Default Web Site. Select each of the Revit Server applications, and in the Content View, right click on the SVC file and select Browse.


1 Answers

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.

like image 54
Michael Edenfield Avatar answered Sep 25 '22 03:09

Michael Edenfield