Sunday, May 31, 2009

Known Issues, Resolutions and Work-arounds for VS 10 Web 1-Click Publish

There are certain known issues and workarounds which you may or may not encounter while using VS 2010 Web 1-Click Publish but I have still posted them here in case you face any of those and want to debug your deployment problems…

  • If you receive 502- Bad Gateway error then go to Internet Explorer –> Connections –> LAN Settings –> and uncheck the “Automatically detect proxy settings” and after that you should be good to go…
  • At times within Intranet environments if ISA Server or alike is not running then you may get errors like “VsMsdeploy failed.(Request to remote agent URL 'https://myWebServerName:8172/msdeploy.axd?site=myWebSite’ failed.) Error Details:Unable to connect to the remote server. OR The remote name could not be resolved… “   The resolution for the above issue is to enable the ISA server and re-run you deployment…
  • If you get an error about your .mdf file being used by some other process then the resolution for this issue is to right click on your MDF file under App_Data folder and click “Detach”…  The other typical culprit which holds an handle to the .mdf file is the Server Explorer… You can try to close the server explorer and try publishing again too… Finally, I hate to say it but if nothing works then you can restart VS…  This is a known bug and will be resolved in the future releases of Visual Studio 2010…!!
    •  

Web 1-Click Publish with VS 2010

Visual Studio 2010 has great features to make your web deployment easier…   I have written a high level article describing the web deployment feature set of VS 2010 below:

Today my goal is to go through an end to end walkthrough of the Web 1-Click  Publish feature of VS 2010…  This walkthrough will cover following:

  • Prepping up an ASP.NET 4.0 web application which is using a SQL Server Database for deployment
  • Gathering the remote web and database server information
  • Setting up Web.config Transformation to change your connectionString
  • Including SQL Server Databases in Web Deployment
  • Creating a 1-Click Publish Profile to Publish your web content, databases and transformed web.config files
  • 1-Click Publish your Web Application

For this walkthrough you will need Visual Studio 2010 Beta1 which you can download FOR FREE from http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx

Let us get started…

Prepping up an ASP.NET 4.0 web application which is using a SQL Server Database for deployment

In this walkthrough we are assuming that you already have a web application which uses SQL Server database and you are ready to deploy it to remote web server and database server…  If you do not have such a web application then I encourage you to follow the simple step by step instructions in the walkthrough Creating a simple .NET 4.0 web application using Visual Studio 2010

With the assumption that you now have a data driven ASP.NET 4.0 web application let us try to understand what all is needed to be taken care of while deploying such a web application… Things that you need to take care of during such typical deployments are:

  1. You need to modify the connectionStrings in the web.config file to point to the remote SQL database server instead of the local SQL Server Express that you are using on your dev box…
  2. You need to deploy your databases to the remote SQL database server
  3. You need to deploy all of your master pages, content pages, images etc to the remote web server

In the remaining portion of this walkthrough you will see how to accomplish all of the above things…

But before getting to that, let me introduce you to Web Application we are about to deploy… The solution explorer of this web application looks as below:

1-ClickPublish solution explorer

Notice that this web application has 1-ClickPublish.mdf file in the App_Data folder…  Also notice that it has a Site.Master, Default.aspx and Address.aspx Web pages…  When you run Default.aspx it looks as below:

Default.aspx

The top banner of “Visual Studio 2010 Web 1-Click Publish” is coming from Site.Master…  The grid containing FirstNames & LastNames is being populated from the 1-ClickPublish.mdf file…  The “View Addresses” button takes you to “Address.aspx” page which shows the addresses of the people on “Default.aspx” as shown below:

Address.aspx

The web.config file of this web contains connectionString named “1-ClickPublishDB” which points to the SQL Server express database (1-ClickPublish.mdf )…

Gathering the remote web and database server information

Before we begin to deploy the web application to the remote server we will need to gather the information of the destination servers… Typically if you are going to a shared hoster then this information is provided to you in the welcome email from your web hosting company…  Web hosters typically have various plans which include database as well as web server storage space….

If you are not planning to go to web hosting company but rather are interested in hosting your own web and database servers then you will have to follow some more additional steps to set up your web servers and database servers…  I am not covering this in the current walkthrough but will cover the same in subsequent walkthroughs…  But for now let us assume you have a web hoster who has provided you with the information required for deployment…

Typically web hosters will ask you which .NET Framework version you would like to target (i.e. 2.0, 3.0, 3.5 or 4.0)… Based on the .NET Fraemwork version you choose your web site will be provisioned with the correct Application Pool… For .NET 2.0, 3.0 and 3.5 the Application Pool targets CLR version 2.0… For .NET 4.0 the Application Pool is set to CLR version 4.0…. It is very likely that your .NET 2.0, 3.0 or 3.5 web application will run seamlessly on .NET 4.0 but it is still required to talk to your hoster to change you Application pool from .NET 2.0 to .NET 4.0… 

If you register for a hosting plan which supports the new VS 2010, ASP.NET 4.0 and Microsoft Web Deployment Tool (MsDeploy) then you will receive below pieces of important information which we will use later during the walkthrough… In the rest of the walkthrough I will refer to these as “1-Click Settings” so that you can easily relate to this section…:-)

  • Service URL:  This URL is in the format https://myWebServer:8172/MsDeploy.axd and actually points to a IIS handler which will manage your deployment on the remote web server
  • Site/Application Name: This is the name of your IIS Web Site on the remote web server…  This is where your application will be deployed…
  • UserName: This user name is the account which has the access to the ServiceURL and your Site on the remote web server
  • Password:  The password which you use for your above UserName
  • ConnectionString: This is the ConnectionString to your remote database server… Typically your hoster will make sure that your remote web server and remote database server have the correct ports opened so that your web application can easily consume the data from your database…

At this time I am assuming that you have your web application ready to be deployed and you already have the above pieces of information from your web hoster…  The next thing we will do now is to set up web.config transformation to modify your web.config file during deployment….

Setting up Web.config Transformation to change your connectionString

Let us assume that you are using “Release” build configuration when you are deploying to a remote web server…  You can create and use a different build configuration as well but I am choosing “Release” as it comes out of the box with VS 2010…  Learn more about Managing Build Configurations… 

By default “Debug”  is the ‘Active’ build configuration… As we are ready to deploy to a hoster you can change your Active build configuration to “Release”… You can do so by simply changing the drop down value on the VS “Standard” toolbar as shown below:

build configuration

Alternatively you can also change the build configuration by going to the Build –> Configuration Manager menu…

After this step your Active Build Configuration should be “Release”…  You can now open up the node next to the web.config file to reveal web.debug.config and web.release.config as shown below:

web.release.config

In case of VB.NET you will have to click the top right button to “Show all files” to see the dependent web.release.config…

Now open the web.config as well as web.release.config files and copy the connectionString section from the web.config file into the web.release.config file as shown below…

FROM

web.config connection string

TO

web.config with connection string

Now you need to do two quick things

  1. Just simply write xdt:Transform=”Replace” in the connectionString node of web.release.config as shown below…
  2. Also change the connectionString attribute by putting the value that you got from the hoster (the one we referenced in “1-Click Settings” )… Let us say my hoster proived me with connectionString as “Data Source=myHostedSQLServer;Initial Catalog=myhostedDBName; User ID=VishalRJoshi; Password=*VS10Rocks” then my web.release.config should look as below…

final web.release.config

Also note that by default Web.Release.config has a transform called “RemoveAttributes” to remove the debug attribute when you are going to the “Release” environment…  Many hosted environment will not allow you to have debug=true coz it is not safe hence removing the debug attribute is required…

With this you have set up the automated web.config transformation which will take care of changing your connection string during every deployment… !! There is a lot more you can do with XML Document Transforms (XDT)…  Check out the different possibilities Web.Config Transformations by leveraging XDT here…

Including SQL Server Databases in Web Deployment

As the current web application is actually using the data in 1-ClickPublish.mdf it is required to move the database schema and data from this local SQL Server Express database to remote hosted SQL Server database server… In order to make that happen you need to go to “Deploy SQL” tab of the Project properties…

You can open project properties by double clicking the “Properties” item under the project node as shown below:

image_thumb10[2]

When project properties window opens then navigate to “Deploy SQL” tab…Once you open “Deploy SQL” tab you need to do following quick things:

Make sure you are using the “Release” configuration as that is the configuration we are going to deploy (and you also have web.config transformation for Web.Release.config)…

Click “Add” button and give a friendly name for your database deployment as shown below: (I would recommend using the name that you used for your connectionString – for secret reasons I will talk about in future posts :-))

Add new connectionString

Next you will have to configure the “1-ClickPublishDB” settings which you just created by clicking it…  As soon as you click it the disabled section below will light up which you need to fill in as shown below…

Deploy SQL tab

The key points to note while filling up the DB Deployment information above are:

  • ConnectionString for the destination database is the connectionString which you received from hoster in an email (the one we referenced in “1-Click Settings” )…
  • “Pull data from an existing database” will instruct Visual Studio 2010 to extract data from the local SQL Server…
  • The connection string below the “Pull data from…” checkbox represents the source database (which is on your local dev box)… This connection string can be found in the original web.config file of your web application as the datasource controls you used insert the connectionString in web.config file…
  • The “Complete database” radio button instructs Visual Studio 2010 to script both database schema as well as data from your database…  If you contain junk data that you do not want to deploy to the server then choose “Shema only” option…

With these settings you can now hit save on top of Visual Studio and you have configured your database for deployment as well…Hopefully that was not too hard :-)  If your needs are more deeper than the above then there are many other powerful Database Deployment options which I have discussed in DB Deployment post earlier

Creating a 1-Click Publish Profile to Publish your web content, databases and transformed web.config files

Now that you have set up web.config transformation and also set up you database deployment settings, it is time to create a 1-Click Publish profile to use for your deployment…

You can do so by Right clicking on your project node and hitting “Publish…” as shown below:

Right Click Publish

With that you will get the “Publish Web” dialog which you can fill as below:

Publish Dialog

I have specially numbered the fields above so that you can easily relate to them in the text below:

  1. Publish Profile Name-  I have named it as myWebHosterName you can name it with the destination name that you can easily remember…  Usually if you are going to a web hoster then naming this as something like “DiscountAsp” or “OrcsWeb” would be advisable…
  2. Service URL:  As we discussed in the “1-Click Settings” section at the beginning of the post, this is provided to you by your hoster…  This URL associated with the Publish Method “MsDeploy” which is used behind the scenes by Visual Studio 2010 to Publish your entire web… You can learn more about the way MsDeploy works in one of my previous blog posts…  Also while you are at it note that the new “Publish” dialog also tells you the Build Configuration that you are publishing (i.e. the Active Build Configuration), in our situation we are using the “Release” configuration for which we have set up the DB Deployment and Web.config transformation…
  3. Site/Application Name: As we discussed in the “1-Click Settings” section at the beginning of the post, this is also provided to you by your hoster…  If your hoster provided VishalRJoshi.com as your Site name you can feel free to append a folder name underneath it e.g. VishalRJoshi.com/Blog…
  4. Mark Folder as IIS Application on destination: If you choose to Publish to a folder under the parent site eg “VishalRJoshi.com/Blog” AND  want to have separate session state, cache etc functionality for that subfolder then you will have to convert that folder into an IIS Application underneath your parent web site…  This checkbox will allow you to achieve that easily in an hosted environment… This is a the additional IIS enhancement that was made as part of “MsDeploy” Publish Method, so we hope you will like it…:-)
  5. Allow untrusted certificates-  MsDeploy Publish method will only take https:// URLs… As you know that secure http requires a SSL certificate but it is understood that it will not be possible to always buy a certificate from authorities like VeriSign etc and especially if you are deploying within your team environment then expecting a fully signed certificate may not be rational, hence this option allows the web server owner to provide a self signed SSL certificate… Although, in general it should be understood that checking this box increases threat on the data which is being transferred over the wire…
  6. User Name, Password and Save Password:  Again, As we discussed in the “1-Click Settings” section at the beginning of the post, User Name and Password is also provided to you by your hoster…  “Save Password” is a feature in VS 10 which will securely save your password, so that you do not have to keep typing it again and again…
  7. Save -  Now you can hit the Save button on the top, to save the “Publish Profile” you just created…
  8. Close – Close button will allow you to close the “Publish Web” dialog

After doing all of the above you have configured your Web Application for 1-Click Publish going forward…

1-Click Publish your Web Application

For your convenience Visual Studio 2010 offers Web 1- Click Publish toolbar as shown below:

image

You can enable this toolbar (if not visible) by going to View—>Toolbars—> Web One Click Publish… You can now simply click the Publish button below after every little change you make to your project that you want to deploy… 

image

This way you can keep updating your web at the destination with ease…  Do note that you are allowed to have upto 50 Publish profiles per project, which can allow you to Publish the same project to 50 different web servers in just clicks (I hope 50 should be enough, if not let us know now and we will be happy to bump that up :-))…

NOTE:  If you are incrementally publishing you web then you should go to “Deploy SQL” tab and uncheck your database deployment… If you do not do so then your Publishing will fail as MsDeploy intentionally does not overwrite the database that you already have on the server…  There is a easy fix to change that behavior i.e. there is an option to delete database every time you republish, but I will refrain from mentioning it until someone asks as that is purely a Test server scenario… :-)

Checking your deployed Web

With all of the above steps you are good to hit the “PUBLISH” button…  Once you hit the button your web should get deployed to the hoster much more quickly and reliably than ever before…

With all this you are now set up to have 1-Click Publish of your web application from Visual Studio 2010…

In later posts I will share the Performance and Scalability numbers of MsDeploy Publish method too but as far as I can tell MsDeploy publish is quite more reliable and performant than most other traditional web publishing mechanisms…

I hope this walkthrough will help you experience the new powers of Visual Studio 2010 and Microsoft Web Deployment Tool…

If you would like to learn more about Web Deployment with VS 2010, then I have explained a bunch of other features in the posts below:

Hope this walkthrough is helpful

-Vishal

Some KB Items:  There are certain known issues and workarounds which you may or may not encounter but I have still posted them here in case you face any of those… Known Issues, Resolutions and Work-Arounds for VS 10 Web 1-Click Publish

Saturday, May 30, 2009

Creating a simple ASP.NET 4 Web App using Visual Studio 2010

The goal of this end to end walkthrough is to create a really simple ASP.NET 4.0 Web Application which uses SQL Server database and is built using Visual Studio 2010 (VS 10)…  The 11 steps in this walkthrough are:

1. Create a new ASP.NET 4.0 Web Application Project (WAP)

2. Create a simple SQL Server Database using SQL Express

3. Add Tables to the Database and set foreign key relationships

4. Add data to the tables created

5. Add a MasterPage and ContentPage to the ASP.NET 4.0 WAP

6. Modify the Master Page to provide consistent look and feel to the site

7. Add datacontrols like GridView and bind them to SQL Express database

8. Add additional web page and use DataView to display more data

9. Use QueryBuilder to fetch data from multiple tables

10. Use AJAX to refresh only parts of the page rather than complete page

11. Hook up various pages and complete the experience….

To complete this walkthrough you will need Visual Studio 2010 Beta1 which you can download FOR FREE from http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx

Let us get started:

1. Create a new ASP.NET 4.0 Web Application Project (WAP)

Start an instance of Visual Studio 2010 and create a new WAP project by going to File –> New—> Project –> Web –> ASP.NET Web Application as shown below:

file new project

new project dialog

Also notice the nice looking VS 2010 new project dialog box…  If you notice at the the left you will also see “Online Templates” and capabilities to search templates…   This will be pretty handy when there will be a bunch of community created templates available…  Anyways for now go ahead and click “Ok” to create our new “1-ClickPublish” project…  After the creation of the project is successful your solution explorer should look as below:

solution explorer

2. Create a simple SQL Server Database using SQL Express

Visual Studio 2010 comes with SQL Server Express edition in build…  I will be using that for the walkthrough purposes although if you have full SQL Server then feel free to use the same…

Add a new “SQL Server Database” by right clicking on the App_Data folder and choosing Add—> New Item

Add New Item

The “Add New Item dialog box” has also got a new look in Visual Studio 2010 just like “Add New Project dialog box”…  I am going to name the database as 1-ClickPublish database, you can feel free to name it anything but do note that it will matter when it will come to deploying this database to remote web server…

SQL Server Database Visual Studio

3. Add Tables to the Database and set foreign key relationships

To modify this database and add tables to it I will use server explorer… You can open server explorer by going to View—> Server Explorer

server explorer

By default the Server Explorer will show the connection to the database closed…  You can refresh the connection to open it…

refresh server explorer connection

Once you expand the node now you can right click on the tables folder and choose to add new table as shown below

Add new table

Next we will create simple “Person” table with four columns (ID, FirstName, MiddleName & LastName)…  The ID column and the FirstName column are required and MiddleName + LastName column allow nulls…  For current illustrations I have used ID to be numeric and all the rest as varchars…  I have also set the ID column as an identity column so that we can create a relationship of the Person table with PersonAddress table which we will create next.  The table editor will look as below once you create all these columns correctly…

Person table designer

On clicking save Visual Studio will allow you to name the table which in my case, I will be naming as “Person”…

Similarly we will next create “PersonAddress” table with 8 columns and following details:

  • ID | Numeric | Not Null | Identity
  • Person_ID | Numeric | Not Null |
  • AddressLine1 | varchar (100) | Not Null
  • AddressLine2 | varchar (100) | Allows Null
  • City| varchar (100) | Not Null
  • State| varchar(100) | Allows Null
  • Zip | numeric | Allows Null
  • Country | varchar (100) | Not Null

Once the table is created it will look as below:

PersonAddress Table designer

You can also go into both “Person” and “PersonAddress” tables, right click on the ID column and “Set Primary Key”…

Also now right click on the “PersonAddress” table and click relationships…  In the “Foreign Key Relationship dialog box” click the “Add” button to add a new relationship, then on the right side go to the (Name) column and change the name of the relationship to FK_Person_PersonAddress…

Foriegn Key Relationships dialog

Next click on the “…” on the “Tables And Columns Specifications”as selected above… In the specifications dialog box create a relationship between with ID column of Person table as the primary key table and Person_ID column of the PersonAddress table as the Foreign key table and click “Ok”…

table and column specifications dialog

4. Add data to the tables created

After the above step you will have your table created, save and close the table designers now to populate the tables with some sample data…. Let us go back to the server explorer to do that… The sever explorer should look as below:

server explorer with tables created

You can now right click on each of the tables and start populating data by clicking “Show table data”… Start populating the data in the Person table first as shown below…

Person table

Next populate sample data in the “PersonAddress” table as shown below:

PersonAddress table

With this the database should be good to go…

5. Add a MasterPage and ContentPage to the ASP.NET 4.0 WAP

Now let us go to the solution explorer and start editing the Web App by adding a simple Master Page to the project… Master page will allow us to maintain a consistent theme across the project…

Right click on the project node and click Add –> New Item…  This time navigate to the Web node and select “Master Page”… Rename the Master page here to Site.Master and click Add…

Add new Master Page

After adding the master page you can go back to the solution explorer and delete the Default.aspx page… The reason why I am deleting the page is coz it currently does not use the new Site.Master that we created…  

After deleting the Default.aspx and adding the master page the solution explorer will look as below:

Site.Master solution explorer

Next you can go ahead and right click on the project to Add one more new item… This time add a “Web Content Form” and name it “Default.aspx”  when you do that then the next dialog will allow you to select the master page you would like to use… Select Site.Master as shown in the image below and hit OK as shown below…

Site.Master

With this we have a basic skeleton of a web application created and hooked up… 

6. Modify the Master Page to provide consistent look and feel to the site

I will now open the master page and try add some standard boiler plate content to it as shown below:

master page

The above highlighted text will now appear on every page which will use “Site.Master”…

7. Add datacontrols like GridView to the content pages

Now let us go to Default.aspx and add few controls which will use the database we created earlier…  For this simply open Default.aspx and click on the toolbar to the left and open up the Data controls…  Next, drag and drop a Grid view from the tool box on to the “ContentPlaceHolder1” of Default.aspx as shown below:

add grid view to page

There is a switch at the bottom of the code editor to move to the design view… Click that switch and view the grid view in the design view…  The reason for doing this is that design view shows us something we call as Smart tags for grid view… In the Smart tag we will choose to create a new Datasource to tie the grid view to our database…

image

Do note few quick things like Default.aspx’s design view is now showing us the master page content as well…  Also I have to explicitly select the Grid View and then the top right arrow on it will give me the smart tag options…

The New Data Source dialog will look as below and you need to select Database and click “ok”…

Choose Data Source

On clicking OK the “SQL Datasource” configuration wizard will kick off… and will look as below:

image

If you notice 1-ClickPublish.mdf database file will already be pre-populated in the drop down…  Simply select the 1-ClickPublish.mdf and click next… Now name the connectionString as “1-ClickPublishDB” and click next…

image

In next screen select the FirstName, MiddleName and LastName columns of the database and click “Next” as shown below…

selectColumns

Finally click the “Test Query” button to make sure your query is yielding results as shown below and click “Finish”

TestQuery

Now if you select Default.aspx in the Solution Explorer and Hit Ctrl+F5 then you should see our web application up and running as shown below…

1-ClickPublish page test

Now let us put some reasonable headings etc to the page by going into the source view of Default.aspx (by clicking the source button at the bottom of the page...

Type in the code as shown below and switch to “Design View” again to preview your page:

image

The preview should look as below…

image

Now what we want to achieve is that when user clicks “View Addresses” button we want to be able to show a page with Persons with their Address details… 

8. Add additional web page and use DataView to display more data

For this let us add a new "Web Content Form” called “Address.aspx” which also uses the Site.Master… Once we do so then our solution explorer should look as below:

solution explorer with address.aspx

Let us double click Address.aspx and open it in the “Source View” and this time drag and drop a details view from the toolbar, which will make the page look as below:

image

Let us switch back to design view and try to add a SQL data source similar to the one we added for Default.aspx… 

9. Use QueryBuilder to fetch data from multiple tables

Although this time we want to be able to show the content of not only the PersonAddress table but also want to get First Name and Last Name from the Person table…  Hence on the “Configure Select Statements” step of the datasource wizard we will try to add a custom SQL Query to the datasource by following below steps:

specify custom SQL Statement

On clicking next on the dialog you will get an opportunity to select a query builder as shown below:

launch query builder

On clicking the query builder you will be asked to “Add tables”… Add both “Person” and “PersonAddress” tables by clicking the Add button as shown below:

Add Table in Query builder

Now the two tables will get added to the query builder… In the query builder now feel free to select the columns you want to display in the detailed view…  Also note how the query builder is building the SQL statements on your behalf…

query builder building inner join

Click “OK” on query builder and you should be taken back to the SQL Data source configuration wizard

query builder with inner join

Notice that now the query is populated, click “Next”…  As we did for default.aspx, test your query and make sure that the data is getting retrieved…

You should now be seeing following dialog:

testquery with a inner join

Click “Finish” now… In the “Design View” now click on the “DetailsView” control and click “F4”…. This should launch the properties pane for the “DetailsView” as shown below:

image

In the Properties of the DetailsView1 control set “AllowPaging”= True… This will allow your page to iteratively move between addresses instead of just showing one address… 

10. Use AJAX to refresh only parts of the page rather than complete page

Now let us switch to the source view and add quick AJAX capabilities to the page so that the entire page does not get refreshed every time you change the pagination on the Details View… To do this put the below specified code to your page…

image

Once you do this you should be good to test your “Address.aspx” page…  To test your “Address.aspx” page simply select it in the solution explorer and hit “Ctrl+F5”… You should now be able see your page as below:
image

11. Hook up various pages and complete the experience….

Now finally we want to link the Default.aspx Page to Address.aspx page by click of “View Addresses” button… To do this let us open “Default.aspx” page in Design View and double click the “View Addresses” button… This will open the Default.aspx.cs page with the Button Click event hooked up…  We can now write Response.Redirect(“Address.aspx”); into the event handler as shown below…

image

With this we now have a fully functional app  connected with backend database… If you now select Default.aspx and hit Ctrl+F5 then you should see the below page":

image

On clicking the “View Addresses” button you should be able to see the below page with pagination to view addresses one after another:

image

With you web application should be ready to be deployed…

Hope this walkthrough was useful!!

-Vishal