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

Expert VB 2005 Business Objects Second Edition phần 10 potx

74 274 0

Đ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 74
Dung lượng 1,32 MB

Nội dung

With simple web methods, this display includes the ability to invoke the method from within the browser. For example, Figure 11-10 shows the result of clicking the Invoke button to execute the GetResourceList() w eb method. CHAPTER 11 ■ WEB SERVICES INTERFACE598 Figure 11-9. WSDL for the GetResourceList web method 6315_c11_final.qxd 4/7/06 2:25 PM Page 598 Your results may vary, of course, depending on the data in your database. A Simple S mart Client To further illustrate how to call PTWebService, and in particular to show how to deal with the custom SO AP header for authentication, the ProjectTracker solution contains a PTServiceClient pr oject. This is a bare-bones smart client application that acts as a consumer for PTWebService. Figure 11-11 shows what the application looks like when running. CHAPTER 11 ■ WEB SERVICES INTERFACE 599 Figure 11-10. Results of invoking the GetResourceList method 6315_c11_final.qxd 4/7/06 2:25 PM Page 599 My goal with this application isn’t to create a complete consumer. I want to use this application to show how to consume a basic web service, and how to set up and pass credentials through the custom SOAP header. As shown in Figure 11-12, PTServiceClient has a web reference to PTService. CHAPTER 11 ■ WEB SERVICES INTERFACE600 Figure 11-11. The PT WebService client application Figure 11-12. Web reference to PTService 6315_c11_final.qxd 4/7/06 2:25 PM Page 600 The URL behavior for this reference is set to Dynamic in the Properties window. This means that the URL for the web service is maintained in the app.config file: <applicationSettings> <PTServiceClient.My.MySettings> <setting name="PTServiceClient_PTService_PTService" serializeAs="String"> < value>http://localhost/PTWebService/PTService.asmx</value> </setting> </PTServiceClient.My.MySettings> </applicationSettings> The <applicationSettings> element is part of the configuration functionality provided by System.Configuration in .NET 2.0, and it is automatically used by Visual Studio when you set the URL behavior property to Dynamic for a web reference. When you add a web reference to your project, Visual Studio uses the WSDL description for the web service to determine all the types it exposes, including CslaCredentials, ProjectData, and the other types accepted as parameters or returned as results from the web methods. Visual Studio uses this information to create proxy classes for all these types, so they can be used in the con- sumer code as though they were local classes. Calling a Web Method The data binding support in Windows Forms works against the proxy classes generated for a web service. This means you can add a type like ProjectData to the Data Sources window much like Project was added in Chapter 9. Figure 11-13 sho ws the D ata Source Configuration Wizard listing all the types from the PTService web reference. When you go to add a data source to the Data Sources window, the first step in the wizard includes the option to add a web service as a data source, as shown in Figure 11-14. CHAPTER 11 ■ WEB SERVICES INTERFACE 601 6315_c11_final.qxd 4/7/06 2:25 PM Page 601 CHAPTER 11 ■ WEB SERVICES INTERFACE602 Figure 11-13. Types available from the PTService web reference Figure 11-14. Adding a web service as a data source 6315_c11_final.qxd 4/7/06 2:25 PM Page 602 While you can use this option, it gets you exactly the same result as if you manually add the web reference and then add the proxy objects as object data sources. In other words, web service proxy objects are always object data sources, regardless of whether you add them using the web service or object options in the Data Source Configuration Wizard. Once the proxy types are in the Data Sources window, you can drag and drop them onto a form just like you would with any business object. This is how the PTServiceClient UI was built. For each type you drag onto the form, Visual Studio creates a corresponding BindingSource o bject in the form’s component tray. The UI controls are bound to the B indingSource c ontrol, and that BindingSource control is bound to your data. Just like in Chapter 9, you need to write a bit of code to set the DataSource property of each BindingSource object. For instance, when the client’s form loads, the following code is run: Private Sub MainForm_Load( _ ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Using svc As New PTService.PTService Me.ProjectInfoBindingSource.DataSource = svc.GetProjectList Me.ResourceInfoBindingSource.DataSource = svc.GetResourceList Me.RoleInfoBindingSource.DataSource = svc.GetRoles End Using End Sub First, an instance of PTService is created: Using svc As New PTService.PTService Notice that it is within a Using block, so the object is pr operly disposed when the code is through with it. Then the project, resource, and role data is retrieved from the web service. Each resulting object is used to set a DataSource pr operty, ultimately populating the three DataGridView controls across the top of the form shown in Figure 11-11. Of course, this is the simple case, since these three web methods don’t require authentication. Let’s look at the case in which a method does require authentication using the custom SOAP header. Providing Credentials for Authentication To supply a SOAP header, the consumer needs to create an instance of the SoapHeader class; in this case, that means CslaCredentials. This object has its properties loaded with appropriate username and password values, and it is then attached to the consumer-side proxy for the web service. To streamline this process throughout the client application, the code is centralized in a SetCredentials() helper method: Private Sub SetCredentials(ByVal svc As PTService.PTService) Dim credentials As New PTService.CslaCredentials credentials.Username = UsernameTextBox.Text credentials.Password = PasswordTextBox.Text svc.CslaCredentialsValue = credentials End Sub First, a CslaCredentials object is created and loaded with values: Dim credentials As New PTService.CslaCredentials credentials.Username = UsernameTextBox.Text credentials.Password = PasswordTextBox.Text B ecause the CslaCredentials class was exposed b y the w eb ser vice, Visual Studio automatically created a consumer-side proxy class for it, used here. CHAPTER 11 ■ WEB SERVICES INTERFACE 603 6315_c11_final.qxd 4/7/06 2:25 PM Page 603 The WSDL definition for the web service also indicated that there are web methods that require this as a SOAP header, so Visual Studio automatically added a CslaCredentialsValue property to the consumer-side proxy. To pass a CslaCredentials object to the server as a SOAP header, all you need to do is set this CslaCredentialsValue property! svc.CslaCredentialsValue = credentials With that done, it becomes relatively easy to call a web method that requires authentication. For instance, the following code is called to assign a resource to a project: Using svc As New PTService.PTService SetCredentials(svc) Try ' do the assignment svc.AssignResource( _ CInt(Me.ResourceIdLabel.Text), New Guid(Me.ProjectIdLabel.Text)) ' refresh the detail view Dim request As New PTService.ProjectRequest request.Id = New Guid(Me.ProjectIdLabel.Text) Me.ProjectDetailBindingSource.DataSource = svc.GetProject(request) Catch ex As Exception MessageBox.Show(ex.Message, "Assign resource", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End Try End Using As before, an instance of the web service proxy is created: Using svc As New PTService.PTService Before doing anything else, however, the credentials are attached to this object: SetCredentials(svc) Now all web method calls within this Using block will automatically have the custom SO AP header with the credentials passed to the server. So when AssignResource() is called, it can authen- ticate the credentials, and the business objects can authorize the action based on the roles for the supplied username: svc.AssignResource( _ CInt(Me.ResourceIdLabel.Text), New Guid(Me.ProjectIdLabel.Text)) Interestingly, the custom SOAP header is also passed to the subsequent GetProject() call: Me.ProjectDetailBindingSource.DataSource = svc.GetProject(request) This doesn’t cause any problem. The GetProject() web method doesn’t have a <SoapHeader()> attribute, so the custom SOAP header is simply ignored by the server-side code. You can look through the rest of the client code in the code download for the book. At this point, ho w ev er , y ou should understand how to set up web service proxy objects as data sources for Windows Forms, how to call simple web methods, and how to call web methods that require a custom SOAP header. CHAPTER 11 ■ WEB SERVICES INTERFACE604 6315_c11_final.qxd 4/7/06 2:25 PM Page 604 Conclusion W eb services enable the creation of another type of interface to business objects. Rather than exposing an interface directly to users, as with Windows forms or web forms, web services expose an interface for use by other, external applications. Those applications can call your web methods to leverage the business functionality and data provided by your application and its business o bjects. You can design your web services along the lines of MTS or COM+ components, effectively creating a public API for your application. In such a case, most people tend to think of the web service as implementing a tier in an n-tier or a client/server model. In many cases, it is better to follow service-oriented thinking, which specifies that a service (web service or otherwise) is an autonomous entity—an independent application, not a tier within a larger application. Service orientation also specifies that your web methods should communicate using a message-based appr oach, for which a web method accepts a complex type as a message, and returns a complex type as a resulting message. In this chapter, I demonstrated how to create web methods using both approaches, so you can decide which works best in your application. The example web service and client illustrate how you can expose all the functionality of your business objects without duplicating business logic in the web service interface itself. The valida- tion, authentication, and other business logic is all encapsulated entirely in the business objects. Chapter 12 will close the book b y showing how to create remote data portal hosts for remoting, Web Services, and Enterprise Services. These remote hosts can be used by the Windows Forms, Web Forms, and Web Services interfaces you’ve seen in the last three chapters. CHAPTER 11 ■ WEB SERVICES INTERFACE 605 6315_c11_final.qxd 4/7/06 2:25 PM Page 605 6315_c11_final.qxd 4/7/06 2:25 PM Page 606 Implementing Remote Data Portal Hosts In Chapters 9 through 11, you saw how to implement Windows Forms, Web Forms, and Web Services interfaces to a common set of business objects. In each chapter, I briefly discussed the configur ation options av ailable in terms of running the server-side data portal components on the client or on an application server. What I hav en’t discussed yet in detail is how to set up application servers to host the server- side data portal components and your business objects. As discussed in Chapter 4, the data portal implements a channel adapter pattern, allowing you to communicate from the client to the appli- cation server using .NET Remoting, Web Services, or Enterprise Services. It is also possible for you to create your own custom channel proxy and host if these standard technologies aren’t sufficient for your needs. It is my intent to provide a WCF (Windows Communication Foundation, or Indigo) channel proxy and host when that technology becomes available. You’ll be able to find further information at www.lhotka.net/cslanet. I want to be ver y clear , however, that I believe you should only use a remote data portal to achieve scalability, security, or fault tolerance objectives. As I discussed in Chapter 1, adding physi- cal tiers to an application is a double-edged sword. You lose performance and increase complexity by adding tiers, so you should have a strong reason for doing so . In this chapter, I will walk through the process of setting up and using three data portal application server hosts: • .NET Remoting • Web Services • Enterprise Services Though no code changes are required in your UI or business objects, each application server will require a slightly different set of steps to work. I n general terms, though, the process for each is similar: 1. Set up the host on the server. 2. Make your business object assembly available to that host. 3. Configure the client to use the new host. To a large degree, the implementation of the data portal in Chapter 4 already took care of the hard parts, so these steps are relatively straightforward in each case. Before creating each host, I want to spend a short time discussing why you might choose each of the channel technologies. 607 CHAPTER 12 ■ ■ ■ 6315_c12_final.qxd 4/7/06 2:23 PM Page 607 [...]... implementing ProjectResource class, 438 read-only business objects, 400 AddBusinessRules method BusinessBase class, 278, 281 implementing Project class, 418–419, 426 implementing ProjectResource class, 437 implementing Role class, 456 object-editing methods in BusinessBase, 120 ValidationRules object managing rules, 126 adding objects child objects, 373 root objects, 372 AddNew method SortedBindingList... business logic authorization, 58 business object centralizing, 26 business objects, 22–25 encapsulation, 54 locating in multiple layers, 18, 19 locations for validation and manipulation business and data access tier, 20 6315_idx_final.qxd 4/7/06 5:27 PM Page 631 sINDEX editable child business objects, 388–391 editable child collections, 397–398 editable root business objects, 384–387 editable root collections,... 80 user interface interacting with, 78 using business object as data source RolesEdit web form, 542 web service data transfers, 577 wrapping in transaction, 220 business objects, life cycle of, 365–377 adding/editing root objects, 372–373 adding/editing/deleting child objects, 373–374 creating child objects, 367–369 creating root objects, 366 deleting root objects, 374–376 object creation, 366–369 object... class, 186 SimpleDataPortal implementing data methods, 225 CanAddObject method editable root business objects, 387 implementing Project class, 420, 422 Cancel button ProjectEdit saving data, 510, 511 CancelEdit method BusinessBase class, 105 , 119–120 BusinessListBase class, 105 deleting and undeleting child objects, 151, 153 edit level, 148 IEditableObject interface, 120, 121, 122 ProjectEdit saving... SortedEnumerator class, 259 classes, PTracker merging similar objects, 336 clean objects IsNew property, BusinessBase class, 114 Find it faster at http://superindex.apress.com/ CanGetObject method editable root business objects, 387 implementing Project class, 420 read-only business objects, 400 CanInsert property data source controls, 317 CanReadProperty method BusinessBase class, 128, 129 implementing Project... creation, 366–369 object disposal, 376–377 object retrieval, 369–371 retrieving child objects, 371 retrieving root objects, 369–370 updating editable objects, 371–376 business rules see also rules common business rules, 277–281 CSLA NET design benefits, 344 method signature for, 70 tracking broken business rules, 40–41, 68 BusinessBase class, 60–62, 112–130, 143–146 authorization rules, 128–130 AuthorizationRules... implementing Roles class, 451–452 business objects, 22–25 see also mobile objects business class structure, 378–405 centralizing business logic, 26, 27 command objects, 402–404 common regions, 378–381 component parts of, 24 composed of state/implementation/interface, 24 CSLA NET design benefits, 344 CSLA NET framework, 59–64 CSLA NET in design process, 345 data portal returning updated business object to UI,... classes used by business developers, 59 IEditableObject interface, 47 implementing business objects, 407–464 invoking methods on, 185 loading from database, 173 locating specific method, 181 managing persistence to data store, 54 models for UI developer, 43–47 name/value list objects, 404–405 object design, 22, 330 potential objects and associated class names, 331 read-only business objects, 398–400... retrieving, 371 root/parent/child behaviors, 123–125 strongly typed collections of child objects, 41–42 switchable business objects, 391–393 UndoableBase class, 66 class-in-charge (Factory pattern) model, 45–47 business objects, 381 Shared factory methods, 46, 47 classes see also base classes business class structure, 378–405 business framework base classes, 60 complete code for, 94 Criteria class, 381–383... object modeling, 50 bi-directional data updates, 81 BinaryFormatter class cloning serializable object, 100 serializing and stacking Hashtable, 110 serializing object state, 100 BindableBase class, 67, 100 104 class diagram for, 68 description, 94 OnPropertyChanged method, 103 OnUnknownPropertyChanged method, 103 binding see data binding Binding object Format event, 304 Parse event, 304 BindingComplete event . your business objects without duplicating business logic in the web service interface itself. The valida- tion, authentication, and other business logic is all encapsulated entirely in the business. serializing your business objects across the network, so the only way that the data on the other end of the wire can be understood is if the data is deserialized back into your business classes DATA PORTAL HOSTS 610 6315_c12_final.qxd 4/7/06 2:23 PM Page 610 Ease of Implementation Finally, there’s the ease of implementation. As I mentioned earlier, neither your UI nor business object code

Ngày đăng: 12/08/2014, 16:21