Today I learned a bit about. NET WCF components, the side of the whim, thinking now is not both back and forth, the interface development is not, so hurriedly wrote a simple background data, haha nonsense not much to say, directly on the code;
Note that you need to import the library!
Entity class: Customer
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacedomain{ Public classCustomer { Public stringCustomerId {Set;Get; } Public stringCompanyName {Set;Get; } Public stringContactName {Set;Get; } Public stringAddress {Set;Get; } Public stringtest1 {Set;Get; } };}
View Code
WCF interfaces
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.ServiceModel.Web;usingSystem.Text;namespaceInterface Test Learning 1{//Note: Using the rename command on the Refactor menu, you can change the interface name "IService1" in code and configuration files at the same time. [ServiceContract] Public InterfaceIService1 {[OperationContract]stringGetData (intvalue); [OperationContract] Compositetype getdatausingdatacontract (Compositetype composite); //TODO: Add your service actions here[OperationContract]stringGetdatajson (stringcustomer); } //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
Implementation classes for WCF interfaces
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.ServiceModel.Web;usingSystem.Text;usingSystem.Data.SqlClient;usingdomain;usingNewtonsoft.json;namespaceInterface Test Learning 1{//Note: Using the rename command on the Refactor menu, you can change the class name "Service1" in Code, SVC, and configuration files at the same time. //Note: In order to start the WCF test client to test this service, select Service1.svc or Service1.svc.cs in Solution Explorer and start debugging. Public classService1:iservice1 { 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; } Public stringGetdatajson (stringcustomer) { stringStrcon ="Data source=192.168.99.28;initial catalog=edu; User Id=pos; Password=pos;"; SqlConnection Con=NewSqlConnection (Strcon); List<Customer> list =NewList<customer>(); Try{con. Open (); //The SQL that will be executed//String sql = "SELECT * from Customer"; stringsql =string. Format ("select * FROM {0}", customer); //creates a command object that specifies the SQL statement to execute with the connection object ConnSqlCommand cmd =NewSqlCommand (sql, con); Console.WriteLine ("Open success, status"+con. State); //executing a query returns a result setSqlDataReader SDR =cmd. ExecuteReader (); while(SDR). Read ()) {Customer C=NewCustomer (); C.customerid= sdr["CustomerId"]. ToString (); C.companyname= sdr["CompanyName"]. ToString (); C.contactname= sdr["ContactName"]. ToString (); C.address= sdr["Address"]. ToString (); C.test1= sdr["test1"]. ToString (); List. ADD (c); } } Catch(Exception ex) {Console.WriteLine (ex). Message); } finally { //Close Connectioncon. Close (); //Console.WriteLine ("Total Query" + count + "bar data"); //Console.readkey ();Console.WriteLine (list. capacity); } //then serialize the list collection and go to the JSON stringJSON =jsonconvert.serializeobject (list); Console.WriteLine (JSON); Console.readkey (); returnJSON; } } }
View Code
Convert JSON data format using JsonConvert (object to JSON)