第一步:创建解决方案WCFServiceDemo
第二步:创建WCF服务库(类库或WCF服务库)WCFService
二-1:添加引用
二-2:创建实体模型Book
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace WCFService.Models { [DataContract] [Serializable] public class Book { [DataMember] public string Name { get; set; } [DataMember] public double Price { get; set; } } }
二-3:创建实现类BookService
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WCFService.Models; namespace WCFService { public class BookService : IBookService { List<Book> list = new List<Book>(); public bool Add(string name, double price) { list.Add(new Book() { Name = name, Price = price }); return true; } public List<Book> GetList() { return list; } } }
二-4:创建接口IBookService(接口必须加上ServiceContract特性,方法必须加上OperationContract特性)
using System; using System.ServiceModel; namespace WCFService { [ServiceContract] public interface IBookService { [OperationContract] bool Add(string name, double price); [OperationContract] System.Collections.Generic.List<WCFService.Models.Book> GetList(); } }
第三步:创建Window服务宿主WindowsServiceHost
三-1:添加引用 System.ServiceModel、WCFService
三-2:编辑WCF配置
原文:http://www.cnblogs.com/wzq806341010/p/3599631.html