Click here to Skip to main content
12,997,441 members (58,951 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 22 Apr 2010

CherrySharp - Call functions via http

, 26 Apr 2010
Rate this:
Please Sign up or sign in to vote.
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...


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

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:

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.



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


About the Author

Software Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

Questionshould this work with obfuscated code? Pin
Lee.W.Spencer1-Aug-13 5:51
memberLee.W.Spencer1-Aug-13 5:51 
AnswerRe: should this work with obfuscated code? Pin
Froghut1-Aug-13 5:57
memberFroghut1-Aug-13 5:57 
GeneralDownload link is dead Pin
dedel25-Apr-10 0:08
memberdedel25-Apr-10 0:08 
GeneralRe: Download link is dead Pin
Froghut25-Apr-10 1:38
memberFroghut25-Apr-10 1:38 
GeneralRe: Download link is dead Pin
Froghut26-Apr-10 4:02
memberFroghut26-Apr-10 4:02 
QuestionHow much of CherryPy is implemented? Pin
torial23-Apr-10 6:11
membertorial23-Apr-10 6:11 
AnswerRe: How much of CherryPy is implemented? Pin
Froghut23-Apr-10 7:34
memberFroghut23-Apr-10 7:34 
GeneralLooks awesome Pin
Toaster9922-Apr-10 15:33
memberToaster9922-Apr-10 15:33 
GeneralRe: Looks awesome Pin
Froghut22-Apr-10 21:35
memberFroghut22-Apr-10 21:35 
GeneralRe: Looks awesome Pin
Froghut26-Apr-10 4:03
memberFroghut26-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.

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