概述
終結點地址定義
AddressHeader header = AddressHeader.CreateAddressHeader("basic", "http://www.cnblogs.com/terrylee", "Terrylee");
指定終結點地址
<service name="TerryLee.WCFAddressing.Service.CalculatorService" behaviorConfiguration="calculatorBehavior"> <endpoint address="[url]http://localhost:8887/CalculatorService[/url]" binding ="basicHttpBinding" contract="TerryLee.WCFAddressing.Contract.ICalculator"> </endpoint> </service>
using (ServiceHost calculatorServiceHost = new ServiceHost(typeof(CalculatorService), new Uri("http://localhost:8887/CalculatorService"))) { calculatorServiceHost.Opened += delegate { Console.WriteLine("Service begin to listen via the Address:{0}", calculatorServiceHost.BaseAddresses[0].ToString()); }; calculatorServiceHost.Open(); Console.Read(); }
<service name="TerryLee.WCFAddressing.Service.CalculatorService" behaviorConfiguration="calculatorBehavior"> <host> <baseAddresses> <add baseAddress="[url]http://localhost:8887/Calculator[/url]"/> </baseAddresses> </host> <endpoint address="myservice1" binding ="basicHttpBinding" contract="TerryLee.WCFAddressing.Contract.ICalculator"> </endpoint> <endpoint address="myservice2" binding ="wsHttpBinding" contract="TerryLee.WCFAddressing.Contract.ICalculator"> </endpoint> </service>
<wsdl:service name="CalculatorService"> <wsdl:port name="BasicHttpBinding_ICalculator" binding="tns:BasicHttpBinding_ICalculator"> <soap:address location="[url]http://localhost:8887/Calculator/myservice1[/url]" /> </wsdl:port> <wsdl:port name="WSHttpBinding_ICalculator" binding="tns:WSHttpBinding_ICalculator"> <soap12:address location="[url]http://localhost:8887/Calculator/myservice2[/url]" /> <wsa10:EndpointReference> <wsa10:Address>[url]http://localhost:8887/Calculator/myservice2[/url]</wsa10:Address> <Identity xmlns="[url]http://schemas.xmlsoap.org/ws/2006/02/addressingidentity[/url]"> <Upn>TerryLee-PC\TerryLee</Upn> </Identity> </wsa10:EndpointReference> </wsdl:port> </wsdl:service>
<service name="TerryLee.WCFAddressing.Service.CalculatorService" behaviorConfiguration="calculatorBehavior"> <host> <baseAddresses> <add baseAddress="[url]http://localhost:8887/Calculator[/url]"/> </baseAddresses> </host> <endpoint address="" binding ="wsHttpBinding" contract="TerryLee.WCFAddressing.Contract.ICalculator"> </endpoint> <endpoint address="myservice2" binding ="wsHttpBinding" contract="TerryLee.WCFAddressing.Contract.ICalculator"> </endpoint> <endpoint address="[url]http://localhost:8886/CalculatorService[/url]" binding ="wsHttpBinding" contract="TerryLee.WCFAddressing.Contract.ICalculator"> </endpoint> <endpoint address="net.tcp://localhost:8885/Calculator" binding ="netTcpBinding" contract="TerryLee.WCFAddressing.Contract.ICalculator"> </endpoint> </service>
ServiceDescription desc = calculatorServiceHost.Description; foreach (ServiceEndpoint endpoint in desc.Endpoints) { Console.WriteLine("Endpoint - address: {0}", endpoint.Address); Console.WriteLine(" binding: {0}", endpoint.Binding.Name); Console.WriteLine(" contract: {0}", endpoint.Contract.Name); }
元數據中終結點地址
<wsdl:service name="CalculatorService"> <wsdl:port name="WSHttpBinding_ICalculator" binding="tns:WSHttpBinding_ICalculator"> <soap12:address location="[url]http://localhost:8887/Calculator[/url]" /> <wsa10:EndpointReference> <wsa10:Address>[url]http://localhost:8887/Calculator[/url]</wsa10:Address> <Identity xmlns="[url]http://schemas.xmlsoap.org/ws/2006/02/addressingidentity[/url]"> <Upn>TerryLee-PC\TerryLee</Upn> </Identity> </wsa10:EndpointReference> </wsdl:port> </wsdl:service>
自定義尋址報頭
using (ServiceHost calculatorServiceHost = new ServiceHost(typeof(CalculatorService), new Uri("http://localhost:8887/CalculatorService"))) { calculatorServiceHost.Opened += delegate { Console.WriteLine("Service begin to listen via the Address:{0}", calculatorServiceHost.BaseAddresses[0].ToString()); }; AddressHeader header = AddressHeader.CreateAddressHeader("basic", "http://www.cnblogs.com/terrylee", "Terrylee"); EndpointAddress ea = new EndpointAddress( new Uri("http://localhost:8887/CalculatorService"), header); calculatorServiceHost.Description.Endpoints.Add( new ServiceEndpoint( ContractDescription.GetContract(typeof(ICalculator)), new WSHttpBinding(), ea)); ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; calculatorServiceHost.Description.Behaviors.Add(behavior); calculatorServiceHost.Open(); Console.Read(); }
<wsdl:service name="CalculatorService"> <wsdl:port name="WSHttpBinding_ICalculator" binding="tns:WSHttpBinding_ICalculator"> <soap12:address location="[url]http://localhost:8887/CalculatorService[/url]" /> <wsa10:EndpointReference> <wsa10:Address>[url]http://localhost:8887/CalculatorService[/url]</wsa10:Address> <wsa10:ReferenceParameters> <basic xmlns="[url]http://www.cnblogs.com/terrylee[/url]">Terrylee</basic> </wsa10:ReferenceParameters> <Identity xmlns="[url]http://schemas.xmlsoap.org/ws/2006/02/addressingidentity[/url]"> <Upn>TerryLee-PC\TerryLee</Upn> </Identity> </wsa10:EndpointReference> </wsdl:port> </wsdl:service>
<s:Envelope xmlns:s="[url]http://schemas.xmlsoap.org/soap/envelope/[/url]"> <s:Header> <basic xmlns="[url]http://www.cnblogs.com/terrylee[/url]">Terrylee</basic> <To s:mustUnderstand="1">[url]http://localhost:8887/CalculatorService[/url]</To> <Action s:mustUnderstand="1">[url]http://tempuri.org/ICalculator/Add[/url]</Action> </s:Header> <s:Body> <Add xmlns="[url]http://tempuri.org/[/url]"> <x>1</x> <y>2</y> </Add> </s:Body> </s:Envelope>
<service name="TerryLee.WCFAddressing.Service.CalculatorService" behaviorConfiguration="calculatorBehavior"> <host> <baseAddresses> <add baseAddress="[url]http://localhost:8887/Calculator[/url]"/> </baseAddresses> </host> <endpoint address="" binding ="wsHttpBinding" contract="TerryLee.WCFAddressing.Contract.ICalculator"> <headers> <basic xmlns="[url]http://www.cnblogs.com/terrylee[/url]">Terrylee</basic> </headers> </endpoint> </service>
結束語
0
收藏
Ctrl+Enter 發佈
發佈
取消