Click here to Skip to main content
Click here to Skip to main content

CherrySharp - Call functions via http

By , 26 Apr 2010
 
 

Introduction

 

Some time ago I had to use CherryPy in one of our companys projects. To quote the website, 'CherryPy is a pythonic, object-oriented HTTP framework.' - but the feature I found the most usefull was that you can expose methods so they can be called by typing in a url in any webbrowser.
I thought it would be nice to have something like that in C#, but had no time to create something like that.

 

Then a few days ago I found the MiniHttpd project here on codeproject - basically also an excellent and easy to use HTTP server framework. And then I thought why not use the MiniHttpd library to write a little something that would emulate the method exposure feature of CherryPy!

 

So thats what I did and what I want to present here.

 

Using the code

 

Usage is really simple!
At first reference my CherrySharp library. Then you just have to write a class that exposes methods using the HttpVisible attribute like this:

 
public class ExampleClass
{
	[HttpVisible]
	public int Add(int a, int b)
	{
		return a + b;
	}
}
 

Then just call the Init function of the CherryServer giving it an instance of that class and the port on which the webserver should listen on:

 
CherryServer.Init(new ExampleClass(), 1234);
 

And thats it! Now "open" the exposed methods by typing in a url. Just use the method name as the 'filename' and append any parameters as a query string, like this:
http://127.0.0.1:1234/add?a=10&b=15

 

Points of Interest

 

Whats happening behind the scenes? I use reflection to find all methods that have the HttpVisible attribute and 'register' them as virtual 'files' in MiniHttpd.
Once a webrequest is made I look at the Query string to get the parameters and again use reflection to cast the parameters to their appropiate types and call the function of the instance of the class that was used in the Init call.

 

Methods that are made http visible can return stuff - it will just be converted to a string using the ToString() method and be printed to the resulting web page (its actually only text, no html).

 

The Init method has several overloads. You can specify wether error messages should be printed to the resulting html page describing the error when something went wrong (useful for debugging, but turned off by default).
You can also specify an object path name to be used for accessing the methods of the specific instance like this:
http://localhost:1234/objectname/method?

 

To make several different Instances and their http visible methods accessible through the same http server you can use the server id that gets returned by the Init method:

 
// <a class="linkifyplus" href="http://localhost:1234/a/print?Integer=1234">http://localhost:1234/a/print?Integer=1234</a>
int serverId = CherryServer.Init(new ExampleClass1(), "a", 1234);
 
// <a class="linkifyplus" href="http://localhost:1234/b/add?a=10&b=15">http://localhost:1234/b/add?a=10&b=15</a>
CherryServer.InitAdditional(new ExampleClass2(), "b", serverId);
 

The HttpVisible attribute also has some properties to make method and parameter names case sensitive and to use a custom method name instead of the default one.

 

References

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Froghut
Software Developer
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralDownload link is deadmemberdedel25 Apr '10 - 0:08 
GeneralRe: Download link is deadmemberFroghut25 Apr '10 - 1:38 
GeneralRe: Download link is deadmemberFroghut26 Apr '10 - 4:02 
QuestionHow much of CherryPy is implemented?membertorial23 Apr '10 - 6:11 
AnswerRe: How much of CherryPy is implemented?memberFroghut23 Apr '10 - 7:34 
GeneralLooks awesomememberToaster9922 Apr '10 - 15:33 
GeneralRe: Looks awesomememberFroghut22 Apr '10 - 21:35 
GeneralRe: Looks awesomememberFroghut26 Apr '10 - 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 26 Apr 2010
Article Copyright 2010 by Froghut
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid