.NET to Javascript compiler

Server   Proxy   # Code   Html page   Start
[WebService(Namespace = "http://jscriptsuite/TestWebSite/services")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
    [WebMethod]
    public DateTime GetException()
    {
        return DateTime.Now;
        // throw new ApplicationException("Test exception 1", new ApplicationException("Test exception 2"));
    }

    [WebMethod]
    public AllTypesResponse GetAllTypes(string[] text, int[] inp, long?[] l, bool[] b, double?[] d, DateTime[] dt)
    {
        AllTypesResponse response = new AllTypesResponse()
        {
            Strings = text,
            Ints = inp,
            Longs = l,
            Booleans = b,
            Doubles = d,
            DateTimes = dt,
            Message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
        };

        for (int i = 0; i < text.Length; i++)
        {
            text[i] += "_out";
        }

        return response;
    }

    [WebMethod]
    public Person[] GetPersons(Person[] persons)
    {
        return persons;
    }
}

[ServiceContract(ServiceType = typeof(Services.WebServiceSoap))]
public interface IServiceClient
{
    IDisposable GetException(Action success, Action<Exception> failed);

    IDisposable GetPersons(Person[] persons,
        Action<Person[]> success, 
        Action<Exception> failed);

    Task<AllTypesResponse> GetAllTypes(string[] text,
        JArray<int?> inp,
        JArray<long?> l,
        bool[] b,
        double?[] d,
        JDate[] dt);
}

class WebServiceApp
{
    readonly static string serviceUrl = "Services/WebService.asmx";

    internal static void GetException(string result)
    {
        CreateClient().GetException(
            delegate() { ShowResult(result, "unexpected success"); },
            delegate(Exception e) { ShowResult(result, e); }
        );
    }

    internal static async void GetAllTypes(string result)
    {
        AllTypesResponse request = new AllTypesResponse()
        {
            Strings = new string[] { "Text1", "Text2" },
            Ints = new JArray<int?>() { 3, 15, 0, 0 },
            Longs = new JArray<long?>() { 181223344, 1, 24, null },
            Booleans = new bool[] { true, false, false, false },
            Doubles = new double?[] { 1, 2.5, null, 4 },
            DateTimes = new JDate[] { new JDate(), new JDate(2000, 1, 1) }
        };

        IServiceClient client = CreateClient();
        try
        {

            AllTypesResponse response = await client.GetAllTypes(request.Strings,
                request.Ints,
                request.Longs,
                request.Booleans,
                request.Doubles,
                request.DateTimes);

            ShowResult(result,
                        string.Format("Text: \r\n\t'{0}'\r\n\t'{1}'\r\n"
                        + "Int: \r\n\t'{2}'\r\n\t'{3}'\r\n"
                        + "Long: \r\n\t'{4}'\r\n\t'{5}'\r\n"
                        + "Bool: \r\n\t'{6}'\r\n\t'{7}'\r\n"
                        + "Double: \r\n\t'{8}'\r\n\t'{9}'\r\n"
                        + "DateTime: \r\n\t'{10}'\r\n\t'{11}'\r\n"
                        + "ServerTime: '{12}'",
                        request.Strings.Join<string>(':'), response.Strings.Join<string>(':'),
                        request.Ints.Join<int?>(':'),
                        response.Ints.Join(':'),
                        request.Longs.Join(':'),
                        response.Longs.Join(':'),
                        request.Booleans.Join<bool>(':'), response.Booleans.Join<bool>(':'),
                        request.Doubles.Join<double?>(':'), response.Doubles.Join<double?>(':'),
                        request.DateTimes.Join<JDate>(';'), response.DateTimes.Join<JDate>(';'),
                        response.Message));
        }
        catch(Exception e)
        {
            ShowResult(result, e);
        }
    }

    internal static void GetPersons(string result)
    {
        Person[] request = new Person[] {
            new Person() { LastName = "LN1", FirstName = "Fn1", Birthday = new JDate(2000, 1, 1) },
            new Person() { LastName = "LN2", FirstName = "Fn2" },
            null
        };

        CreateClient().GetPersons(request,
            delegate(Person[] response)
            {
                ShowResult(result,
                    string.Format("PI1({0}, {1}, {2}), PO1({3}, {4}, {5})\r\n"
                    + "PI2({6}, {7}, {8}), PO2({9}, {10}, {11})\r\n"
                    + "PI3({12}), PO3({13})",
                    request[0].FirstName, request[0].LastName, request[0].Birthday,
                    response[0].FirstName, response[0].LastName, response[0].Birthday,
                    request[1].FirstName, request[1].LastName, request[1].Birthday,
                    response[1].FirstName, response[1].LastName, response[1].Birthday,
                    request[2], response[2]));
            },
            delegate(Exception e) { ShowResult(result, e); });
    }

    static void ShowResult(string resultId, object result)
    {
        ((HtmlInputText)HtmlDocument.Current.GetElementById(resultId)).InnerText = result.ToString();
    }

    static IServiceClient CreateClient()
    {
        if (((HtmlRadioButton)HtmlGlobal.document.GetElementById("soap11")).Checked)
        {
            return new Soap11Service<IServiceClient>() { Url = serviceUrl }.Proxy;
        }

        if (((HtmlRadioButton)HtmlGlobal.document.GetElementById("soap12")).Checked)
        {
            return new Soap12Service<IServiceClient>() { Url = serviceUrl }.Proxy;
        }

        return new JsonService<IServiceClient>() { Url = serviceUrl }.Proxy;
    }
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>
		WebService.html: WebService data transport implementation
	</title>
	<script type="text/javascript" language="javascript" src="js/WebService.js?v3"></script>
</head>
<body>
	WebService.html: WebService data transport implementation
    <br />
    <input type="radio" id="soap11" name="protocol"/> <label for="soap11">Soap 1.1</label> 
    <input type="radio" id="soap12" name="protocol"/> <label for="soap12">Soap 1.2</label> 
    <input type="radio" id="json" name="protocol" checked="checked"/> <label for="json">Json</label> 
	<br/><input type="button" value="GetException" onclick="GetException('exceptionResult')"/>
	<br/><textarea id="exceptionResult" style="width:400px; height:100px"></textarea>
	<br/><input type="button" value="GetAllTypes (await)" onclick="GetAllTypes('allTypesResult')"/>
	<br/><textarea id="allTypesResult" style="width:400px; height:200px"></textarea>
	<br/><input type="button" value="GetPersons" onclick="GetPersons('getPersonsResult')"/>
	<br/><textarea id="getPersonsResult" style="width:400px; height:200px;" wrap="off"></textarea>
</body>
</html>
© 2009 JScriptSuite. All rights reserved.