A WCF service can already be invoked by. NET Core and ASP.
Environmental requirements:
VS2015 Update 2 +vs2015 tooling +. NET Core SDK
:
Https://www.microsoft.com/net/core
Already installed can be ignored.
Install extensions
Open VS2015 Search for WCF Connected Service in Tools-"Extensions and updates online"
then download the installation. Restart the VS2015 after the installation is complete.
Create a WCF Service
Create a new WCF service that adds some methods.
INETCoreService.cs
[ServiceContract] Public InterfaceInetcoreservice {[OperationContract]stringGetData (intvalue); [OperationContract] Compositetype getdatausingdatacontract (Compositetype composite); [OperationContract] List<string>GetList (); //TODO: Add your service actions here } //use the data contract described in the following example to add a composite type to a service operation. [DataContract] Public classCompositetype {BOOLBoolvalue =true; stringStringValue ="Hello"; [DataMember] Public BOOLBoolvalue {Get{returnBoolvalue;} Set{Boolvalue =value;} } [DataMember] Public stringStringValue {Get{returnStringValue;} Set{stringvalue =value;} } }
View Code
Netcoreservice.svc
Public classNetcoreservice:inetcoreservice { Public stringGetData (intvalue) { return string. Format ("You entered: {0}", value); } Publiccompositetype getdatausingdatacontract (Compositetype composite) {if(Composite = =NULL) { Throw NewArgumentNullException ("Composite"); } if(composite. Boolvalue) {composite. StringValue+="Suffix"; } returnComposite; } Publiclist<string>GetList () {varList =Newlist<string>(); List. ADD ("Cnblogs"); List. ADD ("Linezero"); List. ADD ("Test"); returnlist; } }
View Code
. NET Core Calls WCF
Create a new. NET Core Console program.
Then, in the Netcorewcfclient reference right-click Add Connected service
Select WCF Service, and then configure. Fill in the address of the WCF that is running.
Then next, change the Array to list in the data Type option.
And then you're done.
Here we can invoke the WCF service.
Public Static voidMain (string[] args) {Encoding.registerprovider (codepagesencodingprovider.instance); Netcoreserviceclient Client=Newnetcoreserviceclient (); vart = client. Getdataasync ( -); Console.WriteLine (T.result); varT1 =client. Getlistasync (); foreach(varItemincht1. Result) {Console.WriteLine (item); } Compositetype Composite=NewCompositetype (); Composite. Boolvalue=true; Composite. StringValue="Client Calls"; vart2 =client. Getdatausingdatacontractasync (composite); Console.WriteLine (T2. Result.stringvalue); Console.readkey (); }
F5 Execution Program
Github:https://github.com/linezero/blog/tree/master/netcorewcf
Support for the. NET Core version of WCF is not yet complete and somewhat complex and cannot be referenced successfully.
If you think this article is helpful to you, please click " recommend ", thank you.
. NET Core calls a WCF service