1.新建一個MVC web項目。
2.點擊項目,【右鍵】→【添加】→【新建項】

3.點擊【Web】→【Web服務】

4.恭喜,Web Service已經新建成功,里面的方法就可以參考着根據自己的需要進行修改了,是不是很簡單。

5.Web Serice建成之后當然是開始調用了。在【引用】上【右鍵】,添加【服務引用】

6.開始引用。

7.恭喜服務已經引用成功。

再看配置文件,會多出一些代碼,這些都是自動生成的,可以看看理解理解。

8.開始在程序中調用方法了

9.到此為止Web Service的建立到調用已經全部完成。
10.新建WCF與Web Service基本上是一樣的,下面直圖解不在介紹。
點擊項目,【右鍵】→【添加】→【新建項】→【Web】→【WCF服務】


11.WCF建成之后當然是開始調用了。在【引用】上【右鍵】,添加【服務引用】,以后過程即使一模一樣的了。

12.WCF和Web Service已經建立成功,那么如何供外部訪問呢,當時是發布了,發布在IIS就可以,和發布網站是一樣的。
WCF控制台應用程序例子

上面這是個簡單的架構圖。Iservice大家都有的功能的接口(契約),IUserService針對不同的類定義不同的接口,
AbstractService實現IService的公共方法,Uservice實現自己的方法,也可以重寫集成的方法。
下面是分層,Web層,大家可以當做不存在,哈哈。

下面是IService層
using System; using System.Collections.Generic; using System.ServiceModel; namespace IServiceClassLibrary { [ServiceContract] public interface IService<TModel> : IDisposable where TModel : new() { [OperationContract] string Add(TModel tModel); [OperationContract] string Delete(int id); [OperationContract] string Edit(TModel tModel); [OperationContract] TModel GetModel(int id); [OperationContract] IEnumerable<TModel> GetAll(); } }
using System; using System.Runtime.Serialization; namespace IServiceClassLibrary { [DataContract] public class UserData { [DataMember] public int UserID { get; set; } [DataMember] public string UserName { get; set; } [DataMember] public string Password { get; set; } [DataMember] public string Discribe { get; set; } [DataMember] public DateTime SubmitTime { get; set; } } }
using System; using System.Collections.Generic; using System.ServiceModel; namespace IServiceClassLibrary { [ServiceContract] public interface IUserService : IService<UserData> { [OperationContract] IDictionary<int, string> GetUserDict(); } }
下面是Service層
using IServiceClassLibrary; using System; using System.Collections.Generic; namespace ServiceClassLibrary { public abstract class AbstractService<TModel> : IService<TModel> where TModel : new() { public virtual string Add(TModel tModel) { //throw new NotImplementedException(); return "Add"; } public virtual string Delete(int id) { //throw new NotImplementedException(); return "Delete"; } public virtual string Edit(TModel tModel) { //throw new NotImplementedException(); return "Edit"; } public virtual TModel GetModel(int id) { TModel tModel = new TModel(); return tModel; } public virtual IEnumerable<TModel> GetAll() { IList<TModel> tModels = new List<TModel>(); return tModels; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool dispose) { if (dispose) { //CleanUp managed objects by calling thier } } } }
using IServiceClassLibrary; using System.Collections.Generic; namespace ServiceClassLibrary { public class UserService : AbstractService<UserData>, IUserService { public IDictionary<int, string> GetUserDict() { IDictionary<int, string> keyValue = new Dictionary<int, string>(); keyValue.Add(1, "test"); return keyValue; } protected override void Dispose(bool dispose) { if (dispose) { //CleanUp managed objects by calling thier } } } }
下面是Server層,這層需要配置了,【新建】→【控制台應用程序】建好之后,【添加】→【新建項】→【WCF 服務】,建號之后,生成的兩個文件可以刪去了,
查看配置信息,對配置進行修改就可以了,懶人模式,高效准確。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="ServiceClassLibrary.UserService"> <endpoint address="" binding="basicHttpBinding" contract="IServiceClassLibrary.IUserService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8733/" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
using IServiceClassLibrary; using ServiceClassLibrary; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace WCFServer { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(UserService))) { host.AddServiceEndpoint(typeof(IUserService), new BasicHttpBinding(), new Uri("http://localhost:8733/ServiceClassLibrary/UserService/")); if (host.State != CommunicationState.Opening) { host.Open(); } Console.WriteLine("服務已經啟動!"); Console.ReadLine(); } } } }
下面是客服端
using IServiceClassLibrary; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace WCFClient { class Program { static void Main(string[] args) { EndpointAddress ea = new EndpointAddress("http://localhost:8733/ServiceClassLibrary/UserService/"); IUserService proxy = ChannelFactory<IUserService>.CreateChannel(new BasicHttpBinding(), ea); Console.WriteLine(proxy.Delete(0)); Console.WriteLine(proxy.GetUserDict().First()); Console.ReadLine(); } } }
OK,WCF程序已經建立完成,先啟動Server,再啟動Client,程序正常運行。
如果遇到下面的問題,請以管理員身份運行就可以了。

其他的WCF文章
1. WCF入門
2. 我的第一個WCF程序
