WCF编程学习:https://www.cnblogs.com/xfrog/archive/2010/05/12/1733713.html
WCF(Windows Communication Foundation),和WebService功能一样,用于跨平台跨应用程序通信。WCF是面向接口(契约)的。
WebService服务是部署在Web应用程序上的,IIS服务启动后,WebService就启动了。WCF可以部署在Web应用程序上,也可以部署在控制台应用程序上,也可以部署在WinForm应用程序上。当所在的应用程序启动后,WCF服务也就启动了。
添加引用:System.ServiceModel(客户端和服务端都要添加该引用)
WCF服务端:
IUserInfoService.cs(契约接口):
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel; //添加该引用
using System.Text;
using System.Threading.Tasks;
namespace IBLL
{
[ServiceContract] //服务契约(接口)
public interface IUserInfoService
{
[OperationContract] //契约(接口)
int Add(int a,int b);
}
}
UserInfoService.cs(实现契约接口的类):
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class UserInfoService : IUserInfoService //实现接口(满足契约)
{
public int Add(int a, int b)
{
return a+b;
}
}
}
App.config(服务端的配置):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration"> <!--UserInfoService是实现契约接口的类-->
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/> <!--WCF服务对外公布的地址-->
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint> <!--契约(接口)-->
</service> <!--service可以有多个service服务-->
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfiguration">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Program.cs(控制台应用程序,bin/debug/Host.exe以管理员身份运行,运行之后WCF服务就会同时启动):
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel; //添加该引用
using System.Text;
using System.Threading.Tasks;
namespace Host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(BLL.UserInfoService)))
{
host.Open(); //启动WCF服务
Console.ReadKey();
}
}
}
}
客户端引用远程WCF服务第一种方式:
VS开发人员命令提示(以管理员身份运行)--D:--D:\Client(进入客户端应用程序的目录)--输入命令:svcutil http://127.0.0.1:8000/?wsdl /o:UserInfoServiceClient.cs (下载服务文件,并生成客户端类。127.0.0.1是WCF服务的地址,UserInfoServiceClient是自定义的类名)
选择客户端项目--显示所有文件--UserInfoServiceClient--右击--包括在项目中--output.config--右击--包括在项目中;删除App.config,将output.config重命名成App.config。UserInfoServiceClient.cs类中也要添加System.ServiceModel引用,该类中也定义了一个契约(接口),可以注释掉,使用自己定义的契约(接口)。
App.config(客户端的配置):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IUserInfoService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IUserInfoService" contract="IBLL.IUserInfoService"
name="BasicHttpBinding_IUserInfoService" /> <!--也需要引用契约(接口)。客户端和服务端都需要契约(接口)(IBLL.IUserInfoService)-->
</client>
</system.serviceModel>
</configuration>
Program.cs(客户端控制台应用程序):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Client
{
class Program
{
static void Main(string[] args)
{
UserInfoServiceClient client = new UserInfoServiceClient(); //这是从远程服务端下载生成的类。
int sum = client.Add(3,6); //调用远程WCF服务的Add函数。契约接口中定义的函数。
Console.WriteLine(sum.ToString()); //要保证远程的WCF服务已开启。
Console.ReadKey();
}
}
}
客户端引用远程WCF服务第二种方式(和WebService的客户端的方式相同):
Program.cs(客户端应用程序调用远程WCF服务的方式):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Client2
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.UserInfoServiceClient client = new ServiceReference1.UserInfoServiceClient(); //ServiceReference1是添加服务引用时自定义的命名空间。
int sum = client.Add(3,6); //调用远程WCF服务的Add函数。契约接口中定义的函数。
Console.WriteLine(sum.ToString()); //要保证远程的WCF服务已开启。
Console.ReadKey();
}
}
}