.NET Core 和ASP.NET Core 已經可以調用WCF 服務。
環境要求:
VS2015 Update 2 +VS2015 Tooling + .NET Core SDK
下載地址:
https://www.microsoft.com/net/core
已經安裝可以忽略。
安裝擴展
打開VS2015 在 工具-》擴展和更新 聯機中搜索 WCF Connected Service
然后下載安裝。安裝完成后重啟VS2015.
創建WCF服務
新建一個wcf 服務,里面添加一些方法。
INETCoreService.cs

[ServiceContract] public interface INETCoreService { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); [OperationContract] List<string> GetList(); // TODO: 在此添加您的服務操作 } // 使用下面示例中說明的數據約定將復合類型添加到服務操作。 [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } }
NETCoreService.svc

public class NETCoreService : INETCoreService { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } public List<string> GetList() { var list = new List<string>(); list.Add("cnblogs"); list.Add("linezero"); list.Add("測試"); return list; } }
.NET Core調用WCF
新建一個 .NET Core 控制台程序。
然后在NETCoreWCFClient 引用右鍵-》添加連接的服務
選擇WCF Service ,然后配置。將運行起來的WCF的地址填入。
然后下一步,這里在Data Type 選項里將 Array 改成List 。
然后完成即可。
這里我們就可以調用WCF 服務了。
public static void Main(string[] args) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); NETCoreServiceClient client = new NETCoreServiceClient(); var t = client.GetDataAsync(100); Console.WriteLine(t.Result); var t1 = client.GetListAsync(); foreach (var item in t1.Result) { Console.WriteLine(item); } CompositeType composite = new CompositeType(); composite.BoolValue = true; composite.StringValue = "客戶端調用"; var t2 = client.GetDataUsingDataContractAsync(composite); Console.WriteLine(t2.Result.StringValue); Console.ReadKey(); }
F5執行程序
GitHub :https://github.com/linezero/Blog/tree/master/NETCoreWCF
目前.NET Core 版 WCF 的支持還不算很完整,有些復雜並不能成功引用。
如果你覺得本文對你有幫助,請點擊“推薦”,謝謝。