Nov 20 Chaks |

Introduction to Microsoft .Net Services – .Net User Group presentation slides and samples

Thanks to all who came along to my presentation today :)

You can download the presentation below :

Hello World Sample :

 

Using Cardspace authentication with .Net Services

If you want to use CardSpace authentication in your .Net services, then all you need to do is don’t add any authentication type to your binding ;)

Confused? Well, .Net Services by default uses CardSpace Authentication. So, all you need to supply is just your solution name and automatically you will be prompted for CardSpace authentication!

Here is my Service code

static void Main(string[] args){    Console.WriteLine("Enter your solution name : ");    string solutionName = Console.ReadLine();                    string serviceUri = String.Format("sb://{0}/services/{1}/HelloWorldService/",                                 ServiceBusEnvironment.DefaultRelayHostName,                                 solutionName);    ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService), new Uri(serviceUri));    serviceHost.Open();    Console.WriteLine("Service started - Press ENTER to stop");    Console.Read();    serviceHost.Close();}

 

And my config

<system.serviceModel>     <services>          <service name="Chaks.Samples.MyService.HelloWorldService">      <endpoint name="HelloWorldRelayEndpoint"                contract="Chaks.Samples.MyService.IHelloWorldService"                binding="netTcpRelayBinding"/>    </service>  </services></system.serviceModel>

 

But, if you do need to explicitly tell the Service Bus to use CardSpace Authentication, then you need to add an endpoint behavior which uses CardSpace authentication type to your config

<system.serviceModel>     <services>          <service name="Chaks.Samples.MyService.HelloWorldService">      <endpoint name="HelloWorldRelayEndpoint"                contract="Chaks.Samples.MyService.IHelloWorldService"                behaviorConfiguration="CardspaceCredentialsBehavior"                binding="netTcpRelayBinding"/>    </service>  </services>  <behaviors>    <endpointBehaviors>      <behavior name="CardspaceCredentialsBehavior">        <transportClientEndpointBehavior credentialType="CardSpace"/>      </behavior>    </endpointBehaviors>  </behaviors></system.serviceModel>

 

Programming Microsoft .Net Services

If you are not familiar with Microsoft .Net Services, do read my earlier post

So, are you ready to start programming .Net Services ? Not so fast, there are still certain things that you need to know before you start coding ;)

Microsoft .Net Services Account

First you need a Microsoft .Net Services account to actually host and create services that interact with the Service Bus. You can go to Microsoft Connect and apply for the ‘Azure Services Invitations’. Once you register, you will be put in the application pool and if you are lucky, you might get a token immediately for Microsoft .Net Services.

Once you have the token, proceed here to enter the invitation code. Activate the .Net Services and create a solution. Your solution name/password will be used for authentication when you host your services with Microsoft .Net Services. You can also associate Windows Cardspace or a X.509 Certificate for your solution.

azure-services-credentials

Finally, download the .Net Services SDK here

Connection Modes

Service Bus supports three types of connection modes:

Relayed

image

1) Uses relay between server and client.

2) This is the default connection mode

Direct

image

1) Direct connection between server and client, though a relayed connection is used for initialization

2) If Direct connection is not possible, then relay will be used

Hybrid

image

1) Combination of Relay and Direct

2) Establishes a relayed connection, but uses direct connection whenever possible

Service Bus Bindings

The Service Bus is entirely built on Windows Communication Foundation (WCF) . If you are not familiar with WCF, then you could start here. To build a simple ‘Hello World’ service, all you need to do is to create a WCF Service and WCF Client with a ‘specific’ service bus binding. The bindings currently available are:

  • WebHttpRelayBinding
  • BasicHttpRelayBinding
  • BasicHttpRelayContextBinding
  • WSHttpRelayBinding
  • WS2007HttpRelayBinding
  • WS2007FederationHttpBinding
  • NetTcpRelayBinding
  • NetOnewayRelayBinding
  • NetEventRelayBinding

‘Hello World’

Basic steps to write a ‘Hello World’ sample are:

1) Create a WCF Service (Service Contract)

2) Create a Service Bus binding

3) Host in the Service Bus (by authenticating yourself)

4) Write a basic client

5) The client also needs to authenticate with the Service Bus to interact with your service

Building the ‘Hello World’ Service

1) Create your Service Contract

[ServiceContract]public interface IHelloWorldService{    [OperationContract]    string HelloWorld();}

 

2) Implement your Service

[ServiceBehavior(Name = "HelloWorldService",     Namespace = "http://Chaks.Samples.MyService/HelloWorld/")]public class HelloWorldService : IHelloWorldService{    #region IMyService Members    public string HelloWorld()    {        return "[Service returns] Hello World";    }    #endregion}

3) Edit your app.config to hold the WCF endpoints

<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <services>      <service name="Chaks.Samples.MyService.HelloWorldService">        <endpoint name="HelloWorldRelayEndpoint"                  contract="Chaks.Samples.MyService.IHelloWorldService"                  binding="netTcpRelayBinding"/>      </service>    </services>  </system.serviceModel></configuration>

The binding we are using here netTcpRelayBinding and the connection mode is the default, which is Relay.

4) You can now host your service. To do so,

a. Enter your solution name and password

Console.WriteLine("Enter your solution name : ");string solutionName = Console.ReadLine();Console.WriteLine("Enter your solution password : ");string solutionPassword = ReadPassword();

b. Create the Service Bus URI

string serviceUri = String.Format("sb://{0}/services/{1}/HelloWorldService/",                             ServiceBusEnvironment.DefaultRelayHostName,                             solutionName);

c. Specify the Transport Credentials

TransportClientEndpointBehavior endpointBehavior = new TransportClientEndpointBehavior();endpointBehavior.CredentialType = TransportClientCredentialType.UserNamePassword;endpointBehavior.Credentials.UserName.UserName = solutionName;endpointBehavior.Credentials.UserName.Password = solutionPassword;

d. Create your Service Host

ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService), new Uri(serviceUri));

e. Apply the credentials to the endpoints

foreach (ServiceEndpoint endpoint in serviceHost.Description.Endpoints){    endpoint.Behaviors.Add(endpointBehavior);}

f. Open the Service

serviceHost.Open();

If you have entered the proper credentials, your service will now be hosted at the Service Bus - in the Cloud!

Building the ‘Hello World’ Client

1) Enter your solution name and password

Console.WriteLine("Enter your solution name : ");string solutionName = Console.ReadLine();Console.WriteLine("Enter your solution password : ");string solutionPassword = ReadPassword();

2) Create the Service Bus URI

string serviceUri = String.Format("sb://{0}/services/{1}/HelloWorldService/",                             ServiceBusEnvironment.DefaultRelayHostName,                             solutionName);

3) Specify the Transport Credentials

TransportClientEndpointBehavior endpointBehavior = new TransportClientEndpointBehavior();endpointBehavior.CredentialType = TransportClientCredentialType.UserNamePassword;endpointBehavior.Credentials.UserName.UserName = solutionName;endpointBehavior.Credentials.UserName.Password = solutionPassword;

4) Create our HelloWorld channel

public interface IHelloWorldChannel : IHelloWorldService, IClientChannel { }

5) Create the ChannelFactory

ChannelFactory<IHelloWorldChannel> channelFactory =    new ChannelFactory<IHelloWorldChannel>("HelloWorldRelayEndpoint",                 new EndpointAddress(new Uri(serviceUri)));

6) Apply the credentials to the endpoint and create the service channel

channelFactory.Endpoint.Behaviors.Add(endpointBehavior);IHelloWorldChannel channel = channelFactory.CreateChannel();

7) Open the channel and invoke the HelloWorld() method

channel.Open();Console.WriteLine(channel.HelloWorld());channel.Close();

If everything worked fine, you should get a reply from the Service.

Below is the screenshot of the output from our ‘Hello World’ Service and Client

hello-world-output

You can download the sample below

 

Getting Started with Microsoft .Net Services

Microsoft .Net Services is a part of the recently announced Microsoft Azure Platform. What does Microsoft .Net Services provide ?

Microsoft .Net Services consists of three main components:

  1. Service Bus
  2. Access Control
  3. Workflow Services

They provide a hosting platform where you can develop connected, peer to peer applications that can speak to each other without considering the other complexities such as firewall rules and NAT etc,.

Consider the following diagram:

WebService-Common-Problem

Company A has a web service which is also consumed by Company B. Certainly with all the firewalls and ‘network zones’, this doesn’t look simple to develop. There are at least 3 ‘network zones’ for the web service to cross and get into the cloud and even after reaching Company B, there are at least 2 ‘network zones’ to cross to reach the systems. Depending on the company’s network architecture, these ‘network zones’ can be complex and developers need to write complex code to overcome the firewall and NAT rules. Company B also has to enable firewall rules for the incoming requests/responses from the web service from Company A and vice versa. Certainly this is a complex process!

Microsoft .Net Services addresses the above common problem with the help of Service Bus, which provides a communication infrastructure that developers can make use of when developing such complex applications/services. Service Bus hides the complexity of the firewall/router/NAT and enables to host the service in the cloud. The client can then connect to the service through the Service Bus. However, the messages exchanged in a Service Bus are always authenticated and trusted. The Access Control block provides a Security Token Service (STS) which provides Identity and Management Service. So, the above diagram would now change to,

WebService-servicebus

 

How are things accomplished using this Service Bus? Below is a typical message flow that happens in a Service Bus:

  1. Company A requests a Token by authenticating itself to the STS
  2. When authenticated, STS issues a token to Company A
  3. Company A  can now initiate a connection to it’s Service
  4. Company B requests a Token by authenticating itself to STS
  5. When authenticated, STS issues a token to Company B
  6. Company B can now exchange messages with Company A.

The messages are authenticated and checked for authorization respectively by the STS. This can be described in a diagram as follows:

servicebus-message-flow

In my next post, lets write our first ‘Hello World’ application using Microsoft .Net Services


Creative Commons License
Chaks' Corner Blog by Chakkaradeep Chandran is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Based on a work at www.chakkaradeep.com.
Permissions beyond the scope of this license may be available at http://www.chakkaradeep.com.