.Net Service Bus: How to make training kit examples authenticate without problems

I’m trying out Azure Training Kit examples to study cloud stuff that should hit the streets (or sky) later this year. I faced some trouble when trying to connect with .Net Service Bus using settings mentioned in examples (netTcpRelayBinding and CardSpace). I tried hard but CardSpace authentication failed every time. My final solution was simple: I moved to username and password authentication scheme.

I found some very good information from David-Pur Maor’s blog entry Azure .NET Services – A Twitter Service Bus. There is one more reason why you should read this entry – it introduces a Twitter service bus. :)

Basically we need to change the configuration files so there is no information about authentication schemes. Service configuration file looks like this.

<?xml version="1.0" encoding="utf-8" ?>
<
configuration>
  <system.serviceModel>
    <bindings>
      <netTcpRelayBinding>
        <binding name="default" />
      </netTcpRelayBinding>
    </bindings>
 
   
<services>
      <service name="Service.EchoService">
        <endpoint name="RelayEndpoint"
             contract="Service.IEchoContract"
             binding="netTcpRelayBinding"
        />
      </service>
    </services>
 
 
</system.serviceModel
>
</
configuration
>

As a next thing we have to modify Service and Client Program.cs files. You can take contents of these files with copy and paste (I made no corrections to line breaks because you anyway delete them after copying this code).

Program.cs (Service)

using System;
using System.ServiceModel;
using
Microsoft.ServiceBus;
 

namespace
Service
{
   
class Program
    {
       
static void Main(string
[] args)
        {
           
Console.Write("Please enter the solution name to use in this lab: "
);
           
var account = Console
.ReadLine();
 
           
Console.Write("Please enter the solution password: "
);
           
var passwd = Console
.ReadLine();
 
           
var address = ServiceBusEnvironment.CreateServiceUri("sb", account, "EchoService"
);
           
var host = new ServiceHost(typeof
(EchoService), address);
 
           
var behavior = new TransportClientEndpointBehavior
();
            behavior.CredentialType =
TransportClientCredentialType
.UserNamePassword;
            behavior.Credentials.UserName.UserName = account;
            behavior.Credentials.UserName.Password = passwd;
 
           
foreach (var endpoint in
host.Description.Endpoints)
            {
                endpoint.Behaviors.Add(behavior);
            }
 
            host.Open();
           
Console.WriteLine("Service address: "
+ address);
           
Console.WriteLine("Press [Enter] to exit"
);
           
Console.ReadLine();
            host.Close();
        }
    }
}

Program.cs (Client)

using System;
using
System.ServiceModel;
 

using
Microsoft.ServiceBus;
 

namespace
Client
{
   
class Program
    {
       
static void Main(string
[] args)
        {
           
Console.Write("Enter the name of the solution you want to connect with: "
);
           
var solutionName = Console
.ReadLine();
           
Console.Write("Please enter the solution password: "
);
           
var pass = Console
.ReadLine();
 
           
var serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", solutionName, "EchoService"
);
           
var address = new EndpointAddress
(serviceUri);
 
           
var channelFactory = new ChannelFactory<IEchoChannel>("RelayEndpoint"
, address);
 
           
var behavior = new TransportClientEndpointBehavior
();
            behavior.CredentialType =
TransportClientCredentialType
.UserNamePassword;
            behavior.Credentials.UserName.UserName = solutionName;
            behavior.Credentials.UserName.Password = pass;
            channelFactory.Endpoint.Behaviors.Add(behavior);
 
           
IEchoChannel
channel = channelFactory.CreateChannel();
            channel.Open();
 
           
Console.WriteLine("Enter text to echo (or [Enter] to exit):"
);
           
var input = Console
.ReadLine();
           
while (!String
.IsNullOrEmpty(input))
            {
               
try
                {
                   
Console.WriteLine("Server echoed: {0}"
, channel.Echo(input));
                }
               
catch (Exception
e)
                {
                   
Console.WriteLine("Error: "
+ e.Message);
                }
                input =
Console.ReadLine();
            }
 
            channel.Close();
            channelFactory.Close();
        }
    }
}

David’s Twitter example code contains one more hidden gem – method you can use to let users safely insert passwords on command line. No characters are shown, only asterisks.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    One thought on “.Net Service Bus: How to make training kit examples authenticate without problems

    • September 14, 2011 at 7:30 am
      Permalink

      Yes there should realize the reader to RSS my feed to RSS commentary, quite simply

    Leave a Reply

    Your email address will not be published. Required fields are marked *