![clipboard[9] clipboard[9]](http://image58.360doc.com/DownloadImg/2013/01/3110/30053011_2.png)
运行按钮事件代码:
1: private void button1_Click(object sender, EventArgs e)
2: {
3: if (button1.Tag == "run")
4: {
5: ServiceStart();
6: button1.Text = "停止(&X)";
7: button1.Tag = "stop";
8: }
9: else
10: {
11: button1.Text = "运行(&R)";
12: button1.Tag = "run";
13: }
14: }
15:
ServiceStart() 函数代码:
1: private List<ServiceHost> serviceHosts = new List<ServiceHost>();
2: private void ServiceStart()
3: {
4:
5: #region 初?始?化ˉ serviceHosts
6: if (serviceHosts != null)
7: {
8: foreach (ServiceHost t in serviceHosts)
9: {
10: if (t != null)
11: t.Close();
12: }
13: }
14: else
15: {
16: serviceHosts = new List<ServiceHost>();
17: }
18: #endregion
19:
20: string serviceAddress = string.Format("net.tcp://{0}:{1}", "127.0.0.1", "8000");
21:
22: Dictionary<Type,Type> sevtypes=new Dictionary<Type,Type>();
23: sevtypes.Add(typeof(IService1),typeof(Service1));
24: sevtypes.Add(typeof(IService2), typeof(Service2));
25:
26: string endpointAddress = string.Empty;
27: string tName = string.Empty;
28: StringBuilder msgService = new StringBuilder();
29: foreach (var item in sevtypes)
30: {
31: tName = item.Key.Name.Substring(1);
32: endpointAddress = serviceAddress + tName;
33: if (!serviceAddress.EndsWith("/"))
34: endpointAddress = string.Format("{0}/{1}", serviceAddress, tName);
35: ServiceHost serviceHost = new ServiceHost(item.Value, new Uri(endpointAddress));
36:
37: //加载元数据结点
38: ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
39: serviceHost.Description.Behaviors.Add(smb);
40: serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
41: //加载NetTcpBinding结点
42: NetTcpBinding netTcpBinding = new NetTcpBinding();
43: netTcpBinding.Security.Mode = SecurityMode.None;
44: netTcpBinding.ReceiveTimeout = TimeSpan.Parse("00:10:00");
45: netTcpBinding.MaxBufferPoolSize = 2147483647;
46: netTcpBinding.MaxBufferSize = 2147483647;
47: netTcpBinding.MaxConnections = 10;
48:
49: netTcpBinding.ReaderQuotas.MaxDepth = 2147483647;
50: netTcpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
51: netTcpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
52: netTcpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
53: netTcpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
54: netTcpBinding.MaxReceivedMessageSize = 2147483647;
55: serviceHost.AddServiceEndpoint(item.Key, netTcpBinding, endpointAddress);
56:
57: serviceHost.Opened += delegate
58: {
59: msgService.AppendLine(string.Format("{0}开始监听 Uri 为 :{1}/mex", tName, endpointAddress.ToString()));
60: };
61:
62: serviceHost.Open();
63: serviceHosts.Add(serviceHost);
64: }
65: this.textBox1.Text = msgService.ToString();
66: }
67:
代码约定:
每次增加服务和契约的时候都需要同时添加 Dictionary 要不然系统不会启动新增加的服务。
服务地址:
net.tcp://127.0.0.1:8000/服务名/mex

上面的例子生成如下地址:
net.tcp://127.0.0.1:8000/Service1/mex
net.tcp://127.0.0.1:8000/Service2/mex
把 ”Dictionary ”改成配置文件形式,也就通用了。
网上的一个使用配置文件的例子:
配置文件如下:
1: <system.serviceModel>
2: <services>
3: <service name="ChinaQueue.Restaurant.WCFService.ServiceImplementations.PrinterSer" behaviorConfiguration="sb">
4: <host>
5: <baseAddresses>
6: <add baseAddress="net.tcp://localhost:8081" />
7: </baseAddresses>
8: </host>
9: <endpoint name="NetTcpBinding_IPrinterServer" address="PrinterSer" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IServer" contract="ChinaQueue.Restaurant.WCFService.ServiceContracts.IPrinterSer" />
10: <endpoint name="MexTcpBinding_IPrinterServer" address="Printer" binding="mexTcpBinding" contract="IMetadataExchange" />
11: </service>
12: <service name="ChinaQueue.Restaurant.WCFService.ServiceImplementations.CounterSer" behaviorConfiguration="sb">
13: <host>
14: <baseAddresses>
15: <add baseAddress="net.tcp://localhost:8081" />
16: </baseAddresses>
17: </host>
18: <endpoint name="NetTcpBinding_ICounterServer" address="CounterSer" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IServer" contract="ChinaQueue.Restaurant.WCFService.ServiceContracts.ICounterSer" />
19: <endpoint name="MexTcpBinding_ICounterServer" address="Counter" binding="mexTcpBinding" contract="IMetadataExchange" />
20: </service>
21: <service name="ChinaQueue.Restaurant.WCFService.ServiceImplementations.QuerySer" behaviorConfiguration="sb">
22: <host>
23: <baseAddresses>
24: <add baseAddress="net.tcp://localhost:8081" />
25: </baseAddresses>
26: </host>
27: <endpoint name="NetTcpBinding_IQueryServer" address="QuerySer" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IServer" contract="ChinaQueue.Restaurant.WCFService.ServiceContracts.IQuerySer" />
28: <endpoint name="MexTcpBinding_IQueryServer" address="Query" binding="mexTcpBinding" contract="IMetadataExchange" />
29: </service>
30: </services>
31: <behaviors>
32: <serviceBehaviors>
33: <behavior name="sb">
34: <serviceMetadata />
35: <serviceDebug includeExceptionDetailInFaults="true" />
36: </behavior>
37: </serviceBehaviors>
38: </behaviors>
39: <bindings>
40: <netTcpBinding>
41: <binding name="NetTcpBinding_IServer">
42: <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
43: <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
44: <security mode="None">
45: <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
46: <message clientCredentialType="Windows" />
47: </security>
48: </binding>
49: </netTcpBinding>
50: </bindings>
51: <client>
52: <endpoint address="net.tcp://localhost:8081/PrinterSer" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IServer" contract="ChinaQueue.Restaurant.WCFService.ServiceContracts.IPrinterSer" name="NetTcpBinding_IPrinterServer" />
53: <endpoint address="net.tcp://localhost:8081/CounterSer" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IServer" contract="ChinaQueue.Restaurant.WCFService.ServiceContracts.ICounterSer" name="NetTcpBinding_ICounterServer" />
54: <endpoint address="net.tcp://localhost:8081/QuerySer" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IServer" contract="ChinaQueue.Restaurant.WCFService.ServiceContracts.IQuerySer" name="NetTcpBinding_IQueryServer" />
55: </client>
56: </system.serviceModel>
57:
以上是三个服务的配置,
然后在Main方法中Host这三个服务,以下是如何从配置文件中读取服务并进行Host的
System.ServiceModel.Configuration
1: Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
2: ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
3: foreach (ServiceElement el in svcmod.Services.Services)
4: {
5: Type svcType = Type.GetType(el.Name + "," + "ChinaQueue.Restaurant.WCFService");
6: if (svcType == null)
7: throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");
8: ServiceHost aServiceHost = new ServiceHost(svcType);
9: aServiceHost.Open();
10: }
11: