托管(4)
WCF也允许开发者在宿主配置文件中列出基地址内容: system.serviceModel services service name = MyNamespace.MyService host baseAddresses add baseAddress = net.tcp://localhost:8001//
|
WCF也允许开发者在宿主配置文件中列出基地址内容:
<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>
创建宿主时,无论在配置文件中找到哪一个基地址,宿主都会使用它,同时还要加上以编程方式提供的基地址。需要特别注意,我们必须确保配置的基地址的样式不能与代码中的基地址的样式重叠。 我们甚至可以针对相同的类型注册多个宿主,只要这些宿主使用了不同的基地址:
Uri baseAddress1 = new Uri("net.tcp://localhost:8001/"); ServiceHost host1 = new ServiceHost(typeof(MyService),baseAddress1); host1.Open(); Uri baseAddress2 = new Uri("net.tcp://localhost:8002/"); ServiceHost host2 = new ServiceHost(typeof(MyService),baseAddress2); host2.Open();
|
然而,这并不包括第8章介绍的使用线程的情况,以这种方式打开多个宿主并无优势可言。此外,如果基地址是配置文件提供的,那么就需要使用ServiceHost的构造函数为相同的类型打开多个宿主。
托管的高级特性
ServiceHost实现的ICommunicationObject接口定义了一些高级特性,如例1-4所示。
例1-4:ICommunicationObject接口 public interface ICommunicationObject { void Open(); void Close(); void Abort(); event EventHandler Closed; event EventHandler Closing; event EventHandler Faulted; event EventHandler Opened; event EventHandler Opening; IAsyncResult BeginClose(AsyncCallback callback,object state); IAsyncResult BeginOpen(AsyncCallback callback,object state); void EndClose(IAsyncResult result); void EndOpen(IAsyncResult result); CommunicationState State {get;} //更多成员 } public enum CommunicationState { Created, Opening, Opened, Closing, Closed, Faulted }
|
|