Windows azure-AppFabric 一个简单的ServiceBus例子
这里使用ServiceBus特性,向外界暴露一个运行在一内网机器上面,由WCF创建的WebGet Enabled的简单服务。
Code List:
IProduct.cs
IProduct.cs[ServiceContract(Namespace = "http://sample.azure.com/appfabric/servicebus/ProductContract")] public interface IProduct { [OperationContract, WebGet] List<ProductInfo> LookupProducts(); }
ProductService.cs
ProductService .cs[ServiceBehavior(Name = "ProductService", Namespace = "http://sample.azure.com/appfabric/servicebus/ProductService")] public class ProductService : IProduct { public List<ProductInfo> LookupProducts() { List<ProductInfo> infos = new List<ProductInfo>(); for (int i = 0; i < 10; i++) { infos.Add(new ProductInfo { Name = "N" + i.ToString(), ProductID = i }); } return infos; } }
Host这个服务
Host string serviceNamespace = ConfigurationManager.AppSettings["NameSpace"]; Uri address = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "ProductService"); WebServiceHost host = new WebServiceHost(typeof(ProductService), address); host.Open(); Console.WriteLine(address.ToString()); Console.WriteLine("To close this service, please press any key."); Console.ReadKey(); host.Close();
配置服务
Config<?xml version="1.0"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> <system.serviceModel> <bindings> <!-- Application Binding --> <NetTcpRelayBinding> <binding name="default"> <!-- Turn off client authentication so that client does not need to present credential through browser or fiddler --> <security relayClientAuthenticationType="None" /> </binding> </NetTcpRelayBinding> <webHttpRelayBinding> <binding name="default"> <!-- Turn off client authentication so that client does not need to present credential through browser or fiddler --> <security relayClientAuthenticationType="None" /> </binding> </webHttpRelayBinding> </bindings> <services> <service name="SimpleService.ProductService" behaviorConfiguration="default"> <endpoint name="RelayEndpoint" contract="SharedLib.IProduct" binding="webHttpRelayBinding" bindingConfiguration="default" behaviorConfiguration="sharedSecretClientCredentials" address="" /> </service> </services> <extensions> <bindingExtensions> <add name="NetTcpRelayBinding" type="Microsoft.ServiceBus.Configuration.NetTcpRelayBindingCollectionElement, Microsoft.ServiceBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <add name="webHttpRelayBinding" type="Microsoft.ServiceBus.Configuration.WebHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </bindingExtensions> <behaviorExtensions> <add name="transportClientEndpointBehavior" type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, Microsoft.ServiceBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </behaviorExtensions> </extensions> <behaviors> <endpointBehaviors> <behavior name="sharedSecretClientCredentials"> <transportClientEndpointBehavior credentialType="SharedSecret"> <clientCredentials> <sharedSecret issuerName="owner" issuerSecret="**********"/> </clientCredentials> </transportClientEndpointBehavior> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="default"> <serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <appSettings > <add key="NameSpace" value="***"/> </appSettings> </configuration>