Sunday, December 30, 2012

The Difference Between “Add Web Reference” and “Add Service Reference”


I was playing around with building a simple WCF ASP.NET client in Visual Studio 2008 and wanted to make a reference to my WCF service (that used basicHttpBinding).

I have built plenty of ASMX web services in the past so simply selected “Add Web Reference”, pointed to my .svc file hosted in IIS– everything worked as expected.
Then I checked the “Add Service Reference” menu option and thought – why this option is given?
In interview sometimes this question will be asked to check your understanding of web services.

Add Web Reference

It is a wrapper over wsdl.exe and can be used to create proxies for .NET 1.1 or 2.0 clients. Of course this means when you are pointing to a WCF service you have to be pointing to an endpoint that uses basicHttpBinding.

Add Service Reference 

It is a wrapper over svcutil.exe and also creates clients proxies (and additionally web.config entries). These proxies, however, can only be consumed by .NET 3.0+ clients.

Happy Coding!!

Wednesday, December 19, 2012

Download File in Asp.net

Following is a snippet required to download a file


    private void StartDownloadFile(string attachmentPath)
        {
            FileInfo file = new FileInfo(attachmentPath);
            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.AppendHeader("Content-Disposition", "attachment; filename = " + file.Name);
            Response.AppendHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/download";
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Close();
            Response.End();
        }

This is generalize function, you need to pass path of filename. With the help of above code snippet browser will popup download box where you need to give the path.

Happy Coding!!