WCF:初识
分类:
IT文章
•
2024-02-05 08:01:24

结构:
1 using System.ServiceModel;
2 namespace MyServices
3 {
4 [ServiceContract]
5 public interface IHomeService
6 {
7 [OperationContract]
8 int GetLength(string name);
9 }
10 }
契约
1 namespace MyServices
2 {
3 public class HomeService:IHomeService
4 {
5 public int GetLength(string name)
6 {
7 return name.Length;
8 }
9 }
10 }
实现类
1 using System;
2 using System.ServiceModel;
3
4 namespace MyServices
5 {
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 using (ServiceHost host = new ServiceHost(typeof(HomeService)))
11 {
12 try
13 {
14 host.Open();
15 Console.WriteLine("服务开启!");
16 Console.Read();
17 }
18 catch (Exception e)
19 {
20 Console.WriteLine(e.Message);
21 }
22 }
23 }
24 }
25 }