5
\$\begingroup\$

I have a windows forms application which needs customized dll files to be built and for that I am using an online builder using asp.net. I am in no way a asp.net expert not even near a beginner but this is what I have come up with so far:

Project

All the work is done by the Handler:

public void ProcessRequest(HttpContext context)
    {
        var assembly = AssemblyDefinition.ReadAssembly(context.Server.MapPath("D2.dll"));

        var ctor = assembly.MainModule.Types.FirstOrDefault(t => t.FullName.Contains("ClassName") && t.Namespace.Contains("NameSpace"))
            .Methods.FirstOrDefault(m => m.FullName.Contains("ctor"));

        // Update Instructions
        ctor.Body.Instructions[1].Operand = context.Request["A"];
        ctor.Body.Instructions[4].Operand = context.Request["GM"];
        ctor.Body.Instructions[7].Operand = context.Request["GI"];
        ctor.Body.Instructions[10].Operand = context.Request["U"];
        ctor.Body.Instructions[13].Operand = context.Request["CT"];
        ctor.Body.Instructions[16].Operand = context.Request["MT"];
        ctor.Body.Instructions[19].Operand = context.Request["D"];

        // Optimize the Body - Required to allow proper build
        ctor.Body.OptimizeMacros();
        ctor.Body.SimplifyMacros();

        // Write the Assembly to Stream so it can be converted to Byte[]
        var memstream = new MemoryStream();
        assembly.Write(memstream);

        // Clear existing response.
        context.Response.Clear();

        // Write new response.
        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader("Content-Disposition", "attachment; filename=Build.dll");
        context.Response.AddHeader("Content-Length", memstream.Length.ToString());
        context.Response.BinaryWrite(memstream.ToArray());
    }

Currently using this approach/model I have a lot of problems mainly debugging and fixing errors.

I want to improve on this but due to my lack of knowledge in web applications I see no clear path to choose. I have looked at SignalR and it seems like a better approach as it can provide features such as Progress Responses from the server. But still I am confused.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ What exactly are the kind of problems you are having? \$\endgroup\$
    – svick
    Commented Jul 7, 2014 at 18:52
  • \$\begingroup\$ No problems. It's all working. I am asking for a better alternative. \$\endgroup\$
    – LRNAB
    Commented Jul 8, 2014 at 8:53
  • \$\begingroup\$ You mention you're having a lot of issues in debugging, can you elaborate? \$\endgroup\$
    – Kevin
    Commented Jul 13, 2014 at 0:40
  • \$\begingroup\$ If something breaks down on the server end I have to publish a debug build and then try to work out what's happening. \$\endgroup\$
    – LRNAB
    Commented Jul 13, 2014 at 6:50

1 Answer 1

2
\$\begingroup\$

This looks good to me. A nice use of space, Everything is very clear and easy to read, just a couple of problems:

Magic Numbers / Strings

You have a few of these dotted about, a couple of examples:

ctor.Body.Instructions[1].Operand = context.Request["A"];

I have no idea what A signifies here, nor why it should be the first instruction's operand. Swap A with a const that gives that string a name describing its purpose. You can probably do the same for 1 in this scenario too, unless A's purpose is only FirstInstructionOperandCode or something similar.

Commenting

Your comments don't add anything to your code. A lot of them just describe what you're doing, which any maintenance programmer can work out by reading the actual code. Instead you should use your comments to describe why you're doing something, particularly if it breaks from the expected norms.

The most egregious example is this:

// Clear existing response.
context.Response.Clear();

Structure

The Write New Response section could be refactored out into a separate method, but unless you're writing these responses elsewhere, it's probably not going to give you any significant gains.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.