WCF学习--我的第一个WCF例子
Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口。
通信双方的沟通方式,由合约来订定。通信双方所遵循的通信方法,由协议绑定来订定。通信期间的安全性,由双方约定的安全性层次来订定。
契约(Contract)
协议绑定 (Binding)
由于 WCF 支持了HTTP,TCP,Named Pipe,MSMQ,Peer-To-Peer TCP 等协议,而 HTTP 又分为基本 HTTP 支持 (BasicHttpBinding) 以及 WS-HTTP 支持 (WsHttpBinding),而 TCP 亦支持 NetTcpBinding,NetPeerTcpBinding 等通信方式,因此,双方必须要统一通信的协议,并且也要在编码以及格式上要有所一致。
安全性层次

[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
string GetData();
}
通过应用ServiceContractAttribute特性将接口定义成服务契约,要在相应的操作方法上面显式地应用OperationContractAttribute特性。
HelloWorld实现IHelloWorld接口,并具体实现接口定义的方法。
public class HelloWorld :IHelloWorld
{
public string GetData()
{
return "Hello World";
}
}
在Program中实现WCF服务的启动
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloWorld)))
{
host.AddServiceEndpoint(typeof(IHelloWorld), new WSHttpBinding(), "http://127.0.0.1:9999/HelloWord");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/HelloWord/HelloWord");
host.Description.Behaviors.Add(behavior);
}
host.Opened += delegate
{
Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
}; host.Open();
Console.Read();
}
}
}

现在服务已经启动,下面实现客户端的调用。(Client层)
有两种实现方式:
1:客户端层引用服务端DLL(WCF_Console_Service)
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>(new WSHttpBinding(), "http://127.0.0.1:9999/HelloWord"))
{
IHelloWorld proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
string t = proxy.GetData();
}
}
}
}

2:添加服务引用

利用svcUtil.exe生成代码,输入http://127.0.0.1:9999/HelloWord/HelloWord参数(添加svcutil.exe见http://www.cnblogs.com/scottckt/archive/2012/05/20/2510716.html)

//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:2.0.50727.5472
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------ [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IHelloWorld")]
public interface IHelloWorld
{ [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IHelloWorld/GetData", ReplyAction="http://tempuri.org/IHelloWorld/GetDataResponse")]
string GetData();
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface IHelloWorldChannel : IHelloWorld, System.ServiceModel.IClientChannel
{
} [System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class HelloWorldClient : System.ServiceModel.ClientBase<IHelloWorld>, IHelloWorld
{ public HelloWorldClient()
{
} public HelloWorldClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
} public HelloWorldClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
} public HelloWorldClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
} public HelloWorldClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
} public string GetData()
{
return base.Channel.GetData();
}
}
生成的config为
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IHelloWorld" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://127.0.0.1:9999/HelloWord" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IHelloWorld" contract="IHelloWorld"
name="WSHttpBinding_IHelloWorld">
<identity>
<userPrincipalName value="zb-PC\Administrator" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
Program代码为
class Program
{
static void Main(string[] args)
{
HelloWorldClient client = new HelloWorldClient();
string s = client.GetData();
Console.WriteLine(s);
}
}
运行结果

以上为新手学习,高手大神就绕过啦,和大家一起学习
WCF学习--我的第一个WCF例子的更多相关文章
- WCF学习之旅——第一个WCF示例(一)
最近需要用到WCF,所以对WCF进行了解.在实践中学习新知识是最快的,接下来先做了一个简单的WCF服用应用示例. 本文的WCF服务应用功能很简单,却涵盖了一个完整WCF应用的基本结构.希望本文能对那些 ...
- WCF学习之旅——第一个WCF示例(三)
第五步:创建客户端 WCF应用服务被成功寄宿后,WCF服务应用便开始了服务调用请求的监听工作.此外,服务寄宿将服务描述通过元数据的形式发布出来,相应的客户端就可以获取这些元数据.接下来我们来创建客户端 ...
- WCF学习之旅——第一个WCF示例(二)
第四步:通过自我寄宿的方式寄宿服务 WCF服务需要依存一个运行着的进程(宿主),服务寄宿就是为服务指定一个宿主的过程.WCF是一个基于消息的通信框架,采用基于终结点(Endpoint)的通信手段. 终 ...
- WCF学习笔记——Day1:一个WCF demo
Visual Studio2017,使用IIS托管.文中涉及一些WCF的基本概念,e.g.服务契约.托管等.可以先阅读<WCF服务编程>第一章. 1.新建一个WCF服务库(WCF Serv ...
- WCF学习之旅—第三个示例之五(三十一)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) WCF学习 ...
- WCF学习之旅—TcpTrace工具(二十五)
前面的几篇文章,我们学习了怎么开发WCF应用程序与服务,也学习了如何进行WCF的配置.对于Web Service与WCF服务应用,服务端与客户端的通信是通过收发SOAP Message进行,我们如何有 ...
- WCF学习之旅—WCF第二个示例(五)
二.WCF服务端应用程序 第一步,创建WCF服务应用程序项目 打开Visual Studio 2015,在菜单上点击文件—>新建—>项目—>WCF服务应用程序.在弹出界面的“名称”对 ...
- WCF学习之旅—WCF第二个示例(七)
三.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据. 在第一个步骤中,你将 Windows 窗体项目添加到解 ...
- WCF学习之旅—WCF第二个示例(六)
第五步,创建数据服务 在“解决方案资源管理器”中,使用鼠标左键选中“SCF.WcfService”项目,然后在菜单栏上,依次选择“项目”.“添加新项”. 在“添加新项”对话框中,选择“Web”节点,然 ...
随机推荐
- TCP可靠的传输机制
TCP提供一种面向连接的.可靠的字节流服务.面向连接意味着两个使用TCP的应用(一般是一个客户和一个server)在彼此交换数据包之前必须先建立一个TCP连接.这一过程与打电话非常相似.先拨号振铃,等 ...
- Ruby开发环境
Windows上Ruby开发环境的配置 最近公司项目上有需要,需要开发一个puppet的自动化工具,这个工具需要操作存储设备上的各种资源,而鉴于puppet不是善于完成这个任务的首选语言,于是我们 ...
- 经典卷积神经网络的学习(三)—— Inception Net
Google Inception Net 首次出现在 ILSVRC 2014 的比赛中(和 VGGNet 同年),就以较大优势拔得头筹.那届比赛中的 Inception Net 一般被称为 Incep ...
- Android 对.properties文件的读取
/** * * @param filepath .properties文件的位置 */ public void checkFileExists(String filepath){ File file ...
- React Native中的DeviceEventEmitter.addListener与DeviceEventEmitter.emit
官方文档没有对这两个方法做很好的解释,需要自己找资料研究.看了几篇文章,总结是和订阅发布模式差不多,用来事件监听发送的. React Native学习之DeviceEventEmitter传值 R ...
- dotnet core 使用 sqlite 部署到 Centos 服务器
原文:dotnet core 使用 sqlite 部署到 Centos 服务器 本文告诉大家如何创建一个 asp dotnet core 程序,这个程序使用 sqlite 保存,部署程序到 Cento ...
- 树莓派的rc.local档(设置开机)
为了树莓派执行命令或程序时启动.需要被添加到顺序rc.local档.这是为那些谁执行后,直接要权力树莓派没有配置.或者不希望每次都手动启动该程序很实用. 的方法是使用cron和crontab. EDI ...
- .NET开发人员的瓶颈和职业发展
现在社会比前几年浮躁了,越来越多的人抱怨薪水低,高薪工作不好找; 诚然这有CPI的压力,可是也有很多人没有认清自己的职业发展. 很多.net程序员个各种纠结,想拿高薪又拿不到,想提高又不知道怎么能提高 ...
- 简单IO,将一段字符串存入一个记事本
using System; using System.IO; using System.Text; namespace 字符串存入记事本 { class Program { static void M ...
- 一些自成系统、完备的教程(链接、博客、github等)
0. Linus shell Advanced Bash-Scripting Guide 1. latex Some applicable LATEX's info 14 课的个人 CV 制作: 15 ...