Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

.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.


kick it on DotNetKicks.com pimp it Shout it

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# May 31, 2009 4:12 PM

DotNetBurner - Azure said:

DotNetBurner - burning hot .net content

# May 31, 2009 4:14 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# May 31, 2009 4:15 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 31, 2009 4:19 PM

Gunnar Peipman's ASP.NET blog said:

Here you can find my list of Windows Azure postings Using Windows Azure BLOB storage with PHP Windows

# December 5, 2009 9:01 AM

hooher tod said:

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

# September 14, 2011 3:30 AM