对于WCF服务启动的方法,我们可以通过多种方式来实现。在这里我们将会通过一种特殊的方法来实现WCF服务启动。大家首先可以通过一段代码示例来详细分析一下这一服务的启动方法,以此加深这方面的印象。
WCF服务启动代码示例:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using System.ServiceModel.Description;
- namespace WCF1
- {
- [ServiceContract(Name = "MyService", Namespace =
"http://www.huisoftware.com")]- public class MyService
- {
- [OperationContract]
- public string MyMethod(string str)
- {
- return str + "Server Hello World";
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- using (ServiceHost host = new ServiceHost(typeof(MyService)))
- {
- host.AddServiceEndpoint(typeof(MyService), new WSHttpBinding(),
"http://127.0.0.1:8080/MyService");- if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
- {
- ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
- behavior.HttpGetEnabled = true;
- behavior.HttpGetUrl = new Uri("http://127.0.0.1:8080
/MyService/metadata");- host.Description.Behaviors.Add(behavior);
- }
- host.Opened += delegate
- {
- Console.WriteLine("WCF服务已经启动");
- };
- host.Open();
- Console.Read();
- }
- }
- }
- }
WCF服务启动的相关代码就为大家介绍到这里。
【编辑推荐】