Hello! .
App Fabric Service Bus. This publication is the second of its kind, dedicated to this mechanism. At first I recall, we met with the definition and basic scenarios of technology and know how to create your first namespaces. Here you can watch it at this link:.
Introduction to AppFabric Service Bus. scenarios. Today, the post will be more technical - will try to create and use a WCF- service by.
Service Bus. Thus, the problem is as follows: to build a WCF- service, which contains a single method. The method will accept a string parameter and immediately return it to the client. This service will try to host a service on the bus. That is, in fact Service Bus will act as a relay messages between the client and the WCF- service. To solve the problem created a new Solution to the following projects:.
EchoService. Client. ,.
EchoService. Server. ,.
EchoService. Source. In the studio, it looks like this:.
EchoService. Client. is a console application through which users can send messages to the server.
EchoService. Server. - This is our server ( also console application), based on which will open WCF- host who runs one we want to WCF- Service. Since both the client and the server requires a contract WCF- service - we put it in a shared assembly called.
EchoService. Source. In this assembly is a single interface, the code is shown below.
using System;.
using System. Collections. Generic;.
using System. Linq;.
using System. Text;.
using System. ServiceModel;.
namespace EchoService. Source.
{.
[ServiceContract (Name = 'EchoContract', Namespace = 'http://samples. microsoft. com / ServiceModel / Relay / ')].
public interface IEchoContract.
{.
[OperationContract].
string Echo (string message);.
}.
}.
Nothing unusual - a standard interface with a single method of Echo, takes a string parameter and returns. In the assembly.
EchoService. Server. is the implementation of this interface - it's a class called.
EchoServer. - The code is also presented below.
using System;.
using System. Collections. Generic;.
using System. Linq;.
using System. Text;.
using EchoService. Source;.
using System. ServiceModel;.
namespace EchoService. Server.
{.
[ServiceBehavior (Name = 'EchoService', Namespace = 'http://samples. microsoft. com / ServiceModel / Relay / ')].
public class EchoServer: IEchoContract.
{.
public string Echo (string message).
{.
Console. WriteLine ('Received: { 0 }', message);.
return message;.
}.
}.
}.
There are also all standard - class implements an interface marked with the attribute and IEchoService.
ServiceBehavior. (thus the class is marked as the implementation of WCF- service). Echo method in addition to returns came in the parameter string back to the client, and displays it in the server console. But the main functionality of the server is contained in a file Program. cs. Let's look at its contents:.
using System;.
using System. Collections. Generic;.
using System. Linq;.
using System. Text;.
using Microsoft. ServiceBus;.
using System. ServiceModel;.
using System. ServiceModel. Description;.
namespace EchoService. Server.
{.
class Program.
{.
static void Main (string [] args).
{.
Console. Title = ' server ';.
Console. Write ('Enter Service Bus namespaces ');.
string serviceNamespaceDomain = Console. ReadLine ();.
Console. Write ('Issuer Name:');.
string issuerName = Console. ReadLine ();.
Console. Write ('Issuer Secret:');.
string issuerSecret = Console. ReadLine ();.
Uri address = ServiceBusEnvironment. CreateServiceUri ('sb', serviceNamespaceDomain, 'EchoService');.
TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior ();.
sharedSecretServiceBusCredential. CredentialType = TransportClientCredentialType. SharedSecret;.
sharedSecretServiceBusCredential. Credentials. SharedSecret. IssuerName = issuerName;.
sharedSecretServiceBusCredential. Credentials. SharedSecret. IssuerSecret = issuerSecret;.
ServiceHost host = new ServiceHost (typeof (EchoServer), address);.
IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings (DiscoveryType. Public);.
foreach (ServiceEndpoint endpoint in host. Description. Endpoints).
{.
endpoint. Behaviors. Add (sharedSecretServiceBusCredential);.
}.
host. Open ();.
Console. WriteLine (' Location of service:' address);.
Console. WriteLine (' Press [Enter] to exit ');.
Console. ReadLine ();.
host. Close ();.
}.
}.
}.
Let's take a detailed look at what 's going on. So, the first to start the service required input data, namely kridenshaly to access.
Service Bus. and the name of namespaces. It is just going in the first lines of the program. It then creates a unique address universal access service ( at the output we get here is the address in this format:.
sb:/ /. servicebus. windows. net / EchoService. ). To access the.
Service Bus. All clients must be authenticated. In our case, the so-called authentication.
SharedSecret. and there is the following code segment:.
TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior ();.
sharedSecretServiceBusCredential. CredentialType = TransportClientCredentialType. SharedSecret;.
sharedSecretServiceBusCredential. Credentials. SharedSecret. IssuerName = issuerName;.
sharedSecretServiceBusCredential. Credentials. SharedSecret. IssuerSecret = issuerSecret;.
This authentication method is similar to the standard scheme, in which steam is passed username / password ( also supports the following authentication schemes:.
Saml. ,.
SimpleWebToken. and.
Unauthenticated. ). So in the end with the standard tools of the assembly.
System. ServiceModel. dll. creates a WCF- host and run our Echo- service ( of course with each access point is set created earlier behavior, ensuring the ability to connect authenticated users only ).
Let's take a look at the source code of the client.
using System;.
using System. Collections. Generic;.
using System. Linq;.
using System. Text;.
using Microsoft. ServiceBus;.
using System. ServiceModel;.
using EchoService. Source;.
namespace EchoService. Client.
{.
class Program.
{.
static void Main (string [] args).
{.
Console. Title = ' Client ';.
Console. Write ('Enter Service Bus namespaces ');.
string serviceNamespaceDomain = Console. ReadLine ();.
Console. Write ('Issuer Name:');.
string issuerName = Console. ReadLine ();.
Console. Write ('Issuer Secret:');.
string issuerSecret = Console. ReadLine ();.
Uri serviceUri = ServiceBusEnvironment. CreateServiceUri ('sb', serviceNamespaceDomain, 'EchoService');.
TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior ();.
sharedSecretServiceBusCredential. CredentialType = TransportClientCredentialType. SharedSecret;.
sharedSecretServiceBusCredential. Credentials. SharedSecret. IssuerName = issuerName;.
sharedSecretServiceBusCredential. Credentials. SharedSecret. IssuerSecret = issuerSecret;.
ChannelFactory. channelFactory = new ChannelFactory. ('RelayEndpoint', new EndpointAddress (serviceUri));.
channelFactory. Endpoint. Behaviors. Add (sharedSecretServiceBusCredential);.
IEchoContract channel = channelFactory. CreateChannel ();.
((ICommunicationObject) channel). Open ();.
Console. WriteLine (' Enter text to send to the server:');.
string input = Console. ReadLine ();.
while (! String. IsNullOrEmpty (input)).
{.
try.
{.
Console. WriteLine (' The server responded: {0 }', channel. Echo (input));.
}.
catch (Exception e).
{.
Console. WriteLine (' Error:' e. Message);.
}.
input = Console. ReadLine ();.
}.
((ICommunicationObject) channel). Close ();.
channelFactory. Close ();.
}.
}.
}.
Algorithm to connect to the bus completely identical to that meter in the back end. The differences lie only in the fact that we do not open a new host, and the open channel with the code here:.
ChannelFactory. channelFactory = new ChannelFactory. ('RelayEndpoint', new EndpointAddress (serviceUri));.
channelFactory. Endpoint. Behaviors. Add (sharedSecretServiceBusCredential);.
IEchoContract channel = channelFactory. CreateChannel ();.
((ICommunicationObject) channel). Open ();. All that's left to do is add a couple of lines of code in the configuration files of both projects. At the back end to add the following configuration:.
At the same client sends the following snippet:.
In both cases indicated the main access point and a new service called Binding.
netTcpRelayBinding. (this type of bindings in the assembly.
Microsoft. ServiceBus. dll. and provides safe and reliable means of communication between computers.
Service Bus. ).
Everything is ready, it remains to be done only a small step before starting the system. And it found on the site kridenshaly to access (this is our.
Issuer Name. and.
Issuer Secret. Required by both the client and server). You can find them in the section.
>AppFabric - Service Bus. namespaces in the property under the name.
Default Key. By clicking on the button.
View. opens a modal window, which is just and will include data necessary for us.
That's all the preparation can be considered completed. To demonstrate, I 'll show you a couple of screenshots showing the operation of the system.
As you can see the system works fine, but for a service we would not have put a considerable effort, the majority of routine work for us to perform a new WCF- Binding, which is in a namespace.
Microsoft. ServiceBus. In this post you can complete the. The source files of the project can be taken at the following link:.
AppFabric Service Bus Echo Service. Very soon we will get acquainted with more progressive and sophisticated variants of Service Bus, but for now thank you for your attention, I hope the publication was useful and interesting:).