Created
November 19, 2012 07:22
-
-
Save bradygaster-zz/4109405 to your computer and use it in GitHub Desktop.
Connecting Windows Azure Web Sites to On Premises Databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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" /> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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