Keeping your original WCF config file and making it run in Azure as a worker role

If you get 405 errors while trying to access the WCF service with GET enabled in the config file, note that you will need to modify the config file behaviors area right next to where you enable httpget to the DNS name of the azure deployment: x.cloudapp.net

Otherwise, no matter what you do, you will not be able to get the browser to access the WCF URL. This is when you have HTTP end points for the WCF service with internal endpoint of azure.

if (!InAzureCloud && !InAzureEnvironment) { return; }

RoleInstanceEndpoint externalEndPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["EndPoint"];Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);ServicesSection servicesSection = serviceModel.Services;
            
var serviceElement = (from System.ServiceModel.Configuration.ServiceElement p in servicesSection.Services
                      where p.Endpoints != null &&
                      p.Endpoints.Count > 0 &&
                      p.Endpoints[0].Contract == contractType.FullName
                      select p).FirstOrDefault();
 
if (serviceElement != null)
{
   string azureAddressUrl = serviceElement.Endpoints[0].Address.ToString();
   azureAddressUrl= azureAddressUrl.Replace(serviceElement.Endpoints[0].Address.Host + ":" + serviceElement.Endpoints[0].Address.Port.ToString(), 
                                                    externalEndPoint.IPEndpoint.ToString());
 
   BasicHttpBindingElement bindingElement = (from BasicHttpBindingElement p in serviceModel.Bindings.BasicHttpBinding.Bindings
                                             where p.Name == serviceElement.Endpoints[0].BindingConfiguration
                                             select p).FirstOrDefault();
 
   if (bindingElement == null)
   {
        return;
   }
 
   BasicHttpBinding selectedBinding = new BasicHttpBinding();
   bindingElement.ApplyConfiguration(selectedBinding);
   selectedBinding.Name = bindingElement.Name;
 
   this.AddServiceEndpoint(contractType, selectedBinding, azureAddressUrl);
}

Comments

Popular posts from this blog

Tutorial: Using Google Cloud Storage from C# and .NET

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

The ridiculous interview experience