Skip to content

Getting Started (Asp.Net)

Austin Harris edited this page Jul 10, 2017 · 3 revisions

Using json-rpc.net within a asp.net application;

-- Please see this issue until the wiki is updated. https://github.com/Astn/JSON-RPC.NET/pull/83

  1. Create a new ASP.NET Web Application.
  2. Use NuGet to Download the most recent JSON-RPC.NET Asp.Net package.
  3. Edit the web.config file and add a http handler.

For Casini (ASP.NET development server) and IIS6 add the following to <system.web>

  <httpHandlers>
      <add type="AustinHarris.JsonRpc.Handlers.AspNet.JsonRpcHandler" verb="*" path="*.rpc"/>
  </httpHandlers>

For IIS 7+ add the following to <system.webServer>

   <handlers>
      <add name="jsonrpc" type="AustinHarris.JsonRpc.Handlers.AspNet.JsonRpcHandler" verb="*" path="*.rpc"/>
   </handlers>

Create a new class that inherits from JsonRpcService.

    public class HelloWorldService: JsonRpcService {}

Inside your new class create a new method, and attribute it with [JsonRpcMethod].

        [JsonRpcMethod]
        private string helloWorld(string message){
            return "Hello World "+ message;
        }

In Global.asax.cs add a new static member of your new service class type.

public class Global : System.Web.HttpApplication {
    private HelloWorldService service;

    // important that you do this as part of the asp.net lifecycle instead of as static due to the way IIS works. 
    override void init() { 
       service = new HelloWorldService();
    }
}

Test it. POST a JSON-RPC request to /json.rpc.

POST http://localhost:49718/json.rpc HTTP/1.1
User-Agent: Fiddler
Content-Type: Application/Json-Rpc
Host: localhost:49718
Content-Length: 62

{"method": "helloWorld", "params": ["Hello World"], "id": 1  }    

And you should receive a response.

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 29 Aug 2011 05:21:22 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 58
Connection: Close

{"Result":"Hello World Hello World","Error":null,"Id":"1"}