<system.serviceModel> <services> <service name="MyNamespace.MyService"> <endpoint address="http://localhost:8000/MyService" binding="wsHttpBinding" contract="MyNamespace.IMyContract"/> </service> </services> </system.serviceModel>
<service name="MyService"> <endpoint address="http://localhost:8000/MyService" binding="wsHttpBinding" contract="IMyContract"/> <endpoint address="net.tcp://localhost:8001/MyService" binding="netTcpBinding" contract="IMyContract"/> <endpoint address="net.tcp://localhost:8002/MyService" binding="netTcpBinding" contract="IMyOtherContract"/> </service>
<system.serviceModel> <services> <service name="MyNamespace.MyService"> <host> <baseAddresses> <add baseAddress="net.tcp://localhost:8001/"/> <add baseAddress="http://localhost:8002/"/> </baseAddresses> </host> </service> </services> </system.serviceModel>
<system.serviceModel> <services> <service name="MyService"> <endpoint address="net.tcp://localhost:8000/MyService" bindingConfiguration="TransactionalTCP" binding="netTcpBinding" contract="IMyContract"/> <endpoint address="net.tcp://localhost:8001/MyService" bindingConfiguration="TransactionalTCP" binding="netTcpBinding" contract="IMyOtherContract"/> </service> </services> <bindings> <netTcpBinding> <binding name="TransactionalTCP" transactionFlow="true"/> </netTcpBinding> </bindings> </system.serviceModel>
應用到全部沒有直接指定bindingConfiguration的EndPoint。一種綁定類型BasicHttpBinding/netTcpBinding/wsHttpBinding/...只能有一個默認配置app
<netTcpBinding> <binding transactionFlow="true"/> <!--沒有name屬性--> </netTcpBinding>
ServiceHost host = new ServiceHost(typeof(MyService)); Binding wsBinding = new WSHttpBinding(); Binding tcpBinding = new NetTcpBinding(); host.AddServiceEndpoint(typeof(IMyContract),wsBinding, "http://localhost:8000/MyService"); host.AddServiceEndpoint(typeof(IMyContract),tcpBinding, "net.tcp://localhost:8001/MyService"); host.AddServiceEndpoint(typeof(IMyOtherContract),tcpBinding, "net.tcp://localhost:8002/MyService"); host.Open();
Uri tcpBaseAddress = new Uri("net.tcp://localhost:8000/"); ServiceHost host = new ServiceHost(typeof(MyService),tcpBaseAddress); Binding tcpBinding = new NetTcpBinding(); //Use base address as address host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,""); //Add relative address host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,"MyService"); //Ignore base address host.AddServiceEndpoint(typeof(IMyContract),tcpBinding, "net.tcp://localhost:8001/MyService"); host.Open();
ServiceHost host = new ServiceHost(typeof(MyService)); //沒有基地址 NetTcpBinding tcpBinding = new NetTcpBinding(); tcpBinding.TransactionFlow = true; //設置綁定屬性 host.AddServiceEndpoint(typeof(IMyContract),tcpBinding, "net.tcp://localhost:8000/MyService"); host.Open();
使用下列構造函數,能夠使用配置文件中的指定綁定配置來初始化綁定,或是使用空串來使用默認的綁定配置tcp
public class NetTcpBinding : Binding,... { public NetTcpBinding(string configurationName); //More members }
若是沒有在配置文件和代碼中提供終結點,WCF會生成自動生成默認終結點函數
例如代碼:this
[ServiceContract] interface IMyContract {...} [ServiceContract] interface IMyOtherContract {...} class MyService : IMyContract,IMyOtherContract {...} for this hosting code: Uri httpBaseAddress = new Uri("http://localhost:8000/"); Uri tcpBaseAddress = new Uri("net.tcp://localhost:9000/"); Uri ipcBaseAddress = new Uri("net.pipe://localhost/"); ServiceHost host = new ServiceHost(typeof(MyService), httpBaseAddress,tcpBaseAddress,ipcBaseAddress); //在host open以前沒有添加任何終結點 host.Open();
WCF自動生成的終結點以下:spa
<service name = "MyService"> <endpoint name = "BasicHttpBinding_IMyContract" address = "http://localhost:8000/" binding = "basicHttpBinding" contract = "IMyContract" /> <endpoint name = "NetTcpBinding_IMyContract" address = "net.tcp://localhost:9000" binding = "netTcpBinding" contract = "IMyContract" /> <endpoint name = "NetNamedPipeBinding_IMyContract" address = "net.pipe://localhost/" binding = "netNamedPipeBinding" contract = "IMyContract" /> <endpoint name = "BasicHttpBinding_IMyOtherContract" address = "http://localhost:8000/" binding = "basicHttpBinding" contract = "IMyOtherContract" /> <endpoint name = "NetTcpBinding_IMyOtherContract" address = "net.tcp://localhost:9000" binding = "netTcpBinding" contract = "IMyOtherContract" /> <endpoint name = "NetNamedPipeBinding_IMyOtherContract" address = "net.pipe://localhost/" binding = "netNamedPipeBinding" contract = "IMyOtherContract" /> </service>
也能夠經過AddDefaultEndpoints來添加默認結點點。有了這個方法,在已有非終結點的狀況下也中添加默認終結點。code
public class ServiceHost : ... { public void AddDefaultEndpoints(); //More members }
在沒有提供終結點的狀況下,WCF根據基地址的scheme來推斷所須要的綁定。這個就叫Protocol mapping。能夠經過配置文件來改變Protocol mapping的行爲:blog
<system.serviceModel> <protocolMapping> <add scheme = "http" binding = "wsHttpBinding" /> </protocolMapping> </system.serviceModel>
還能夠爲綁定指定配置:ip
<protocolMapping> <add scheme = "http" binding = "wsHttpBinding" bindingConfiguration = "..." /> </protocolMapping>