Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Messenger" /> class.
 /// </summary>
 /// <param name="servcieModel">The servcie model.</param>
 public Messenger(string endPointAddress, string contracName, string assemblyName, string serviceName)
 {
     this.model = new RouteMeModel()
     {
         ContractName     = contracName,
         EndPointAddress  = endPointAddress,
         FullAssemblyName = assemblyName,
         ServiceName      = serviceName
     };
 }
 /// <summary>
 /// Adds the new service entry.
 /// </summary>
 /// <param name="msg">The MSG.</param>
 private void AddNewServiceEntry(RouteMeModel msg)
 {
     using (var ctx = new RouteContext())
     {
         ctx.ChangeTracker.DetectChanges();
         msg.SericeUID = Guid.NewGuid().ToString();
         ctx.Services.Add(msg);
         ctx.SaveChanges();
         //Add a route for the service
         AddServiceBusEntry(msg);
     }
 }
 /// <summary>
 /// Checks if routing entry exists.
 /// </summary>
 /// <param name="msg">The MSG.</param>
 /// <returns></returns>
 private bool CheckIfRoutingEntryExists(RouteMeModel msg)
 {
     using (var ctx = new RouteContext())
     {
         var entry = (from service in ctx.Services
                      where service.ServiceName.Equals(msg.ServiceName) && msg.ContractName.Equals(msg.ContractName)
                      select service).FirstOrDefault();
         if (entry == null)
         {
             return(false);
         }
         else
         {
             return(true);
         }
     }
 }
        /// <summary>
        /// Adds the service bus entry.
        /// </summary>
        /// <param name="message">The message.</param>
        private void AddServiceBusEntry(RouteMeModel message)
        {
            //Load the contract assembly from blob storage
            //Get the current type of contract to add to the client endpoint
            var storageConnection = CloudConfigurationManager.GetSetting("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");

            var cloudStorage = new ReliableCloudBlobStorage(StorageAccountInfo.Parse(storageConnection));

            var contractAssemblyContainer = CloudConfigurationManager.GetSetting("AssemblyContainerName");

            var contractAssemblyName = CloudConfigurationManager.GetSetting("ContractAssemblyName");

            byte[] data = null;

            using (MemoryStream mstream = new MemoryStream())
            {
                var gotIt = cloudStorage.Get(contractAssemblyContainer, contractAssemblyName, mstream);
                if (gotIt)
                {
                    //Get the byte content. This methods does not care about the position
                    data = mstream.ToArray();
                }
            }

            //Now we load the contract assembly
            var assembly = Assembly.Load(data);

            Type contractType = assembly.GetType(message.ContractName);

            //The contract description we use
            var conDesc = ContractDescription.GetContract(contractType);

            var HTTPbinding = new BasicHttpBinding();

            var currentServiceEndPoint = new ServiceEndpoint(
                conDesc,
                HTTPbinding,
                new EndpointAddress(message.EndPointAddress));

            currentServiceEndPoint.Name = message.ServiceName;

            var routerMainEndpoint = owner.Description.Endpoints.Where(ep => ep.Name == "RouterMain").FirstOrDefault();

            var conDescRouter = ContractDescription.GetContract(typeof(IRequestReplyRouter));
            var rEndPoint     = new ServiceEndpoint(conDescRouter, new BasicHttpBinding(), new EndpointAddress(routerMainEndpoint.Address.Uri.OriginalString + "/" + message.ServiceName));

            rEndPoint.Name = message.ServiceName;

            this.owner.AddServiceEndpoint(rEndPoint);

            var addressFilter = new EndpointAddressMessageFilter(new EndpointAddress(routerMainEndpoint.Address.Uri.OriginalString + "/" + message.ServiceName));

            //We don't want to route on headers only
            rc.RouteOnHeadersOnly = false;

            //Add the filter table
            rc.FilterTable.Add(addressFilter, new List <ServiceEndpoint>()
            {
                currentServiceEndPoint
            });

            //Apply the dynamic configuration
            this.owner.Extensions.Find <RoutingExtension>().ApplyConfiguration(rc);
        }