将服务寄宿与控制台:
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; using MyWcfService; namespace Host { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(Service1))) { host.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "http://127.0.0.1:8733/Service1"); if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) { ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; behavior.HttpGetUrl = new Uri("http://127.0.0.1:8733/Service1/metadata"); host.Description.Behaviors.Add(behavior); } host.Opened += delegate { Console.WriteLine("BookService,按任意键终止服务!"); }; host.Open(); Console.Read(); } } } }
客户端
1.添加服务引用
2.
private void button1_Click(object sender, EventArgs e) { Service1Client service1Client = new Service1Client(); MessageBox.Show(service1Client.GetData(2)); }