Windows Azure Service Bus Relay Service

Source: Internet
Author: User

The Windows Azure Service Bus allows a public connection point between the inside and outside of the Web service to connect internal and external services without changing the Enterprise Firewall or other security configurations.

When using Azure cloud services, it is not scientific to use IP addresses to establish connections due to scaling. The relay service can act as a good public connection point. Of course, it is generally recommended to use a more scientific queue)

 

There is a relay service in the Azure service bus. It is a service that resident in the cloud to help with the auxiliary connection work, the client call requests are forwarded to the server through the Azure Relay Service.

The relay service, regardless of the client or server, is more like an address used to represent a service. If everyone connects to the same address, they can communicate with each other.

 

650) this. width = 650; "title =" image "style =" border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/194959B56-0.png "" 472 "height =" 384 "/>

Generally, enterprise applications have firewalls, and enterprises are usually restricted by firewalls when providing external services, so they have to configure additional firewalls.

 

With the service bus, enterprise applications can be released without the need for additional firewall configurations.

When the relay service in any mode is unavailable in other modes, it will eventually try to connect port 80 to port 443, in general, enterprise firewalls do not block these two ports. Enterprises always need to access Internet cafes)

 

Creating a relay service is simple, basically equivalent to creating a service bus, because the relay service cannot be created separately and can only be created programmatically.

You can create a service bus at the Azure Portal

650) this. width = 650; "title =" image "style =" border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/1949592407-1.png "" 357 "height =" 389 "/>

Click "add" and enter the namespace. The namespace is available and has not been registered.) Select the region and subscribe, and create a service bus upon confirmation.

After creation

650) this. width = 650; "title =" image "style =" border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/1949592155-2.png "" 294 "height =" 317 "/>

Click the connection information below

Then find the default issuer and default key

This is the credential connecting to the service bus in the future. You need to record it and save it...

 

 

The following roughly demonstrates how to use the service bus

 

The following uses WCF as an example to demonstrate how to program using the service bus.

The service bus can be found in the Microsoft. ServiceBus. dll assembly. Most types exist in the Microsoft. ServiceBus namespace.

You can find the latest version in the reference Nuget package of)

650) this. width = 650; "title =" image "style =" border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/194959B09-3.png "" 360 "height =" 68 "/>

Later, I learned that in the relay service, WCF does not use Orthodox systems such as wshttpbinding or nettcpbinding, but uses the binding of the relay service special edition.

However, the relay service is well compatible with the original WCF programming architecture, and its special binding type is basically followed by a relay

For example, NetTcpRelayBinding and WS2007HttpRelayBinding. There are also several relay service-specific bindings, such as NetOnewayRelayBinding and NetEventRelayBinding.

 

The programming of the relay service is basically the same as that of the common WCF programming model.

But there are also some differences. When connecting to the service bus, you must first use the aforementioned issuer and issuer key as the information to connect to the service bus. You always need to know which service bus you are connected ?)

// Defines a RelayCredentials attribute of the TransportClientEndpointBehavior type.
string ownner = "ServiceBus.Issuer";
string token = "ServiceBus.IssuerToken";
// ServiceBus token first checks the Azure configuration file. If the local App. config is not read for local debugging)
try
{
    this.issuer = RoleEnvironment.GetConfigurationSettingValue(ownner);
    this.issuertoken = RoleEnvironment.GetConfigurationSettingValue(token);
}
catch
{
    this.issuer = ConfigurationManager.AppSettings[ownner];
    this.issuertoken = ConfigurationManager.AppSettings[token];
}
RelayCredentials = new TransportClientEndpointBehavior (); // configure the Service Bus shared key
RelayCredentials.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuer, issuertoken);

 

Because many relay services are used on cloud services, it is generally used to read from the configuration file cscfg in Role, if it is not the cloud service environment, an exception will be thrown and the local configuration file app will be read in the catch. config or web. config)

If the read succeeds, the value is assigned to RelayCredentials. TokenProvider through TokenProvider. CreateSharedSecretTokenProvider (issuer, issuertoken ).

Note that the relaycredenior here is an EndpointBehavior

 

With this, you can program most of them like normal WCF.

// Generate ServiceHost
// ServiceInstanc is a hypothetical service instance
ServiceHost sh = new ServiceHost(typeof(ServiceInstance));
// Service Bus URI
// Note that all protocols except http and https use the sb type
// For example, all files such as net. tcp must start with sb.
// The three parameters are the service names of the Protocol namespace.
// The namespace must be consistent with the namespace used to apply for the Service Bus
Uri address = ServiceBusEnvironment.CreateServiceUri("sb", servicenNamespace, serviceName); 
// Add the endpoint to ServiceHost
// Use NettcpRelayBinding
// ServiceContract is the assumed service contract
sh.AddServiceEndpoint(typeof(ServiceContract), new NettcpRelayBinding(), address);
// Add a Service Bus token to all endpoints
foreach (var endpoint in sh.Description.Endpoints) 
{
Endpoint. EndpointBehaviors. Add (RelayCredentials); // the Endpoint of the Service token obtained in the previous section
} // Foreach is a bit superfluous here, but if there are many endpoints, you can add them in this way.
Sh. Open (); // start the service

 

 

Once the service bus is successfully started and connected to the relay service, you can view the related information in the Azure portal.

650) this. width = 650; "title =" image "style =" border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/1949592I9-4.png "" 493 "height =" 189 "/>

There is no connection information here. If there is a connection, there will be corresponding information here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.