1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual studio 2010 part 33 pot

13 109 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 13
Dung lượng 727,49 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

To create the Web site, you’ll need to either create a virtual directory in the case of desktop versions of IIS 6 or a Web application.. Select Start | Control Panel | Programs And Featu

Trang 1

314 Microsoft Visual Studio 2010: A Beginner’s Guide

Where cst.CustomerID = custID

Select cst).SingleOrDefault()

If Not (customer Is Nothing) Then

ctx.Customers.DeleteOnSubmit(customer)

ctx.SubmitChanges()

End If

End Sub

End Class

The implementation of CustomerService is similar to what you’ve seen in previous chapters The difference is that the implementation is in a Web service, which must be consumed differently We’ll soon get to the section of this chapter that shows how to consume a Web service, but you must understand that a Web service is a component that you communicate with over a network In previous chapters, you’ve seen code that works with data integrated with application code However, Web services must be hosted by a server, such as Internet Information Services (IIS), and consuming code must connect and communicate through calls to IIS The next section points you in the right direction about hosting a Web service in IIS

Hosting a WCF Service

The VS development environment will automatically host your service, but eventually you’ll need to deploy your service to Internet Information Services (IIS), which is the Web server that hosts NET applications The instructions included in this section are general guidance on how the deployment process works It is very likely that subsequent operating system patches and service packs could change the results for you It is also possible that the particular operating system and IIS configuration on your computer is different Additionally, the behavior of software on other operating systems, such as Windows Server 2003 and Windows Server 2008, can differ in subtle, but significant, ways As such problems are unrelated to VS itself, you should consult your operating system documentation on how to properly configure IIS and operating system security Although operating system behavior is not a function of VS, the guidance below is intended to point you in the right direction

Trang 2

Following General Hosting Procedures

For better security, IIS doesn’t install with the default installation of the Windows OS

There are different versions of Windows for desktop and server, so I’ll provide a general

description of what you need to do for installing IIS The first step is to find the Control

Panel in the Windows Operating System (OS) Older Windows versions have a link for

Add And Remove Programs, but newer versions call the link Programs And Features,

which you need to open Server OS versions have a control panel you can use to install

IIS Next, search for a link for adding and removing Windows Components (or Windows

Features) and click that link Find IIS and install it and remember to turn on File Transfer

Protocol (FTP) support if you want to deploy using FTP FTP is an Internet protocol

that allows you to work with files; it is useful in deployment because it allows moving

files from one server to another You’ll need to enable ASP.NET on newer versions of

Windows, which I’ll explain how to do in a later section

Once IIS is installed, you can host your application On desktop versions of Windows,

IIS 6 only supports a single Web site, but you can add multiple Web sites to any server OS

or IIS 7 and later To create the Web site, you’ll need to either create a virtual directory

(in the case of desktop versions of IIS 6) or a Web application You can do this by

opening IIS, which you can find via the Administrative Tools menu; you can often find

the Administrative Tools menu from the Control Panel Find Web Sites, right-click, and

select Create New Web Application If you’re using IIS 6 on a desktop, you’ll need to go

down an additional level, right-click Default Web Site, and select Create Virtual Directory Don’t change any of the default values while stepping through the wizard, but you will

need to specify a name for the virtual directory or site name and the physical path The

virtual directory/site name is the location that a user would add to the address bar The

physical path is the location in your file system that you want the application to reside in

This location defaults to c:\inetpub, assuming that your OS is deployed to the c: drive

Installing IIS 7 on Windows 7

The following is a walk-through for setting up IIS 7 on Windows 7

1 Select Start | Control Panel | Programs And Features, which will display the Uninstall

Or Change A Program window, shown in Figure 11-3

2 Click the “Turn Windows features on or off” link, which will display the Windows

Features window, shown in Figure 11-4

Trang 3

316 Microsoft Visual Studio 2010: A Beginner’s Guide

Figure 11-3 The Uninstall Or Change A Program window

Figure 11-4 The Windows Features window

Trang 4

3 This example enables FTP, which is one of the ways you can deploy a Web site Ensure the option for “Ensure IIS Metabase and IIS 6 configuration compatibility,” under

the IIS 6 Management Compatibility branch, is selected When you click OK, the IIS

server will be installed

Creating a Web Site on IIS 7 on Windows 7

Next, you’ll need to create a Web site on IIS 7 on Windows 7 by following these steps:

1 Select Start | Control Panel | Administrative Tools, which will display the Administrative Tools window, shown in Figure 11-5

2 Double-click Internet Information Services (IIS) Manager to display the Internet

Information Services (IIS) Manager window, shown in Figure 11-6

Order of Installations Matter

ASP.NET and WCF Web Services are hosted in IIS and require special configuration to

allow hosting by IIS Therefore, it’s helpful if IIS is installed before VS is installed VS

will install all of the ASP.NET and WCF Service settings if IIS is installed If you install

IIS after VS is installed, you can still set up ASP.NET and WCF Service settings with

the following commands; first ASP.NET:

"%windir%\Microsoft.NET\Framework\v4.0.21006\aspnet_regiis.exe" –i –enable

and then WCF Services (all on one line):

"%WINDIR%\Microsoft.Net\Framework\v3.0\Windows Communication

Foundation\ServiceModelReg.exe" –r

The actual directory name for the aspnet_regiis.exe file might be different because

the v4.x.x.x will change in the future, so you might need to open Windows Explorer and

search for the actual directory name yourself

Trang 5

318 Microsoft Visual Studio 2010: A Beginner’s Guide

3 Right-click Sites and select Add Web Site, or click the Add Web Site link on the Actions panel to show the Add Web Site window, shown in Figure 11-7

4 Give the Web site a name and specify the physical location As shown in Figure 11-7, the name of the site is WcfDemo and the site will be physically located at c:\WebSites\ WcfDemo Notice that the Port in the Binding section is set to 8080 The default for a Web site port is 80, but you can have only a single site with that port Alternatively, you could change the host name and keep port 80 In this case, we decided to set the port to

8080 so that the addresses of the Web sites don’t overlap Use a different port number if

8080 is already used on your system Clicking OK will create the Web site

Figure 11-5 The Administrative Tools window

Trang 6

5 Ensure that the WcfDemo Web site is selected in IIS Manager and click the Add FTP

Publishing link on the Actions pane, which is on the right side of IIS Manager You’ll

see the Binding And SSL Settings window, shown in Figure 11-8

6 Accept the defaults on the Binding And SSL Settings window and click Next to show

the Authentication And Authorization window, shown in Figure 11-9

7 Set options on the Authentication And Authorization window according to who you

want to be able to access this Web site Anonymous allows anyone to access the site

and is less secure A more secure option would be to restrict access to specified users or groups that you trust Basic authorization shows a login screen when someone connects

to the FTP site Clicking Finish will enable this site for FTP access

Figure 11-6 The IIS Manager window

Trang 7

320 Microsoft Visual Studio 2010: A Beginner’s Guide

8 In IIS Manager, select Application Pools An application pool is a process that you can assign Web sites to This gives Web sites protection from each other because if one process crashes, it doesn’t bring down Web sites in other processes IIS created

an application pool for your Web site with the same name when creating it previously Double-click the application pool named after your Web site and set its NET

Framework version to v4 The exact version number could vary in the future, so you want to ensure it’s the same NET Framework version number that you build your application with in VS

Once your Web site is set up, you can deploy, which is discussed next

Figure 11-7 The Add Web Site window

Trang 8

Deploying the WCF Service to IIS

If you want to run the WCF service in VS, you don’t have to do anything because VS

already set the project up to run with a built-in server The discussion in this section is

intended to help you deploy to IIS on a Windows 2008 server If you just want to run the

Web service in VS, you can skip this section for now and move to the next section on how

to build a client that communicates with the Web service Then return to this section when you’re actually ready to deploy to IIS

To deploy a Web service project, you’ll need to obtain the address of the Web site,

modify the project configuration file, and use the VS Publish tool

Figure 11-8 The Binding And SSL Settings window

Trang 9

322 Microsoft Visual Studio 2010: A Beginner’s Guide

TIP

You must run VS as Administrator to publish To do this, close VS (if running), locate the

VS Start menu item (don’t click yet), right-click, and select Run As Administrator.

The technique used in the preceding section to create a Web site distinguished the Web site by making it run on port 8080 Although the Web site is named WcfDemo, it’s located

on the local machine, whose domain is localhost If you deployed the Web service to a site that already had a domain, you would use that domain name For example, the domain for the C# Station community site is csharp-station.com, which is addressed as http://www csharp-station.com Each Web service at a location is addressed by a *.svc file name and the name that VS creates is called WcfDemoCS.CustomerService.svc Putting the pieces

of WcfDemo site together results in an address of http://localhost:8080/WcfDemoCS CustomerService.svc

Figure 11-9 The Authentication And Authorization window

Trang 10

the project, which is a file that belongs only to VS The app.config file is never deployed

with your Web service, but it generates a file named web.config, which is deployed

with the project In WPF projects, VS uses the app.config file to generate a file named

projectname.exe.config in the same folder as the projectname.exe file WCF Service

projects don’t generate a config file in the output folder, but they do generate a web.config file when you deploy You’ll see web.config soon, after deployment

During development, you work with the app.config file, which is easy to find and

open in your project The app.config file has a lot of information in it, so Listing 11-6 is a

small excerpt that shows you the salient elements of the WCF configuration

Listing 11-6 The WCF service address in app.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.serviceModel>

<bindings>

</bindings>

<client />

<services>

<service name="WcfDemoCS.CustomerService">

<endpoint address="mex" binding="mexHttpBinding"

contract="IMetadataExchange" />

<host>

<baseAddresses>

<add baseAddress=

"http://localhost:8732/Design_Time_Addresses

/WcfDemoCS/CustomerService/" />

</baseAddresses>

</host>

</service>

</services>

</system.serviceModel>

</configuration>

Trang 11

324 Microsoft Visual Studio 2010: A Beginner’s Guide

Following the path in Listing 11-6—configuration, system.serviceModel, services,

service, host, and baseAddresses—you’ll find a baseAddress element in bold The

baseAddress in Listing 11-6 is split into two lines to fit the book, but remember to combine

it into a single line The baseAddress is declaring that applications can communicate

with this service via this address This is a VS development Web server address that

was generated for this WCF service Previously, you saw how we figured out what the deployment address of this application should be Therefore, when you deploy, comment out the development address and replace it with the deployment address, like this:

<baseAddresses>

<! <add baseAddress=

"http://localhost:8732/Design_Time_Addresses/WcfDemoCS/Service1/" /> > <add baseAddress=" http://localhost:8080/WcfDemoCS.CustomerService svc " />

</baseAddresses>

The <! and > are comment delimiters, and anything in between them won’t be interpreted as part of the configuration Notice how the deployment address is used (uncommented) as the base address After deployment, you can comment the deployment address and uncomment the development address so that you can continue working with the WCF service with the VS Web server

In addition to the baseAddress, you need to ensure your database connection is

updated for the deployment environment In the development environment, the default

DB connection string defaults to using Integrated Security = true as login credentials, which uses the identity of the currently logged-in user The result in the deployment environment is that the application will run as the identity of the application pool the Web site is assigned to The problem with this is that the application pool doesn’t have access to your database The best approach is to create a user for your application only, give that user access to your database, and then set the connection string to use the credentials of that user

Create a user in your Windows OS that will be used for SQL Server and then give that user access to the database If you’re using an Express version of SQL Server, it can help if you download the free SQL Server Express Management Studio Because of all the variables that can affect setting up security, refer to SQL Server documentation for more guidance This chapter uses SQL authentication, so go ahead and create a Windows or SQL user for the MyShop database

Trang 12

to use the credentials of that user account, like this For best security, please remember to

change the password:

<add name=

"WcfDemoCS.Properties.Settings.MyShopConnectionString"

connectionString=

"Data Source=.\sqlexpress;Initial Catalog=MyShop;

User ID=MyUserAccount;Password=G7b@H8m2a%lM6y;Pooling=False"

providerName="System.Data.SqlClient" />

To deploy, right-click the Web Services project, WcfDemo, and click Publish, which

will display the Publish WCF Service window shown in Figure 11-10

In the Publish WCF Service window, set the Target Location to the address where the

WCF Service is deployed You saw how to figure out the address earlier in this section

You can choose to either replace only matching files or delete all files in the deployment

location You normally only want to copy files needed to run this application because the

deployment will be quicker with fewer files and possibly more secure by only deploying

what is necessary The check box for Include Files From The App_Data Folder is disabled

because there isn’t an App_Data folder in the WCF Service project However, this same

tool is used to deploy an ASP.NET Web site, which might have an App_Data folder

Figure 11-10 The Publish WCF Service window

Ngày đăng: 04/07/2014, 03:20