Create a WCF Service using plain text files

Source: Internet
Author: User

This article will teach you how to create a WCF Service using plain text files instead of Visual Studio.

I,Write code:

First, create two TXT files on your drive C. The file name is serverprogram.txt, and the file name is clientprogram.txt.

Enter the following code in serverprogram.txt to implement an additional WCF Service. The code is simple:

 1 using system;
2 using system. servicemodel;
3
4 namespace simplewcfservice
5 {
6 class serverprogram
7 {
8 Static void main (string [] ARGs)
9 {
10
11 basichttpbinding binding = new basichttpbinding ();
12 URI serviceuri = new uri ("http: // localhost: 8001 ");
13 servicehost host = new servicehost (typeof (simplewcfservice), serviceuri );
14 host. addserviceendpoint (typeof (isimplewcfservice), binding, "operationservice ");
15 host. open ();
16 console. writeline ("Service Startup ");
17 console. Readline ();
18 host. Close ();
19
20}
21}
22
23 [servicecontract]
24 public interface isimplewcfservice
25 {
26 [operationcontract]
27 int add (int A, int B );
28}
29
30 public class simplewcfservice: isimplewcfservice
31 {
32 public int add (int A, int B)
33 {
34 return A + B;
35}
36}
37}

Enter the following code in clientprogram.txt to call the above WCF Service:

 1 using system;
2 using system. servicemodel;
3 using system. Windows. forms;
4
5 namespace simplewcfserviceclient
6 {
7 class clientprogram
8 {
9 static void main (string [] ARGs)
10 {
11 string input;
12 int a, B;
13 console. writeline ("enter two integers separated by commas! ");
14 input = console. Readline ();
15 A = int. parse (input. Split (',') [0]);
16 B = int. parse (input. Split (',') [1]);
17 basichttpbinding binding = new basichttpbinding ();
18 channelfactory <isimplewcfservice> factory = new channelfactory <isimplewcfservice> (binding, new endpointaddress ("http: // localhost: 8001/operationservice "));
19 isimplewcfservice proxy = factory. createchannel ();
20 int result = proxy. Add (A, B );
21 console. writeline (string. Format ("after calculation by the WCF Service, {0} plus {1} originally equals {2}.", a, B, result ));
22 console. Readline ();
23
24
25}
26
27 [servicecontract]
28 public interface isimplewcfservice
29 {
30 [operationcontract]
31 int add (int A, int B );
32}
33}
34}

 

Ii. Compile the Code:

Open command prompt. Run the CMD command to go to disk C.
1. Enter the following command to compile the server code:
CSC/R: "C: \ WINDOWS \ Microsoft. NET \ framework \ v4.0.30319 \ System. servicemodel. dll" serverprogram.txt
2. Enter the following command to compile the client code:
CSC/R: "C: \ WINDOWS \ Microsoft. NET \ framework \ v4.0.30319 \ System. servicemodel. dll" clientprogram.txt
3./R: "C: \ WINDOWS \ Microsoft. NET \ framework \ v4.0.30319 \ System. servicemodel. dll" is used to introduce system. servicemodel. dll.

3. Run the program:
1. Server


2. Client:


 

Summary:This document uses a text file to create a WCF Service. This is the method I used to test WCF on the server. Since vs is not installed on the server, only. Net framwork is available. This method is only applicable to learning and research and is not recommended in practice.

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.