Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#
Tip/Trick

CherrySharp - Call functions via http

Rate me:
Please Sign up or sign in to vote.
4.89/5 (5 votes)
26 Apr 2010CPOL2 min read 17.3K   5   10
Download source and example project - 64.37 KB IntroductionSome 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...


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:



C#
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:



C#
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:



C#
// <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)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionshould this work with obfuscated code? Pin
Lee.W.Spencer1-Aug-13 5:51
Lee.W.Spencer1-Aug-13 5:51 
AnswerRe: should this work with obfuscated code? Pin
Froghut1-Aug-13 5:57
Froghut1-Aug-13 5:57 
GeneralDownload link is dead Pin
dedel25-Apr-10 0:08
professionaldedel25-Apr-10 0:08 
GeneralRe: Download link is dead Pin
Froghut25-Apr-10 1:38
Froghut25-Apr-10 1:38 
GeneralRe: Download link is dead Pin
Froghut26-Apr-10 4:02
Froghut26-Apr-10 4:02 
QuestionHow much of CherryPy is implemented? Pin
torial23-Apr-10 6:11
torial23-Apr-10 6:11 
AnswerRe: How much of CherryPy is implemented? Pin
Froghut23-Apr-10 7:34
Froghut23-Apr-10 7:34 
GeneralLooks awesome Pin
toaster9922-Apr-10 15:33
toaster9922-Apr-10 15:33 
Looks really cool, but the link is down Frown | :-(
Computer Logiciel

Advanced Software for Today's Generation

GeneralRe: Looks awesome Pin
Froghut22-Apr-10 21:35
Froghut22-Apr-10 21:35 
GeneralRe: Looks awesome Pin
Froghut26-Apr-10 4:03
Froghut26-Apr-10 4:03 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.