<system.serviceModel> <services> <service name="MyNamespace.MyService"> <endpoint address="http://localhost:8000/MyService" binding="wsHttpBinding" contract="MyNamespace.IMyContract"/> </service> </services> </system.serviceModel>
应用到所有没有直接指定bindingConfiguration的EndPoint。一种绑定类型BasicHttpBinding/netTcpBinding/wsHttpBinding/...只能有一个默认配置
<netTcpBinding> <binding transactionFlow="true"/> <!--没有name属性--> </netTcpBinding>
使用下列构造函数,可以使用配置文件中的指定绑定配置来初始化绑定,或是使用空串来使用默认的绑定配置
public class NetTcpBinding : Binding,... { public NetTcpBinding(string configurationName); //More members }
如果没有在配置文件和代码中提供终结点,WCF会生成自动生成默认终结点
例如代码:
[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自动生成的终结点如下:
<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来添加默认结点点。有了这个方法,在已有非终结点的情况下也中添加默认终结点。
public class ServiceHost : ... { public void AddDefaultEndpoints(); //More members }
在没有提供终结点的情况下,WCF根据基地址的scheme来推断所需要的绑定。这个就叫Protocol mapping。可以通过配置文件来改变Protocol mapping的行为:
<system.serviceModel> <protocolMapping> <add scheme = "http" binding = "wsHttpBinding" /> </protocolMapping> </system.serviceModel>
还可以为绑定指定配置:
<protocolMapping> <add scheme = "http" binding = "wsHttpBinding" bindingConfiguration = "..." /> </protocolMapping>
转载于:https://www.cnblogs.com/vicsmb/p/5244472.html
相关资源:数据结构—成绩单生成器