How to get started with WCF helloworld

Source: Internet
Author: User

1. Open vs, file-> New-> Project-> WCF Service Application

2. After creating a WCF, add the code in iservice1.svc as follows: For details, see comments.

  

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. runtime. serialization;
Using system. servicemodel;
Using system. servicemodel. Web;
Using system. text;

Namespace myserver
{
// Note: You can use the "RENAME" command on the "refactor" menu to change the Interface Name "iservice1" in both code and config file together.
[Servicecontract]
Public interface iservice1
{

[Operationcontract]
String getdata (INT value );

[Operationcontract]
Compositetype getdatausingdatacontract (compositetype composite );

// Custom Method
[Operationcontract]
String getname (string name );

// Todo: add your service operations here
}

// Use a data contract as your strated in the sample below to add composite types to service operations.
[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 ;}
}
}

}

3. Add the following code to service1.svc-> service1.svc. CS: For details, see annotations.

  

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. runtime. serialization;
Using system. servicemodel;
Using system. servicemodel. Web;
Using system. text;

Namespace myserver
{
// Note: You can use the "RENAME" command on the "refactor" menu to change the class name "service1" in code, SVC and config file together.
Public class service1: iservice1
{
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;
}

// Custom Method
Public String getname (string name)
{
Return name;
}

}
}

4. Well, you have successfully created a WCF. Run the command and copy the URL. For example, my website is http: // localhost: 3285/

5. In the same solution, right-click Add-> Project-> Visual C #-> Windows-> console application

6. Right-click the references in the project you added, right-click Add service reference, copy your url to address, click Go, change your namespace to myserver, and click OK.

7. If everything goes well, then program. CS is as follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
// Note the introduction of namespace (myserver is the server name I just set)
Using leleapplication1.myserver;

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Service1client SC = new service1client (); // new object
Console. writeline (SC. getname ("zhangyongbin"); // call the server Method
Console. readkey (); // to pause the output form
}
}
}

Then run the program.

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.