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

Introducing Windows Azure- P39 potx

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

Đ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 368,12 KB

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

Nội dung

Create a service host class.. Listing 6-3 shows the implementation for the host class.. The class constructor of the service host accepts four parameters that will be passed in when the

Trang 1

178

3 Create a service host class The format to compose an address of NET Service Bus is the following, where, [ ] = optional, < > = required

<[sb][http][https]>://<solution>.servicebus.windows.net[/service topic][/sub topic] The endpoint address to be used in this exercise is

sb://softnetsolutionsservicebus.servicebus.windows.net/Pheonix/RelayService/

4 Listing 6-3 shows the implementation for the host class This class declares two

constructors The second constructor takes the endpoint URI as a parameter This

allows the host to be able to accept different endpoints in order to support multiple modes for the NET relay connection As you can see, we use a username and password

to authenticate in this example The class constructor of the service host accepts four parameters that will be passed in when the host instance is instantiated The first

parameter is a generic type parameter of the WCF service implementation class (not the service contract interface type), and the other three parameters are account-related parameters: solutionName, password, and topic The topic parameter is used to

construct the URI address as a lower-level hierarchy in case there are multiple service hosts registered under the same solution, and we can get all URI addresses that

are globally unique The remaining part of the host implementation is pretty

straightforward We use the URI address to instantiate a ChannelFactory instance (the ChannelFactory class can accept the WCF interface type IRelayPublishEventService, which is derived from both the custom-defined WCF service interface and the

IClientChannel interface defined from the System.ServiceModel namespace) We then call the CreateChannel() and Open() methods sequentially to start the service request listener on the host side from the cloud

Listing 6-3 Implementation of the Host Class

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Security.Cryptography;

using System.ServiceModel;

namespace SoftnetSolutions.RelayService.PublishChannel

{

using Microsoft.ServiceBus;

using Microsoft.ServiceBus.Description;

public class RelayPublishEventHost <T> where T : class

{

protected ChannelFactory<IRelayPublishEventService> channelFactory = null; public string ServiceTitle { get; set; }

public IRelayPublishEventService Channel { get; set; }

public RelayPublishEventHost(T serviceImpl,

string topic,

string solutionName,

string password)

{

Trang 2

179

ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;

TransportClientEndpointBehavior relayCredentials =

new TransportClientEndpointBehavior();

relayCredentials.CredentialType =

TransportClientCredentialType.UserNamePassword;

relayCredentials.Credentials.UserName.UserName = solutionName;

relayCredentials.Credentials.UserName.Password = password;

ServiceTitle = topic;

Uri serviceAddress =

ServiceBusEnvironment.CreateServiceUri("sb", solutionName,

String.Format("{0}/RelayService/", ServiceTitle));

ServiceHost host = new ServiceHost(serviceImpl.GetType(), serviceAddress);

host.Description.Endpoints[0].Behaviors.Add(relayCredentials);

host.Open();

channelFactory =

new ChannelFactory<IRelayPublishEventService>("RelayEndpoint",

new EndpointAddress(serviceAddress));

channelFactory.Endpoint.Behaviors.Add(relayCredentials);

Channel = channelFactory.CreateChannel();

Channel.Open();

}

public RelayPublishEventHost(T serviceImpl,

Uri serviceAddress,

TransportClientEndpointBehavior relayCredentials)

{

ServiceHost host = new ServiceHost(serviceImpl.GetType(), serviceAddress

host.Description.Endpoints[0].Behaviors.Add(relayCredentials);

host.Open();

channelFactory =

new ChannelFactory<IRelayPublishEventService>("RelayEndpoint",

new EndpointAddress(serviceAddress));

channelFactory.Endpoint.Behaviors.Add(relayCredentials);

Channel = channelFactory.CreateChannel();

Channel.Open();

}

}

}

5 Create a Windows console application, as shown in Listing 6-4 This class reads the

password and solution name from the configuration file and creates an instance of

the host class A user can type any text message and post to the hub All parties

hooked to the hub will be notified when the event happens The credential

authentication is against a NET Service Bus instead of the local Windows system

Therefore the event can be delivered to applications behind a firewall

Trang 3

180

Listing 6-4 Implementation for Windows Console Application

using System;

using System.Collections.Generic;

using System.Text;

using System.Security.Cryptography;

using System.ServiceModel;

using Microsoft.ServiceBus.Description;

using System.Configuration;

namespace SoftnetSolutions.RelayService.PublishChannel

{

using Microsoft.ServiceBus;

class Program

{

private Program(string[] args)

{

}

static void Main(string[] args)

{

Program programInstance = new Program(args);

programInstance.Run();

}

private void Run()

{

string subject = ConfigurationManager.AppSettings["Topic"];

string solutionName = ConfigurationManager.AppSettings["Solution"]; string password = ConfigurationManager.AppSettings["password"]; PublishEventService service = new PublishEventService();

RelayPublishEventHost<PublishEventService> host =

new RelayPublishEventHost<PublishEventService>(service,

subject,

solutionName,

password);

Console.WriteLine(string.Format(

"{0} - Connecting success, Press <Enter> to exit -{0}", Environment.NewLine));

string input = Console.ReadLine();

while (input != String.Empty)

{

PostData postData = new PostData();

postData.Message = input;

host.Channel.PostMessage(postData);

Trang 4

181

input = Console.ReadLine();

}

host.Channel.Close();

}

}

}

6 Set up CardSpace NET Service Bus Relay Authentication To use the CardSpace

runtime authentication mode, the following steps need to be done

1 Add a card to the solution in the cloud via the NET Service Bus manage-

ment portal page Log in to the NET Services and SQL Azure portal at

http://portal.ex.azure.microsoft.com/default.aspx (Note that this portal

page is a different portal page from the Windows Azure portal page and the

Azure Services Developer Portal.) Then follow the instructions to submit the

card shown in Figure 6-3

2 Modify the code of the host implementation from Listing 6-3 as shown in

Listing 6-5

Authentication Modes

.NET Service Bus supports runtime authentication with six modes:

Discussion of all these modes is beyond the scope of this book We are going to investigate two of these

supported modes as an example The UserNamePassword mode is the most frequently used runtime

authentication mode That is also used in this exercise as the boldface lines show in Listing 6-3

Listing 6-5 Use the CardSpace for NET Service Bus Connection Authentication

relayCredentials.CredentialType = TransportClientCredentialType.CardSpace;

1. UserNamePassword

2. CardSpace

3. X509Certificate

4. Unauthenticated

5. FederationViaCardSpace

6. AutomaticRenewal

Trang 5

182

Figure 6-3 Submit a CardSpace to NET Services

7 Send the card interactively at runtime Now, when you run the application, you are asked to submit the same card for authentication as Figure 6-4 shows

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