. NET Core calls a WCF service

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.