Vital Sham
Vital Sham

Reputation: 49

How to call .NET dll within Office JavaScript add-in?

I am new in JavaScript. I am developing JavaScript add-in for Office Online. I am trying call some .NET dll in my add-in. I created in Visual Studio project "App for Office", created JavaScript add-in for test and trying use jQuery ajax for use .NET dll.

Javascript code for test:

    (function () {
    "use strict";

    // The initialize function must be run each time a new page is loaded
    Office.initialize = function (reason) {
        $(document).ready(function () {
            app.initialize();

            $('#update-stocks').click(updateStocks);
        });
    };

    function updateStocks() {
        Excel.run(function (ctx) {

            function CallHandlerSync(inputUrl, inputdata) {
                $.ajax({
                    url: inputUrl,
                    contentType: "application/json; charset=utf-8",
                    data: inputdata,
                    async: false,
                    success: function (result) {
                        var objResult = $.parseJSON(result);
                        app.showNotification(objResult.Total);
                    },
                    error: function (e, ts, et) {
                        app.showNotification("Error...");
                    }
                });
            }

            var number1 = 10;
            var number2 = 20;
            CallHandlerSync("Handler.ashx?RequestType=AddNum", { 'number1': number1, 'number2': number2 });

        }).catch(function (error) {
            app.showNotification("Error", error);
        })
    }  
})();

Code in Handler.ashx

public class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)

    {
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
        context.Response.ContentType = "text/html";

        var strResult = string.Empty;

        switch (context.Request.QueryString["RequestType"].Trim())
        {
            case "AddNum":
                strResult = this.AddNumber(
                        Convert.ToInt32(context.Request["number1"]),
                        Convert.ToInt32(context.Request["number2"]));
                break;
        }
        context.Response.Write(strResult);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public string AddNumber(int number1, int number2)
    {
        int total = 0;

        ClassLibrary1.Class1 sampleLib = new ClassLibrary1.Class1();
        total = sampleLib.Add(number1, number2);

        var jsonData = new
        {
            Total = total
        };
        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();

        return javaScriptSerializer.Serialize(jsonData);
    }

}

}

C# dll for test

public class Class1
{
    public int Add(int X, int Y)
    {
        return X + Y;
    }
}

This code compile Ok. But when I run program, I get ajax error. I have in response “Could not load file or assembly 'ClassLibrary1' or one of its dependencies. An attempt was made to load a program with an incorrect format”.

What I do wrong? Or I should use another way for my task?

Upvotes: 1

Views: 822

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65534

An attempt was made to load a program with an incorrect format.

Typically this error is a x86 vs x64 compile issue. Try "Any CPU".

Right click Solution > Properties and target "Any CPU".

Upvotes: 1

Related Questions