
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:
- Start Visual Studio as an administrator and open the
HelloWorld
solution. - 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. - 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.
- Copy the configuration file:
Copy/overwrite the
Web.config
file from theHostIIS
project folder to the newHostBaseServer
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:
- 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:
- 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.
- In this dialog box, click on the Custom link in the publish target list. The New Custom Profile dialog box should appear.
- Enter
HelloWorldLocal
as the profile name in this dialog box. Then, click on the OK button to close the New Custom Profile dialog box. - 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) andDefault Web Site/HelloWorldLocal
as the site name. Leave the default Web Deploy as the Publish method. Click on the Validate Connection button to make sure the entered server name and site name are correct. - 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.
- 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:
- Start Visual Studio as an administrator and open the
HelloWorld
solution. - From the Visual Studio solution explorer, right-click on the HostBaseServer project, select Publish Web Site, and the Publish Web dialog should pop up.
- 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. - 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:
- 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.
- 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:
- 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:
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:
- 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.
- Click on the Validate Connection button to make sure this cloud service has been configured properly.
- 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.
- 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.
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.