The code sample demonstrates the following typical scenario:
- You host a web service in the cloud.
- You distribute a client application from the cloud.
- You consume the cloud service from the client application.
In the code sample, the client application is an XAML Browser Application (XBAP), and the cloud service is a Windows Communication Foundation (WCF) service hosted in a Microsoft Azure Web Role.
Download informationTo download this code sample, click the following link:
Distribute XBAP from Microsoft Azure To distribute a XBAP from Microsoft Azure, use the following steps:
- Create a Microsoft Azure solution with at least one Web Role project.
- Create a new (or include an existing) XBAP project in the same solution.
- Build the solution.
- Right click the Web Role project, choose Add existing item, navigate to the output directory of the XBAP application, and add the following files to the Web Role project:
- Select the above files, and set their Build Action to Content.
- If you want to display the XBAP application when the user navigates to the root URI of your Web Role, please modify web.config, and add the following under the system.webServer section (replace XbapClient.xbap to your XBAP's name):
Note If you want to modify the original XBAP project, you must take additional steps for the changes to be reflected in the Web Role:
- Open a Visual Studio command prompt, and run the following command to clear the XBAP cache:
- Delete the .exe, .manifest, and .xbap files from the Web Role.
- Repeat step 3 to step 5 above.
Access web services from XBAPIn most cases, to provide a true connected experience, the client applications that are distributed from the cloud need to invoke web services hosted in the cloud, This sample demonstrates how to access a WCF SOAP service from XBAP.
The process is similar to accessing an SOAP service from a normal client. If you need to allow clients to add a service reference to your Microsoft Azure WCF service, you need the hotfix and workaround by visiting the following web site:
However, there are also a few unique tasks to perform in order to make WCF calls successful in XBAP:
- You must configure the service endpoint in code rather than in app.config. By default, in Microsoft Azure (and normal IIS environment), an <file name>.config file (such as web.config and app.config) is not allowed to be downloaded to the client. You can configure system.webServer to allow the download, but that makes web.config to be downloadable as well, which may cause serious security concerns. Therefore it is always recommended to take the code only approach for XBAP applications. Silverlight applications do not have this problem, because the configuration file name's extension is ClientConfig, rather than config.
- After adding the service reference, you need to remove the generated app.config file.
- To configure the service endpoint in code, you can use the following overload of the client proxy class:
public DataCenterServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress)
This overload requires the service address information. You can use an absolute address. But if the service is hosted in the same Web Role, it is recommended to use a relative URI, so you don't need to modify the code before deploying the application to the cloud. The relative URI can be obtained with the help of BrowserInteropHelper.Source property:
Uri serviceUri = new Uri(BrowserInteropHelper.Source, "../DataCenterService.svc");
- A partial trust XBAP can only consume services from the same domain by default. To invoke cross domain services, you must modify the application properties to use full trust, or grant yourself permission to access web resources. Silverlight cross domain access policy file does not affect XBAP. Note if your XBAP is not use the default partial trust configuration, you must make sure it does not perform any tasks that may harm the end user's machine, and you must sign the XBAP with a valid certificate, otherwise you won't be able to deploy a non-default partial trust XBAP to an internet environment, such as the cloud. This sample does not cover the non-default partial trust scenario.
- Microsoft Azure
LanguagesThis code sample contains the following programming languages:
- This sample application was created by using Visual Studio 2010 with Microsoft Azure Tools for Microsoft Visual Studio 1.2.
- Microsoft Azure
What is All-In-One Code Framework?All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Web site:
How to find more All-In-One Code Framework samplesTo find more All-In-One Code Framework samples, you can simply click the following link:
Rapid publishing disclaimerMicrosoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.
ID Bài viết: 2425647 - Xem lại Lần cuối: 09-10-2014 - Bản sửa đổi: 1