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; }
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.
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.
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.
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:
[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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With