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

Introducing Windows Azure- P35 pptx

5 253 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 188,92 KB

Nội dung

CHAPTER 5 ■ AZURE .NET SERVICES—ACCESS CONTROL 143 private User registeredUser = null; #region IUserRegisterService Members public string Ping() { return string.Format(" I am here <{0}>", this.ToString()); } public void RegisterUser(string xmlString) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlString); XmlSerializer serializer = new XmlSerializer(typeof(User)); StringReader reader = new StringReader(xmlString); registeredUser = (User)serializer.Deserialize(reader); } catch (Exception ex) { } } public string GetRegisteredUser() { XmlSerializer serializer = new XmlSerializer(typeof(User)); StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); serializer.Serialize(writer, registeredUser); return writer.GetStringBuilder().ToString(); } #endregion } } Service Implementations and Configurations The following is the implementation of the server (Listing 5-9) and its configuration (Listing 5-10). Listing 5-9. Implementations for Service Host using System; using System.Security.Cryptography.X509Certificates; using System.ServiceModel; using System.ServiceModel.Description; CHAPTER 5 ■ AZURE .NET SERVICES—ACCESS CONTROL 144 namespace AzureForDotNetDeveloper.DotNetService.ServiceBus { class Program { static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(AzureForDotNetDeveloperWCFservice)); host.Open(); Console.WriteLine(" UserRegister service is running."); Console.WriteLine(" Press <Enter> to terminate server"); Console.ReadLine(); host.Close(); } private static string ReadSolutionName() { Console.Write( string.Format( " Please enter your solution name: {0}", Environment.NewLine ) ); return Console.ReadLine(); } } } Listing 5-10. Configurations for Service Host <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="basicHttpBinding1" /> </basicHttpBinding> </bindings> <services> <service behaviorConfiguration="UserRegisterServiceBehavior" name="AzureForDotNetDeveloper.DotNetService.ServiceBus« .AzureForDotNetDeveloperWCFservice"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="UserRegisterEndpoint" contract="AzureForDotNetDeveloper.DotNetService.ServiceBus« .IAzureForDotNetDeveloperWCFservice" /> <endpoint address="mex" CHAPTER 5 ■ AZURE .NET SERVICES—ACCESS CONTROL 145 binding="mexHttpBinding" name="mexEndpoint" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost/AzureForDotNetDeveloperWCFservice" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="UserRegisterServiceBehavior"> <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost/AzureForDotNetDeveloperWCFservice/wsdl" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> Client Implementations and Configurations The client proxy class needs to be generated from Visual Studio. Compile the server and start the server. When the server is running, right-click on the client project References node and select Add Service Reference from the context menu to bring up the Add Service Reference dialog windows. As Figure 5-6 shows, enter the service address http://localhost/AzureForDotNetDeveloperWCFservice in the address text box, and the name of the client class in the namespace text box to generate the client proxy class. Figure 5-6. Run server and generate the client proxy class from Visual Studio CHAPTER 5 ■ AZURE .NET SERVICES—ACCESS CONTROL 146 The following is the client implementation using the proxy. Listing 5-11. Using the Proxy in a Client Implementation static void Main(string[] args) { UserRegisterServiceClient.UserRegisterServiceClient client = new UserRegisterServiceClient.UserRegisterServiceClient(); try { Console.WriteLine(string.Format(" Ping server return = <{0}>{1}", client.Ping(), Environment.NewLine)); User user = new User(); user.FirstName = "Henry"; user.LastName = "Li"; user.Password = "Hello Azure WCF host"; user.TimeRegistered = DateTime.Now; XmlSerializer serializer = new XmlSerializer(user.GetType()); StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); serializer.Serialize(writer, user); client.RegisterUser(writer.GetStringBuilder().ToString()); string xmlString = client.GetRegisteredUser(); XmlSerializer deSerializer = new XmlSerializer(typeof(User)); StringReader stringReader = new StringReader(xmlString); User registeredUser = (User)serializer.Deserialize(stringReader); Console.WriteLine( string.Format(" User <{0} {1}> register success @[{2}].{3}", registeredUser.FirstName, registeredUser.LastName, registeredUser.TimeRegistered.ToString(), Environment.NewLine)); } catch (Exception e) { DumpException(e); } client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to exit client."); Console.ReadLine(); } CHAPTER 5 ■ AZURE .NET SERVICES—ACCESS CONTROL 147 static void DumpException(Exception e) { Console.WriteLine(e.Message); } Test Results for What We Have Built At this point we have done the first section of development. Let’s test what we have so far. Start the server if it is not running yet; in this case we have the server running as localhost. Now enter the service address http://localhost/AzureForDotNetDeveloperWCFservice in a browser; we should see the results as Figure 5-7 shows. Run the client program, and we should have results as in Figure 5-8. Figure 5-7. Run the server and use Internet Explorer to verify that the service has been created . select Add Service Reference from the context menu to bring up the Add Service Reference dialog windows. As Figure 5-6 shows, enter the service address http://localhost/AzureForDotNetDeveloperWCFservice

Ngày đăng: 05/07/2014, 01:20

w