Click here to Skip to main content
11,433,997 members (55,858 online)
Click here to Skip to main content

Tagged as

CherrySharp - Call functions via http

, 26 Apr 2010 CPOL
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...

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)

Share

About the Author

Froghut
Software Developer
Germany Germany
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 26 Apr 2010
Article Copyright 2010 by Froghut
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid