.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<TPersonsList, TPerson>
        where TPersonsList : ICollection<TPerson>, new()
        where TPerson : IPerson, new()
{
    IDisposable GetPersons(IEnumerable<IPerson> persons,
        Action<TPersonsList> success,
        Action<Exception> failed);

    IDisposable GetPersons(IEnumerable<IPerson> persons,
        Action<TPerson[]> success,
        Action<Exception> failed);
}

public interface IPerson
{
    [DataMember]
    string FirstName { get; set; }

    [DataMember]
    string LastName { get; set; }

    [DataMember]
    JDate Birthday { get; set; }
}

public class GenericServiceApp
{
    readonly static string serviceUrl = "Services/WebService.asmx";
    readonly static HtmlLazy<GenericServiceApp> instance = new HtmlLazy<GenericServiceApp>(delegate() { return new GenericServiceApp(); });

    readonly Person[] request;
    readonly Grid<Person> grid;
    readonly HtmlElement error;

    GenericServiceApp()
    {
        request = new Person[]
        {
            new Person() { FirstName = "Alice", LastName = "Cooper", Birthday = new JDate(2000, 1, 1)  },
            new Person() { FirstName = "Bob", LastName = "Silver", Birthday = new JDate(1992, 4, 5)  },
            new Person() { FirstName = "Sue", LastName = "Barnaby" }
        };

        grid = new Grid<Person>() { 
            Element = HtmlDocument.Current.GetElementById("grid"),
            Columns =
            {
                CreateColumn("First Name", delegate(Person person) { return person.FirstName; }),
                CreateColumn("Last Name", delegate(Person person) { return person.LastName; }),
                CreateColumn("Full Name", delegate(Person person) { return person.FullName; }),
                CreateColumn("Birtday", delegate(Person person) { return string.Format("{0:d}", person.Birthday); })
            },
            ClassName = "grid"
        };

        error = HtmlDocument.Current.GetElementById("error");
        HtmlWindow.Current.AdviseResize(Resize);
        Resize();
    }

    void Resize()
    {
        HtmlRect rc = grid.Element.GetBoundingClientRect();
        grid.Element.Style.Height = Math.Max(50, HtmlWindow.Current.InnerHeight - rc.Top - 70) + "px";
        grid.Element.Style.Width = Math.Max(50, HtmlWindow.Current.InnerWidth - 2 * rc.Left) + "px";
    }

    static GridColumn<Person> CreateColumn(string title, Func<Person, string> accessor)
    {
        return new GridColumn<Person>()
        {
            Width = 100,
            AllowResize = true,
            Header = new SortableTextColumnHeader() { Text = title },
            Renderer = delegate(HtmlElement outer, Person person, object state)
            {
                string text = person != null ? accessor(person) : null;
                if (text != state as string)
                {
                    outer.InnerText = text;
                }
                return text;
            }
        };

    }

    void _ReceiveTypedArray()
    {
        CreateClient().GetPersons(request,
            delegate(Person[] response)
            {
                ShowResult(new ObservableList<Person>(response), null);
            },
            delegate(Exception e) { ShowResult(null, e); });

    }

    void _ReceiveTypedList()
    {
        CreateClient().GetPersons(request,
            delegate(ObservableList<Person> response)
            {
                ShowResult(response, null);
            },
            delegate(Exception e) { ShowResult(null, e); });
    }

    void ShowResult(IObservableList response, Exception e)
    {
        if (e == null)
        {
            error.Style.Display = StyleDisplay.None;
            grid.Element.Style.Display = StyleDisplay.Default;
            grid.DataSource = response;
        }
        else
        {
            error.Style.Display = StyleDisplay.Default;
            grid.Element.Style.Display = StyleDisplay.None;
            error.InnerText = e.ExtractAllMessages();

        }
    }

    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;
    }

    public static void ReceiveTypedArray()
    {
        instance.Value._ReceiveTypedArray();
    }

    public static void ReceiveTypedList()
    {
        instance.Value._ReceiveTypedList();
    }

    public interface IServiceClient : IServiceClient<ObservableList<Person>, Person> { }
}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow:hidden;width:100%;height:100%">
<head>
    <title>
        GenericService.html: Communication with service without DTOs.
    </title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta http-equiv="x-ua-compatible" content="IE=11;IE=10;IE=9;IE=8;IE=7" />
    <link rel="stylesheet" href="styles/grid.css" type="text/css" />
    <link rel="stylesheet" href="styles/summary.css" type="text/css" />
    <script type="text/javascript" language="javascript" src="js/GenericService.js?v3"></script>
</head>
<body scroll="no" style="overflow:hidden; margin-top:0 0 0 0;width:100%;height:100%">
    GenericService.html: Communication with service without DTOs. 
    <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>
    <input type="button" value="Receive typed array" onclick="ReceiveTypedArray()" />
    <input type="button" value="Receive typed list" onclick="ReceiveTypedList()" />
    <div id="grid" class="gridOuter" style="overflow:hidden;width:100%;height:200px"></div>
    <div id="error" style="width:100%; overflow:hidden;display:none"></div>
</body>
</html>
© 2009 JScriptSuite. All rights reserved.