WCF入门教程2——创建第一个WCF程序

本文详细介绍了如何通过WCF创建一个简单的服务应用,包括定义服务契约、创建宿主程序和服务客户端,以及通过控制台应用程序进行服务调用的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本节目标

定义服务契约

创建宿主程序

创建客户端程序访问服务

定义服务契约

ServiceContract特性:该特性可被用来作用于子类或者借口之上,并允许重复声明。

OperationContract:只有定义了该特性的方法才会被放入服务之中。

1、新建服务程序

新建项目——类库,这里我们先不直接新建一个WCF服务,而是新建一个类库,命名为HelloService


添加引用

删除Class1.cs,然后新建一个接口IHelloService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel; //添加命名空间,这是WCF的核心库

namespace HelloService

{
   [ServiceContract]
   public interface IHelloService
   {
       [OperationContract]
       string SayHello(string name);
   }
}

添加HelloService类:

public classHelloService:IHelloService
   {
       public string SayHello(string name)
       {
            return "你好,我是:" + name;
       }
   }

ServiceHost类型:当IISWAS作为宿主程序时,IISWAS会自动创建ServiceHost类型。

手动创建的基本语法:public ServiceHost(TypeserviceType,params Uri[] baseAddresses);

2、新建宿主

新建项目——控制台应用程序


然后添加System.ServiceModel引用,和项目引用HelloService,引用之前的类库项目。

HelloServiceHost 项目中Program.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 

using System.ServiceModel;
using System.ServiceModel.Channels; //使用到了绑定 

namespace HelloServiceHost
{
   class Program
   {
       static void Main(string[] args)
       {
            using (MyHelloHost host=newMyHelloHost())
            {
                host.Open();
                Console. Console.ReadLine();
            }
       }
   }

   public class MyHelloHost:IDisposable
   {
       /// <summary>
       /// 定义一个服务对象
       /// </summary>
       private ServiceHost _myHelloHost; 
       public const string BaseAddress = "net.pipe://localhost"; //基地址
       public const string HelloServiceAddress = "Hello"; //可选地址
       public static readonly Type ServiceType =typeof(HelloService.HelloService);  //服务契约实现类型
       public static readonly Type ContractType =typeof(HelloService.IHelloService);  //服务契约接口
       public static readonly Binding HelloBinding = new NetNamedPipeBinding(); //服务定义一个绑定
 
       /// <summary>
       /// 构造方法
       /// </summary>
       public MyHelloHost()
       {
            CreateHelloServiceHost();
       }

       /// <summary>
       /// 构造服务对象
       /// </summary>
       protected void CreateHelloServiceHost()
       {
            _myHelloHost = newServiceHost(ServiceType, new Uri[] { new Uri(BaseAddress) });//创建服务对象
           _myHelloHost.AddServiceEndpoint(ContractType, HelloBinding,HelloServiceAddress); //添加终结点
       }

       /// <summary>
       /// 打开服务方法
       /// </summary>
       public void Open()
       {
            Console.WriteLine("开始启动服务...");
            _myHelloHost.Open();
            Console.WriteLine("服务已启动");
       }

       /// <summary>
       /// 销毁服务宿主对象实例
       /// </summary>
       public void Dispose()
       {
            if (_myHelloHost != null)
                (_myHelloHost asIDisposable).Dispose();
       }
   }

}

3、新建客户端调用程序

新建项目——控制台应用程序

HelloClient项目中Program.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 

using System.ServiceModel;
using System.ServiceModel.Channels;
using HelloService;
 
namespace HelloClient
{
   class Program
   {
       static void Main(string[] args)
       {
          using(HelloProxy proxy=new HelloProxy())
            {
                //利用代理调用方法
               Console.WriteLine(proxy.Say("郑少秋"));
               Console.ReadLine();
            }
       }
   }

   [ServiceContract]
   interface IService
   {
       [OperationContract]
       string Say(string name);
   }

   class HelloProxy:ClientBase<IHelloService>,IService
   {
       public static readonly Binding HelloBinding = newNetNamedPipeBinding();  //硬编码定义绑定
       //硬编码定义地址 注意:这里要和之前服务定义的地址保持一直
       public static readonly EndpointAddress HelloAddress =newEndpointAddress(new Uri("net.pipe://localhost/Hello"));
       public HelloProxy() : base(HelloBinding,HelloAddress) { } //构造方法

       public string Say(string name)
       {
            //使用Channel属性对服务进行调用
            return Channel.SayHello(name);
       }
   }
}

先运行HelloServiceHost

然后运行HelloClient


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值