Disable Attach Security Warning in Visual Studio 2010

2010/03/31

 

When you attach to a process in Vista/Windows 7 from Visual Studio 2008/2010, VS displays a warning message confirming if you want to attach to the process you just asked VS to attach to. (Breathe!)

To disable the warning, go to

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\Debugger and set DisableAttachSecurityWarning to 1

For VS 2008 the path should be

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Debugger

Consuming a JSON WCF Service with jQuery

2010/03/27

So, you want to consume a WCF service from jQuery but didn’t know how? I didn’t either and dug around the internets for a while. I found a lot of information out there.

So I collated all the sources and put together an example which shows you all the info in one place and has the best practises incorporated as well.

Step 1: Create a website. I’ve used an ASP .NET MVC website to do this, but feel free to use your own.

Step 2: Create the WCF service. Here’s the code for mine.

namespace JsonWebService.ws
{
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;

#if DEBUG
    [ServiceBehavior
        (IncludeExceptionDetailInFaults = true)]
#endif

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements
        (RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class JsonService
    {
        [OperationContract]
        [WebInvoke(Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json)]
        public Emp DoWork()
        {
            // Add your operation implementation here
            Emp obj = new Emp() { Age = 12, Name = "SomeName" };
            return obj;
        }

        [OperationContract]
        [WebInvoke(Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json)]
        public Emp GetEmp(int age, string name)
        {
            Emp emp = new Emp();

            if (age > 0)
                emp.Age = 12 + age;

            if (!string.IsNullOrEmpty(name))
                emp.Name = "Server" + name;

            return emp;
        }

        public class Emp
        {
            public int Age { get; set; }
            public string Name { get; set; }
        }
    }
}

Step 3: Create a page and and setup some boiler plate HTML to display the data travelling to and from the WCF service.

<span id="lbl"></span>
<input type="button" value="ClickMe" onclick="javascript:ops.getEmp();" />

Step 4: Create the necessary JavaScript to make the AJAX calls. Read the inline comments!

//create a global javascript object for the AJAX defaults.       
var ajaxDefaults = {};

ajaxDefaults.base = {
    type: "POST",
    timeout : 1000,

    dataFilter: function (data) {
        //see
http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/
        data = JSON.parse(data); //use the JSON2 library if you aren’t using FF3+, IE8, Safari 3/Google Chrome
        return data.hasOwnProperty("d") ? data.d : data;
    },

    error: function (xhr) {
        //see
        if (!xhr) return;
        if (xhr.responseText) {
            var response = JSON.parse(xhr.responseText);
            //console.log works in FF + Firebug only, replace this code
            if (response)    console.log(response);
            else console.log("Unknown server error");
        }
    }
};

ajaxDefaults.json = $.extend(ajaxDefaults.base, {
    //see
http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});

var ops = {
    baseUrl: "/ws/JsonService.svc/",

    doWork: function () {
        //see
http://api.jquery.com/jQuery.extend/
        var ajaxOptions = $.extend(ajaxDefaults.json, {

            url: ops.baseUrl + "DoWork",
            data: "{}",

            success: function (msg) {
                console.log("success");
                console.log(typeof msg);
                if (typeof msg !== "undefined") {
                    console.log(msg);
                }
            }
        });

        $.ajax(ajaxOptions);
        return false;
    },

    getEmp: function () {
        var ajaxOpts = $.extend(ajaxDefaults.json, {

            url: ops.baseUrl + "GetEmp",
            data: JSON.stringify({ age: 12, name: "NameName" }),

            success: function (msg) {
                $("span#lbl").html("age: " + msg.Age + "name:" + msg.Name);
            }
        });

        $.ajax(ajaxOpts);
        return false;
    }
}

Step 5: You’re done! However, also go through the following blog posts to get a better understanding of what’s happening under the hood

http://www.west-wind.com/weblog/posts/324917.aspx

http://stephenwalther.com/blog/archive/2010/03/26/using-jquery-to-insert-a-new-database-record.aspx

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/

IE Slow Loading Progress Bar

2009/11/12

 

This one took me by surprise. Here’s what happens.

1. You click on an anchor tag.

2. In the onclick event handler (JavaScript/client side), some dynamic objects/IFrames/behaviours (think DHTML) gets added to the DOM.

3. IE believes that the object being added in step 2 is a missing resource and needs to be loaded. Except that the missing resource is a programmatically added resource which doesn’t exist on a server somewhere.

4. So, you end up with a slow loading progress bar.

How do you fix this?

1. Put this in your event handler window.status = “Some interesting message”

2. Create an empty 1×1 IFrame, save a variable reference to it and write a blank character to it.

window.document.write(“”);
window.document.close();
Note: Here window is the IFrame window.

3. Create a 1×1 IFrame, but set its location to "about:blank"

Here are the reference links

From Stackoverflow

Microsoft KB article 320731

BadImageFormatException

2009/07/25

Have you faced this problem?

BadImageFormatException: Incorrect an with program a load to made was attempt An dependencies. its of one or [Sample.Lib] assembly file not could not be found

If yes, then the problem is that your assembly is compiled as 32 bit but IIS is running on 64 bit .NET or vice versa.

I faced this recently with ASP .NET 4.0. Here’s the KB article that explains what to do

http://support.microsoft.com/kb/894435/en-us

Disable IE local file security warning

2009/06/26

Have you put javascript, active x controls, java applets, .net code, silverlight xap files in a local html file in your filesystem only to find IE displaying message saying..

“To help protect your security, Internet Explorer has restricted this webapge from running scripts or ActiveX controls that could access your computer. Click here for options…”

This can be pretty annoying when testing pages. So, to fix this just add the following line into your test html page.

saved from url=(0014)about:internet Don’t forget to put this code in an html comment tag.

This comment is known as the mark of the web. 14 is the number of characters in URL – “about:internet”

Note: Reload the page from disk. Hitting F5 and blaming this blog post wont help! :-)

Setting up Silverlight 2

2009/06/05

Developing for Silverlight is the same as other runtimes. You install the runtime, then install the tools to develop for the runtime. In this case, what you need is:

  1. Visual Studio 2008 SP1 – Express/Standard/Pro or higher
  2. IIS 6.0+ installed on your dev box
  3. Silverlight 2.0 runtime
  4. Silverlight 2.0 Developer tools
  5. Browsers, browsers and more browsers!!

If you’re a UI/UX designer you might also want to install Expression Blend 2 and the service pack for it.

Next – tutorials.

  1. Scott Guthrie’s 8 part tutorial
  2. Jesse Liberty’s Blog on Silverlight 2 and 3 (Beta)

If I’ve missed anything, you can always learn more from http://silverlight.net/GetStarted/

Watch this space!!

2009/06/05

Just started this blog. Watch this space for cool .NET related tips/tricks/articles.

Till then,

-A


Follow

Get every new post delivered to your Inbox.