Quickly build a Demo using WCF and build a Demo using WCF

Source: Internet
Author: User

Quickly build a Demo using WCF and build a Demo using WCF
Quickly build a Demo using WCF

Ps: This Demo only demonstrates how to quickly create a WCF

1. First, build the IBLL, BLL, and Model layers. Since the data access layer is not the focus, the establishment of WCF is the main content, so this Demo omitted the data access layer.

Create a BLL class library project and add the UserInfo class as follows:

 

 1 namespace Model 2 { 3     [DataContract] 4     public class UserInfo 5     { 6         [DataMember] 7         public int Id { get; set; } 8         [DataMember] 9         public string Name { get; set; }10     }11 }

 

Object must be serialized when being transmitted over the network. Pay attention to the following points:

1. XML ing will be performed without marking, and all public attributes/fields will be serialized.

2. The [Serializable] flag serializes all readable and writable fields.

3. [DataContract] and [DataMember] are used together to mark serialized fields.

Next, create an IBLL class library project and add the IUserInfoService class as the interface contract. The Code is as follows:

1 namespace IBLL2 {3     [ServiceContract]4     public interface IUserInfoService5     {6         [OperationContract]7         UserInfo GetUser(int id);8     }9 }

Ps: Remember to tag the methods to be operated!

Similarly, create a BLL class library project and add the UserInfoService class. The Code is as follows (only set up for the test WCF ):

 1 namespace BLL 2 { 3     public class UserInfoService:IUserInfoService 4     { 5         public UserInfo GetUser(int id) 6         { 7             return new UserInfo() { Id = id, Name = "test" }; 8         } 9     }10 }

 

2. Build a WCFHost)

Create a console project and add it to the configuration node in the configuration file app. config:

<system.serviceModel>    <services>      <service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration">        

Note the following:

1. Because it is a local test, baseAddress is filled with the local address, which can be modified according to the actual situation.

2. Change the name attribute of the service node to "namespace + class name" of the specific service class at The BLL layer ".

3. Change the contract attribute of the endpoint node to the "namespace + Interface Name" of the specific service interface in the IBLL layer ".

Add the following to the main program code:

1 using (ServiceHost host = new ServiceHost (typeof (UserInfoService) 2 {3 host. open (); 4 Console. writeLine ("the service has been started. Press any key to stop... "); 5 Console. readKey (); 6 host. close (); 7}

Ps: replace typeof () with the service class to be called.

On the project page, right-click the project and choose export from the shortcut menu. Open the folder in the file resource manager, find the executable program wcfservice.exe, right-click and choose "Run as administrator ".

 

3. the last step is to test whether the WCF Service is successfully built. Create a winform project WCFClient, right-click the project, add service reference, and enter http: // localhost: 8000/(baseAddress entered in the configuration file ). Call the constructed WCF Service in this program:
1 UserInfoServiceClient client = new UserInfoServiceClient();2 UserInfo u =  client.GetUser(5);3 MessageBox.Show(u.Id + "-" + u.Name);

After running, a message is displayed, indicating that the WCF Service is successfully set up!

 

Related Article

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.