Skip to content

Instantly share code, notes, and snippets.

@bradygaster-zz
Created November 19, 2012 07:22
Show Gist options
  • Save bradygaster-zz/4109405 to your computer and use it in GitHub Desktop.
Save bradygaster-zz/4109405 to your computer and use it in GitHub Desktop.
Connecting Windows Azure Web Sites to On Premises Databases
namespace WebSitesWithOnPremIntegration.Core
{
public class Customer
{
public long Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
}
using System.Data.Entity;
using System.Data.Entity.Migrations;
namespace WebSitesWithOnPremIntegration.Core
{
public class CustomerDataContext : DbContext
{
public CustomerDataContext() : base("CustomerDatabase") { }
public DbSet<Customer> Customers { get; set; }
}
public static class CustomerService
{
public static void Add(Customer customer)
{
var ctx = new CustomerDataContext();
ctx.Customers.Add(customer);
ctx.SaveChanges();
}
}
internal sealed class Configuration :
DbMigrationsConfiguration<CustomerDataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
}
}
using System.Web.Mvc;
using WebSitesWithOnPremIntegration.Core;
namespace WebSitesWithOnPremIntegration.Web.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Customer customer)
{
ServiceBusHelper.Setup().Publish(customer);
return View(new Customer());
}
}
}
@model WebSitesWithOnPremIntegration.Core.Customer
@{
ViewBag.Title = "Registration Form";
}
@section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title</h1>
<h2>Thanks for being a new customer!</h2>
</hgroup>
<p>
Use the form below to provide all your private customer data, which we'll store
in our own, secure, private storage facility, surrounded by armed guards, each
of whom has their very own machine gun and trained Doberman.
</p>
</div>
</section>
}
@using (Html.BeginForm())
{
<div>
<label>Full Name</label>
@Html.TextBoxFor(x => x.Name)
</div>
<div>
<label>City</label>
@Html.TextBoxFor(x => x.City)
</div>
<div>
<label>Country</label>
@Html.TextBoxFor(x => x.Country)
</div>
<input type="submit" value="Save" />
}
using System;
using WebSitesWithOnPremIntegration.Core;
namespace WebSitesWithOnPremIntegration.ServiceBusListener
{
class Program
{
static void Main(string[] args)
{
ServiceBusHelper
.Setup()
.Subscribe<Customer>((customer) =>
{
CustomerService.Add(customer);
Console.WriteLine("Customer {0} from {1}, {2} saved",
customer.Name,
customer.City,
customer.Country);
});
}
}
}
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using System;
using System.Configuration;
namespace WebSitesWithOnPremIntegration.Core
{
public class ServiceBusHelper
{
const string TOPIC_NAME = "Customers";
const string SUBSCRIPTION_NAME = "BasicSubscription";
const string SERVICE_BUS_CONNECTION_STRING = "Microsoft.ServiceBus.ConnectionString";
string _connectionString;
TopicClient _client;
NamespaceManager _namespaceManger;
private ServiceBusHelper()
{
}
private ServiceBusHelper CreateNamespaceManager()
{
if (_namespaceManger == null)
_namespaceManger = NamespaceManager.CreateFromConnectionString(_connectionString);
return this;
}
private ServiceBusHelper CreateTopicIfNotExists()
{
if (!_namespaceManger.TopicExists(TOPIC_NAME))
_namespaceManger.CreateTopic(TOPIC_NAME);
return this;
}
public static ServiceBusHelper Setup()
{
return new ServiceBusHelper
{
_connectionString =
ConfigurationManager.AppSettings[SERVICE_BUS_CONNECTION_STRING]
}
.CreateNamespaceManager()
.CreateTopicIfNotExists();
}
public ServiceBusHelper Publish<T>(T target)
{
if (_client == null)
_client = TopicClient.CreateFromConnectionString(_connectionString, TOPIC_NAME);
_client.Send(new BrokeredMessage(target));
return this;
}
public void Subscribe<T>(Action<T> callback)
{
if (!_namespaceManger.SubscriptionExists(TOPIC_NAME, SUBSCRIPTION_NAME))
_namespaceManger.CreateSubscription(TOPIC_NAME, SUBSCRIPTION_NAME);
var subscriptionClient = SubscriptionClient.CreateFromConnectionString(
_connectionString,
TOPIC_NAME,
SUBSCRIPTION_NAME);
while (true)
{
var message =
subscriptionClient.Receive(TimeSpan.FromSeconds(3));
if (message != null)
{
try
{
if (callback != null)
callback(message.GetBody<T>());
message.Complete();
}
catch
{
message.Abandon();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment