内容简介:那天有个小孩教我WCF【2/31】
WCF整体系统学习,2016-11-11 -2016-11-27 开发环境vs2015 .net framework 4.0 触屏WPF教程暂时搁置
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
啥契约,来说都是一个接口,在C#中,接口拓展一个类的行为,添加一个统一风格,到处收儿子。
服务,数据,消息
我想花费31天,系统学习WCF,跟上脚步,修炼 内功
控制WSDL文档的生成
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
作为 ServiceContract 特性,有很多成员
SC成员
CallbackContract 当契约是双工时候,读取或设置回调契约的类型
ConfigurationName 获取或设置服务在应用程序配置文件中的名称
HasProtectionLevel 读取一个值,表示此成员是否有一个保护级别
Name 获取或设置WSDL 文档中<portType>节点的名称
Namespace 获取或设置WSDL 文档中<portType>节点的命名空间
ProtectionLevel 设置对契约的绑定是否支持ProtectionLevel属性的值
SessionMode 获取或设置会话是否允许,以及是否得到了请求
以上同样适用 OperationContract 特性
其他OC成员
Action 定义能够唯一标识此操作的动作,WCF根据此动作把请求消息发送给响应的操作
AsyncPattern 标识操作已实现,也可以用Begin/End方法进行异步调用
HasProtectionLevel 标识ProtectionLevel属性是否已 使显式地进行设置
IsOneWay 表示操作只有单个输入消息,没有响应的输出消息
IsInitiating 定义操作是否是会话过程的一个初始操作
IsTerminating 定义在操作结束后WCF是否终止当前会话
ProtectionLevel 定义操作在运行时需要的消息级安全保护
DEMO演示 解决方法重载 对外暴露
新建IProductService 接口
下面有段代码,方法重载了,我们可以使用Name对外改 名字
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IAYUIBuyService
{
public interface IProductService
{
string AYQQ(string version);
string AYQQ(string ayuiversion,DateTime publishDate);
void Download(string id);
}
}
修改代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
namespace IAYUIBuyService
{
[ServiceContract(Namespace="http://www.ayjs.net/wcf/2016/11/14",Name ="AYUIProductService")]
public interface IProductService
{
[OperationContract(Name ="GetAYQQByVersion")]
string AYQQ(string version);
[OperationContract(Name = "GetAYQQByVersionOverloaded")]
string AYQQ(string ayuiversion,DateTime publishDate);
[OperationContract(IsOneWay =true,ProtectionLevel =System.Net.Security.ProtectionLevel.None)]
void Download(string id);
}
}
在AYUIBuyService程序集里面,添加类,然后实现该接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IAYUIBuyService;
namespace AYUIBuyService
{
/// <summary>
/// AYUI产品购买服务
/// 2016-11-13 23:08:32
/// </summary>
public class ProductService : IProductService
{
public string AYQQ(string version)
{
return "AYQQ版本为"+version;
}
public string AYQQ(string ayuiversion, DateTime publishDate)
{
return "使用版本AYUI" + ayuiversion+"的AYQQ已经发现,发布时间:"+publishDate.ToShortDateString();
}
public void Download(string id)
{
Console.WriteLine("下载成功:("+id+")");
}
}
}
来吧,Host配置下,这时候,我们可以不用那个wcf配置 工具 了,自己手动粘贴,改改就行了。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="OpenMetaDataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:1111/AYUIService/MEX" />
</behavior>
<behavior name="OpenProductMetaDataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:1111/ProductService/MEX" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="OpenMetaDataBehavior" name="AYUIBuyService.BuyService">
<endpoint address="http://localhost:1111/AYUIBuyService" binding="wsHttpBinding"
bindingConfiguration="" contract="IAYUIBuyService.IBuyService" />
</service>
<service behaviorConfiguration="OpenProductMetaDataBehavior" name="AYUIBuyService.ProductService">
<endpoint address="http://localhost:1111/AYUIProductService"
binding="basicHttpBinding" bindingConfiguration="" contract="IAYUIBuyService.IProductService" />
</service>
</services>
</system.serviceModel>
</configuration>
修改Program.cs
class Program
{
static ServiceHost AYUIBUYSERVICEHOST;
static ServiceHost AYUIPRODUCTSERVICEHOST;
static void Main(string[] args)
{
Console.WriteLine("AYUI服务启动中...");
try
{
AYUIBUYSERVICEHOST = new ServiceHost(typeof(BuyService));
AYUIBUYSERVICEHOST.Open();
AYUIPRODUCTSERVICEHOST = new ServiceHost(typeof(ProductService));
AYUIPRODUCTSERVICEHOST.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("服务启动成功");
Console.ReadLine();
}
}
编译项目,管理员方式运行Host
然后
客户端,右键Service References文件夹,添加服务引用,一般来说,1个接口层,1个程序集的,这里我为了写demo,快一点,2个服务和实现都放到一起了。
同样高级里面,我们修改array为linkedlist
点击确定。
然后客户端使用,这里客户端无需知道服务端方法名字的,所以 客户端 只是根据元数据生成 代理的,我们从wsdl文件可以看出
客户端的服务相关配置我们也不需要配置的,如果你懂,你也不需要微软提供的,wcf配置工具的。
下节课,我们讲数据契约相关,还有 派生类的 元数据解决
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
推荐您阅读更多有关于“WCF,”的文章
以上所述就是小编给大家介绍的《那天有个小孩教我WCF【2/31】》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 消息队列解耦是骗小孩儿的
- 那天有个小孩教我WCF【3/31】
- 那天有个小孩教我WCF【2/31】
- 那天有个小孩教我WCF【1/31】
- 那天有个小孩教我WCF【3/31】
- 那天有个小孩教我WCF【1/31】
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Ruby on Rails 3 Tutorial
Michael Hartl / Addison-Wesley Professional / 2010-12-16 / USD 39.99
“Ruby on Rails™ 3 Tutorial: Learn Rails by Example by Michael Hartl has become a must read for developers learning how to build Rails apps.” —Peter Cooper, Editor of Ruby Inside Using Rails ......一起来看看 《Ruby on Rails 3 Tutorial》 这本书的介绍吧!