Windows azure-AppFabric 一個簡單的ServiceBus例子

來源:互聯網
上載者:User

這裡使用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>

運行效果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.