.NET Services (2) - UserNamePassword Credential Type
1. Using code to implement UserNamePassword Type
Service:
static void Main(string[] args)
{
Console.Write("Your Solution Name: ");
string solutionName = Console.ReadLine();
Console.Write("Your Solution Password: ");
string solutionPassword = Console.ReadLine();
// create the endpoint address in the solution's namespace
Uri address = new Uri(String.Format("sb://{0}/services/{1}/EchoService/", ServiceBusEnvironment.DefaultRelayHostName, solutionName));
// create the credentials object for the endpoint
TransportClientEndpointBehavior userNamePasswordServiceBusCredential = new TransportClientEndpointBehavior();
userNamePasswordServiceBusCredential.CredentialType = TransportClientCredentialType.UserNamePassword;
userNamePasswordServiceBusCredential.Credentials.UserName.UserName = solutionName;
userNamePasswordServiceBusCredential.Credentials.UserName.Password = solutionPassword;
// create the service host reading the configuration
ServiceHost host = new ServiceHost(typeof(EchoService), address);
// add the Service Bus credentials to all endpoints specified in configuration
foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
{
endpoint.Behaviors.Add(userNamePasswordServiceBusCredential);
}
// open the service
host.Open();
Console.WriteLine("Service address: " + address);
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
// close the service
host.Close();
}
[ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public class EchoService : IEchoContract
{
public string Echo(string text)
{
Console.WriteLine("Echoing: {0}", text);
return text;
}
}
[ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public interface IEchoContract
{
[OperationContract]
string Echo(string text);
}
public interface IEchoChannel : IEchoContract, IClientChannel
{
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<!-- Application Binding -->
<netTcpRelayBinding>
<binding name="default" />
</netTcpRelayBinding>
</bindings>
<services>
<!-- Application Service -->
<service name="Microsoft.ServiceBus.Samples.EchoService">
<endpoint name="RelayEndpoint"
contract="Microsoft.ServiceBus.Samples.IEchoContract"
binding="netTcpRelayBinding"
bindingConfiguration="default"
address="" />
</service>
</services>
</system.serviceModel>
</configuration>
Client:
public static void Main(string[] args)
{
Console.Write("Enter the name of the solution you want to connect with: ");
string solution = Console.ReadLine();
Uri serviceUri = new Uri(String.Format("sb://{0}/services/{1}/EchoService/", ServiceBusEnvironment.DefaultRelayHostName, solution));
TransportClientEndpointBehavior userNamePasswordServiceBusCredential = new TransportClientEndpointBehavior();
userNamePasswordServiceBusCredential.CredentialType = TransportClientCredentialType.UserNamePassword;
userNamePasswordServiceBusCredential.Credentials.UserName.UserName = solution;
userNamePasswordServiceBusCredential.Credentials.UserName.Password = "Password01!";
ChannelFactory<IEchoChannel> channelFactory = new ChannelFactory<IEchoChannel>(
"RelayEndpoint", new EndpointAddress(serviceUri));
channelFactory.Endpoint.Behaviors.Add(userNamePasswordServiceBusCredential);
IEchoChannel channel = channelFactory.CreateChannel();
channel.Open();
Console.WriteLine("Enter text to echo (or [Enter] to exit):");
string input = Console.ReadLine();
while (input != String.Empty)
{
try
{
Console.WriteLine("Server echoed: {0}", channel.Echo(input));
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
input = Console.ReadLine();
}
channel.Close();
channelFactory.Close();
}
[ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public interface IEchoContract
{
[OperationContract]
string Echo(string text);
}
public interface IEchoChannel : IEchoContract, IClientChannel
{
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<!-- Application Binding -->
<netTcpRelayBinding>
<binding name="default" />
</netTcpRelayBinding>
</bindings>
<client>
<!-- Application Endpoint -->
<endpoint name="RelayEndpoint"
contract="Microsoft.ServiceBus.Samples.IEchoContract"
binding="netTcpRelayBinding"
bindingConfiguration="default"
address="http://AddressToBeReplacedInCode/" />
</client>
</system.serviceModel>
</configuration>
2. Using config file to implement UserNamePassword type
Service:
private static void Main()
{
Uri address = new Uri(String.Format(CultureInfo.InvariantCulture, "sb://{0}/services/{1}/EchoService/", ServiceBusEnvironment.DefaultRelayHostName, [Solution Name]));
ServiceHost host = new ServiceHost(typeof(EchoService), address);
host.Open();
Console.WriteLine("Service address: " + address);
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
host.Close();
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="serviceBusUserName">
<transportClientEndpointBehavior credentialType="UserNamePassword">
<clientCredentials>
<userNamePassword userName="*****" password="*****"/>
</clientCredentials>
</transportClientEndpointBehavior>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<!-- Application Binding -->
<netTcpRelayBinding>
<binding name="default" />
</netTcpRelayBinding>
</bindings>
<services>
<service name="Service.EchoService">
<endpoint name="RelayEndpoint"
contract="Service.IEchoContract"
binding="netTcpRelayBinding"
bindingConfiguration="default"
behaviorConfiguration="serviceBusUserName"
address="" />
</service>
</services>
</system.serviceModel>
</configuration>
Client:
private static void Main()
{
Console.Write("Enter the name of the user you want to connect with: ");
string serviceUserName = Console.ReadLine();
Uri serviceUri = new Uri(String.Format(CultureInfo.InvariantCulture, "sb://{0}/services/{1}/EchoService/", ServiceBusEnvironment.DefaultRelayHostName, serviceUserName));
ChannelFactory<IEchoChannel> channelFactory = new ChannelFactory<IEchoChannel>("RelayEndpoint", new EndpointAddress(serviceUri));
IEchoChannel channel = channelFactory.CreateChannel();
channel.Open();
Console.WriteLine("Enter text to echo (or [Enter] to exit):");
string 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();
}
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="serviceBusUserName">
<transportClientEndpointBehavior credentialType="UserNamePassword">
<clientCredentials>
<userNamePassword userName="Yang_ForAzureTest" password="Password01!"/>
</clientCredentials>
</transportClientEndpointBehavior>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<!-- Application Binding -->
<netTcpRelayBinding>
<binding name="default" />
</netTcpRelayBinding>
</bindings>
<client>
<!-- Application Endpoint -->
<endpoint name="RelayEndpoint"
contract="Client.IEchoContract"
binding="netTcpRelayBinding"
bindingConfiguration="default"
behaviorConfiguration="serviceBusUserName"
address="http://AddressToBeReplacedInCode/" />
</client>
</system.serviceModel>
</configuration>
posted on 2009-02-25 16:17 Yang - Windows Azure 阅读(460) 评论(0) 编辑 收藏 举报