时间:2022-11-08 09:50:25 | 栏目:.NET代码 | 点击:次
WCF:Windows Communication Foundation ,Windows通信基础。
SOP:Service Orientation Architechture,面向服务的架构。
WebService是以BasicHttpBing方式运行的WCF。
1、创建解决方案WCFService

依次添加四个项目,如上图,Client和Hosting为控制台应用程序,Service和Service.Interface均为类库。
2、引用关系
一个类库项目,定义服务契约。
服务契约抽象了服务的所有操作,一般契约为接口形式存在。
//服务协定
[ServiceContract(Name = "ICalculator", Namespace = "http://SampleWcfTest")] //webservice描述文件用的portType命名空间
//CallbackContract =typeof(ICallBack),//双工时的返回协定
//ConfigurationName = "Calculator",//配置文件重的服务名
//ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign,//保护等级
//SessionMode = SessionMode.Allowed//设置会话的支持模式
public interface ICalculator
{
//操作协定
[OperationContract]
double Add(double n1, double n2);
}
一个类库项目,提供对契约的实现。
public class Calculator : ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
// Code added to write output to the console window.
Console.WriteLine("Return: {0}", result);
return result;
}
}
VS的“WCF服务库项目”自动生成了svc文件和对应的svc.cs文件以及App.config文件,运行此项目,会自动启动“WCF服务主机”和“WCF测试客户端”窗口
一个控制台项目,通过自我寄宿的方式作为Sevice项目服务的宿主。寄宿进程为ServiceHost1.exe。
服务寄宿的目的是开启一个进程,为WCF服务提供一个运行环境,并为服务添加一个或者多个终结点,然后暴漏给服务消费者。
WCF服务需要一个运行着的宿主进程,服务寄宿就是给服务指定一个宿主的过程。、
WCF采用基于终结点(EndPoint)的通信手段。终结点有地址(Address),绑定(Binding)和契约(Contract)三部分组成,三要素也可以记作:EndPoint=ABC。
一个终结点包含了通信所必须的所有信息,具体如下:
//承载服务的宿主
using (ServiceHost selfHost = new ServiceHost(typeof(Calculator)))
{
try
{ //添加服务终结点
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), new Uri("http://localhost:8000/GettingStarted/"));
//添加服务元数据行为
if (selfHost.Description.Behaviors.Find() == null)
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri("http://localhost:8000/GettingStarted/metadata")
selfHost.Description.Behaviors.Add(smb);
}
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("input to terminate service.");
Console.WriteLine();
while ("exit" == Console.ReadLine())
{
selfHost.Close();
}
}
catch (CommunicationException ex)
{
Console.WriteLine(ex.Message);
selfHost.Abort();
}
}
打开Hosting项目中的app.config,添加以下代码即可。
可直接右键点击config文件选择“编辑WCF配置”菜单,或通过VS的“工具”菜单,选择“WCF Service配置编辑器”菜单编辑配置文件。
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
服务
<service name="Service.CalculatorService" behaviorConfiguration="metadataBehavior">
<endpoint address="http://127.0.0.1:1111/GettingStarted" binding="wsHttpBinding" contract="Service.Interface.ICalculator"></endpoint>
</service>
</services>
行为
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:1111/GettingStarted/metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Hosting代码修改如下:
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Opened += delegate
{
Console.Write("CalculatorService已经启动,按任意键终止服务");
};
host.Open();
Console.Read();
}
一个Web应用程序,通过IIS寄宿的方式将服务寄宿于IIS中,
寄宿进程为w3wp.exe。WAS激活服务:Window Activation Services。
1、创建WCF服务文件:CalculatorService.svc:
<%@ ServiceHost Sevice=”GettingStarted.CalculatorService” %>
2、配置文件
与app.config相比,web.config无EndPointAddress?服务的地址为.svc所在的地址,默认的元数据为…../CalculatorService.svc?ws…
<system.serviceModel>
<services>
<service name="GettingStarted.CalculatorService" behaviorConfiguration="metadataBehavior" >
<endpoint address="CalculatorService" binding="wsHttpBinding" contract="Calculator"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
可以使用 WCF REST 编程模型。
默认WebHttpBinding。
在协定的实现方式上添加 WebGet或者WebInvoke属性
[OperationContract]
[WebInvoke(UriTemplate = "div?x={x}&y={y}")]
long Divide(long x, long y);
[OperationContract]
[WebGet(UriTemplate = "hello?name={name}")]
string SayHello(string name);
举例2:
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Test1", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
string Test1(string userName, string password);
[OperationContract]
[WebGet(UriTemplate = "Test/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string Test(string id);
}
使用WebServiceHost类
Uri baseAddress = new Uri("http://localhost:8000/");
WebServiceHost svcHost = new WebServiceHost(typeof(CalcService), baseAddress);
try
{
svcHost.Open();
Console.WriteLine("Service is running");
Console.WriteLine("Press enter to quit...");
Console.ReadLine();
svcHost.Close();
}
catch (CommunicationException cex)
{
Console.WriteLine("An exception occurred: {0}", cex.Message);
svcHost.Abort();
}
3、调用:
http://…/div?x=1&y=2
CalculatorServiceClient基类是System.ServiceModel.ClientBase,该基类封装了ChannelFactory
using (CalculatorServiceClient proxy = new CalculatorServiceClient())
{
double result = proxy.Add(1, 2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
}
//using (ChannelFactory channelFactory = new ChannelFactory(new WSHttpBinding(), "<a href="http://127.0.0.1:1111/CalculatorService" rel="external nofollow" target="_blank">http://127.0.0.1</a>:1111/CalculatorService"))//构造函数中指定了终结点的ABC三要素,
using (ChannelFactory channelFactory = new ChannelFactory("CalculatorService"))//通过配置文件来进行,对应的为config 文件中终结点的name
{
ICalculator proxy = channelFactory.CreateChannel();
using (proxy as IDisposale)
{
Console.WriteLine("x+y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2));
}
}
配置文件:
<configuration>
<system.serviceModel>
<client>
<endpoint name="CalculatorService" address="http://127.0.0.1:1111/CalculatorService" binding="wsHttpBinding" contract="Service.Interface.ICalculator" />
</client>
</system.serviceModel>
</configuration>
WCF中常用的binding方式:
BasicHttpBinding、WSHttpBinding、WSDualHttpBinding和WSFederationHttpBinding选项适合于通过XML Web服务协议提供契约类型。显然,如果需要使该服务可适用于更多场合(多种操作系统和多种编程语言),这些就是需要关注的绑定,因为所有这些绑定类型都基于XML表示编码数据并且使用HTTP传送数据。
在下面清单中,注意到可以在代码中表示WCF绑定(通过System.ServiceModel名称空间中的类类型),或者作为在*.config文件中定义的XML属性表示WCF绑定。
如果正在构建一个分布式系统,该系统涉及使用.NET 3.0/3.5库配置的一组连网机器(换句话说,所有机器都运行Windows XP、Windows Server 2003或Windows Vista),就可以通过绕开Web服务绑定并选择使用TCP绑定来增强性能,TCP绑定确保以紧凑二进制格式(而不是XML)编码所有数据。同样,在使用下表的的绑定时,客户和主机必须是.NET应用程序。
注:二进制编码格式使用TCP、IPC、MSMQ可以获取最佳性能,但是它是以牺牲互操作性为代价,因为它只支持WCF到WCF的通信。