配置文件方式设置EndPoint(ABC)
基本配置
<system.serviceModel> <services> <service name="MyNamespace.MyService"> <endpoint address="http://localhost:8000/MyService" binding="wsHttpBinding" contract="MyNamespace.IMyContract"/> </service> </services> </system.serviceModel>
为一个Service配置多个结点
<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/...只能有一个默认配置
<netTcpBinding> <binding transactionFlow="true"/> <!--没有name属性--> </netTcpBinding>
代码方式设置EndPoint
基本方式
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();
使用下列构造函数,可以使用配置文件中的指定绑定配置来初始化绑定,或是使用空串来使用默认的绑定配置
public class NetTcpBinding : Binding,... { public NetTcpBinding(string configurationName); //More members }
时间: 2024-11-05 18:46:09