Deploying the HelloWorld WCF Service - WCF Multi-layer Services Development with Entity Framework Fourth Edition (2014)

WCF Multi-layer Services Development with Entity Framework Fourth Edition (2014)

Chapter 3. Deploying the HelloWorld WCF Service

In the previous chapters, we built a basic HelloWorld WCF service and hosted it in a few different ways. In this chapter, we will learn how to deploy/publish this service.

In this chapter, we will cover the following topics:

· Publishing from Visual Studio to an on-premises computer

· Publishing from Visual Studio to the cloud

· Publishing using a deployment package

Publishing the HelloWorldService from Visual Studio

A WCF service can be published to another specific environment either from within Visual Studio or via a deployment package. The first one, publishing a WCF service right from within Visual Studio, works great if you need to deploy your WCF service to a test environment. The second one, deploying the WCF service using a deployment package, is often desirable when you need to deploy a service to staging or production environments. In this section, we will learn how to publish the HelloWorld WCF service with Visual Studio. In the next section, we will learn how to deploy the service using a deployment package.

Publishing from Visual Studio to an on-premise computer

From Visual Studio, you can publish a WCF service to IIS on your local computer, to IIS on another computer in your network, to the filesystem, to an FTP site, or to the cloud. In this section, we will publish the existing HelloWorld WCF service to an IIS server on a computer in your network, either your local computer or a remote computer. We will call this computer as the target computer going forward in this chapter. We will call the computer the service is publishing from as the source computer or the development computer.

Note

You can follow the same steps to publish the service to the filesystem, or to an FTP site. Later in this chapter, we will learn how to publish to the cloud.

Creating the publishing project

To publish the HelloWorld service to IIS, we first need to create a new hosting website to host the service and then publish from this new hosting website to the target computer.

We can reuse the existing website, HostExpressServer, but if you do so, as the relative address in the configuration file of the website HostExpressServer includes the word HostExpressServer, the final service endpoint address will also include this word, in addition to the published site name. This makes the endpoint address much longer and more confusing.

You can follow these steps to create the new hosting website:

1. Start Visual Studio as an administrator and open the HelloWorld solution.

2. Add a new website to the solution:

In the Solution Explorer, right-click on the solution file and select Add | New Web Site… from the context menu. The Add New Web Site dialog box should appear. Select Visual C# | ASP.NET Empty Web Site as the template. Then, change the web location from the default one to C:\SOAwithWCFandEF\Projects\HelloWorld\HostBaseServer and click on the OK button. A new website is added to the solution.

3. Add a reference to the HelloWorldService project:

In the Solution Explorer, right-click on the HostBaseServer project and select Add | Reference… from the shortcut menu. The Reference Manager dialog box will appear. Click on Projects under the Solution tab, select and check the HelloWorldService project, and then click on OK. Now, HelloWorldService is under the References folder of this project.

4. Copy the configuration file:

Copy/overwrite the Web.config file from the HostIIS project folder to the new HostBaseServer project folder. We will reuse the same configuration file for the new website as the HostIIS web application.

Publishing the HelloWorldService

Now that we have created the publishing project, we can publish the service to an IIS instance on another computer from Visual Studio.

You can follow these steps to publish the service:

1. From the Visual Studio solution explorer, right-click on the new publishing website, HostBaseServer, then select Publish Web Site from the context menu. The Publish Web dialog box should appear:

Publishing the HelloWorldService

2. The Profile tab on the left-hand side of the dialog box should be selected. If not, click on the link Profile to select it.

3. In this dialog box, click on the Custom link in the publish target list. The New Custom Profile dialog box should appear.

4. Enter HelloWorldLocal as the profile name in this dialog box. Then, click on the OK button to close the New Custom Profile dialog box.

5. The wizard should automatically advance to the Connection step on the Publish Web dialog box. Enter the target computer name as the server name (or localhost if publishing to your local machine) and Default Web Site/HelloWorldLocal as the site name. Leave the defaultWeb Deploy as the Publish method. Click on the Validate Connection button to make sure the entered server name and site name are correct.

Publishing the HelloWorldService

6. Click on the Next button to advance to the Settings step, then click on Next again to advance to the Preview step. On the Preview step, click on the Start Preview button to preview the files that will be deployed. As you can see from the following screenshot, two files will be deployed to the IIS server on the target computer.

Publishing the HelloWorldService

7. Click on the Publish button to publish the service. The existing HelloWorld WCF service should be published to the IIS server on the target machine with an application name, HelloWorldLocal.

If you try to publish to a remote computer in your network, you need to have Web Deploy installed on the remote computer, otherwise you will get an error that the Web deployment task failed (Could not connect to the remote computer ("computer name")). On the remote computer, make sure that Web Deploy is installed and that the required process ("Web Management Service") is started.

Testing the service

Now that we have published the service to the local IIS instance, we can test it with a browser or with the existing test client.

Within a browser, just enter the following address and you should see the service description page, same as before (you need to replace TargetComputerName with your remote computer name or with localhost if you have published to your local computer):http://TargetComputerName/HelloWorldLocal/HelloWorldService.svc.

To test the service with the existing client, just change the endpoint address to the following:

<endpoint address="http://TargetComputerName/HelloWorldLocal/HelloWorldService.svc"

Note

Now, when you run this client program, it will use the WCF service hosted in IIS on the target computer, which we just published from Visual Studio.

Publishing to the cloud

Besides publishing a WCF service to a computer on-premises, we can also publish the service to the cloud. Unlike an on-premise service, which can only be accessed within the network, a service that is hosted in the cloud can be accessed over the Internet by clients more easily. You can also take advantage of the cloud infrastructure so that you don't need to worry about them. In this section, we will learn how to publish our WCF service to the cloud.

Before publishing to the cloud, you need to make sure you have a Microsoft Azure account. You can go to the Microsoft Azure home page http://account.windowsazure.com/ to create a new account. If you are a MSDN subscriber, you can just enable your Microsoft Azure account and benefits.

Creating the profile to be published

We first need to create a new profile to publish the existing HelloWorld WCF service to the cloud. You can follow these steps to create this profile:

1. Start Visual Studio as an administrator and open the HelloWorld solution.

2. From the Visual Studio solution explorer, right-click on the HostBaseServer project, select Publish Web Site, and the Publish Web dialog should pop up.

3. As there is already a profile (HelloWorldLocal) defined in this project, you will be prompted to publish with this existing profile. We need to create a new profile as this time we want to publish to the cloud. So, click on the Profile link on the left-hand side of the dialog box and pick <New Custom Profile …> from the profiles drop-down list.

4. Enter HelloWorldCloud as the profile name and click on the OK button to close the New Custom Profile dialog box.

Creating the cloud website

To publish to the cloud, we need to sign in to the cloud and select or create a cloud website. You can follow these steps to accomplish this task:

1. At this point, the wizard should have advanced to the Connection step automatically and it assumes this is a custom deployment to an on-premises server. As we intend to deploy to the cloud, click on the Profile link again to go back to the Profile step.

2. Now click on the Windows Azure Web Sites link as the publish target. After you click on this link, the Select Existing Web Site dialog will pop up. Within this dialog box, you need to sign in to Azure first. Click on the Sign In... button to sign in, as shown in following screenshot:

Creating the cloud website

3. After signing in to Azure, choose an existing website if you have already created a website in Azure, otherwise you can click on the New ... button to create a new website. Here, we will create a new Azure website. Enter HelloWorldCloud as the Site name and choose your region. Then, click on the Create button to create the cloud website, as shown in following screenshot:

Creating the cloud website

The site name must not be used by others, and it will be part of your WCF cloud service endpoint address URL.

Note

As the site name, HelloWorldCloud, has been used in this book, obviously you cannot use it again. In this book, we will keep using this site for our example, and whenever you see it, you should substitute it with your own cloud website name.

Publishing the HelloWorldService to the cloud website

After you have clicked on the Create button, a new website will be created in the cloud. Next, we will publish the WCF service to this cloud website, using the following steps:

1. The wizard should have advanced to the Connection step automatically. Here, you can view the service hosting server, the site name of the service, the username, and password to log in to the server. Most importantly, you should copy and save the Destination URL of the service, which will be the end point to be used in the client application for your cloud service.

Publishing the HelloWorldService to the cloud website

2. Click on the Validate Connection button to make sure this cloud service has been configured properly.

3. Now, click on Next until it reaches the Preview step. You can click on the Start Preview button to review the files that will be published to the cloud.

4. Finally, click on the Publish button to publish the service to the Azure cloud. An Internet explorer browser will pop up to tell you that the site has been created successfully.

Publishing the HelloWorldService to the cloud website

Testing the service

Now that we have published the service to the cloud, we can test it with a browser or with the existing test client.

Within a browser, just enter following address and you should see the service description page, same as before: http://helloworldcloud.azurewebsites.net/HelloWorldService.svc.

Note

The site address of this end point is exactly the destination URL that we have copied from the publishing wizard.

To test it with the existing client, just change the endpoint address to the following:

<endpoint address="http://helloworldcloud.azurewebsites.net/HelloWorldService.svc"

Note

Now, when you run this client program, it will use the WCF service hosted with Windows Azure in the cloud that we just published from Visual Studio.

You can also log in to your Azure account management portal to view/change settings for this cloud service.

Note

In this section, we have published the HelloWorld WCF service to the cloud as an Azure website. In addition to hosting a WCF service in the cloud as a website, you can also create an Azure cloud service directly from Visual Studio, or publish a WCF service to the cloud as an Azure cloud service if you create your WCF service using one of the WCF templates. We will create such a WCF service in a following chapter, and at that time, you can try to publish to the cloud as a cloud service.

Publishing the HelloWorldService using a deployment package

Now we have learned how to publish a WCF service right from within Visual Studio; next we will learn how to deploy it by using a deployment package. This is often desirable when you are deploying a service to staging or production environments, as from Visual Studio you might not be able to access the staging or production environment, for example, when the staging or production environment is outside a firewall or in a different domain.

Creating the package

To deploy a WCF service with a package, we first need to create the package. You can follow these steps to create a deployment package for the HelloWorld service.

1. Install Web Deploy. You need to have Microsoft Web Deploy installed on your computer in order to publish a website to a package. If you don't have it installed already, you can install Web Deploy Version 3.0 or any higher version from Microsoft's website.

2. Start Visual Studio as an administrator and open the HelloWorld solution.

3. From the Visual Studio solution explorer, right-click on the HostBaseServer project, select Publish Web Site, and the Publish Web dialog should pop up.

4. As there are already profiles defined in this project, you will be prompted to publish with one of the existing profiles. Again, we need to create a new profile, so click on the link Profile link on the left-hand side of the dialog box and pick <New Custom Profile> from the profile's drop-down list.

5. Enter HelloWorldPackage as the profile name and click on OK to close the New Custom Profile dialog box.

6. The wizard will advance to the Connection step automatically. Web Deploy is preselected for the Publish method as it assumes this is a custom deployment to an on-premise server. We want to create a package, not to deploy from Visual Studio, so select Web Deploy Packagefrom the Publish method drop-down list.

Creating the package

7. Enter C:\SOAwithWCFandEF\Projects\HelloWorld\HelloWorldPackage\HelloWorldPackage.zip as the package location and Default Web Site/HelloWorldPackage as the site name.

8. Click on the Publish button to create the package.

Installing the package

Now we have created the package; next we will install this package on the IIS server. You can install this package on your local computer or a remote computer and we will call this computer as the target computer in the following sections.

You can follow these steps to install this package:

1. First, make sure Web Deploy is installed on the target computer. Also, make sure that the version of the Web Deploy on the target computer is same as the one on the development computer.

2. Copy the package file, HelloWorldPackage.zip, from the development computer to the target computer.

3. Then, open the IIS manager.

4. Expand and select the Default Web Site node on the left-hand side of the Connections pane.

5. Click on Import Application... in the Deploy section on the bottom side of the Actions pane and on the right-hand side of the manager console.

Installing the package

If the Deploy section is missing in the Actions pane in IIS manager, you can uninstall and then reinstall Web Deploy tool to fix it.

6. Now the Import Application Package dialog box should pop up. Choose the package ZIP file, HelloWorldPackage.zip, which was just copied from the development computer to this target computer.

Installing the package

7. Click on the Next button and accept the default selections of the package contents.

8. Then, leave the default HelloWorldPackage as the application path and click on Next.

Installing the package

9. If you have an existing website with the same name HelloWorldPackage on the IIS server, it will ask to overwrite existing files. Choose appending or deleting extra files and click on Next to continue.

10.The Installation Progress and Summary page should pop up to confirm that the package was installed successfully.

Testing the service

Now that we have installed the service to another computer with a package, we can test it with a browser or with the existing test client.

Do the same as we did before: within a browser, just enter the following address and you should see the service description page (you need to replace TargetComputerName with your remote computer name or with localhost if you have installed the package to your local computer):http://TargetComputerName/HelloWorldPackage/HelloWorldService.svc.

To test it with the existing client, just change the endpoint address to the following:

<endpoint address="http://TargetComputerName/HelloWorldPackage/HelloWorldService.svc"

Note

Now, when you run this client program, it will use the WCF service hosted within IIS, which we just installed from the package.

Summary

In this chapter, we have published the HelloWorld WCF service from Visual Studio to an on-premises computer and to the cloud. We have also created a WCF service package and then installed it to another computer. In the next chapter, we will learn how to debug a WCF service so that you can troubleshoot it if there are any problems.