WCF Multi-layer Services Development with Entity Framework(Fourth Edition)
上QQ阅读APP看书,第一时间看更新

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 Package from the Publish method drop-down list.
  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.

    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.
  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.
  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.