WCF(Windows Communication Foundation)是一系列支持数据通信的应用程序框架。
定义服务契约步骤:
1、新建类库,并引入程序集的System.ServiceModel


2、创建接口,定义服务契约,操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;//添加
namespace WCFHelloService
{
[ServiceContract]//用于类或结构上,用于指示WCF此类或结构能够被远程调用
public interface IHelloService
{
[OperationContract]//用于类中的方法(Method)上,用于指示WCF该方法可被远程调用
string SayHello(string name);
}
}
3、创建类,实现上面接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace WCFHelloService
{
class HelloService:IHelloService
{
public string SayHello(string name)
{
return name + " say Hello!";
}
}
}
本文介绍了如何使用Windows Communication Foundation (WCF) 创建服务契约,包括定义接口、操作并实现服务。步骤涉及新建类库、添加`System.ServiceModel`引用,创建`IHelloService`接口和其实现类`HelloService`。适合初学者理解WCF远程调用原理。
710

被折叠的 条评论
为什么被折叠?



