内容简介:那天有个小孩教我WCF【3/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,跟上脚步,修炼 内功
数据契约
DataContract标记实体类,DataMember标记属性
System.Runtime.Serialization下的 DataContractSerializer 类专为WCF设计,快,效率高
POCO对象,比如string,int等常用对象,都归纳为POCO
由于参数过多,我们可以封装为类,也就是OOP中的封装,但是为了wcf,需要在上面标记DataContract和DataMember
POCO对象如果不需要暴露给客户端,使用IgnoreDataMember标记
新建AYUIModels程序集,这里存放Model
我建立了2个普通类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AYUIModels
{
public class AyRequest
{
public string Ip { get; set; }
public string UserName { get; set; }
public DateTime ReqDateTime { get; set; }
public string Action { get; set; }
public long Timestamp { get; set; }
}
}
一个模拟响应
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AYUIModels
{
public class AyResponse
{
public string ConnectionId { get; set; }
public string ClientIp { get; set; }
public DateTime ServerDate { get; set; }
}
}
我们引用 序列化标记类
using System.Runtime.Serialization;
修饰这个文件
[DataContract]
public class AyResponse
{
[DataMember]
public string ConnectionId { get; set; }
[DataMember]
public string ClientIp { get; set; }
[DataMember]
public DateTime ServerDate { get; set; }
}
DEMO测试,使用这2个约定的类
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
分别在接口和接口实现层,添加model引用
然后接口层,加入新的方法
实现层,实现方法,在对应的ProductService中实现方法
public AyResponse GetServerTime(AyRequest req)
{
AyResponse res = new AyResponse();
res.ServerDate = DateTime.Now.AddMinutes(1);//模拟时间
res.ClientIp = req.Ip;
res.ConnectionId = Guid.NewGuid().ToString();
//记录日志
Console.WriteLine("客户端:"+req.Ip+":请求了操作:"+ req.Action);
return res;
}
编译项目,然后启动host,然后客户端中 右键
更新服务引用
此时wsdl中
客户端生成了几个代理类,这些是 工具 生成的,我们暂时不研究。
由于服务端
增加了Ignore,所以客户端的代理类中也是没有的。
客户端增加测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using AYOrderSystem.OpenProductService;
namespace AYOrderSystem
{
class Program
{
static void Main(string[] args)
{
using (OpenAYUIService.BuyServiceClient client = new OpenAYUIService.BuyServiceClient())
{
Console.WriteLine(client.GetPrice(2));
}
using (AYUIProductServiceClient client = new AYUIProductServiceClient())
{
Console.WriteLine(client.GetAYQQByVersionOverloaded("6.2", DateTime.Now));
AyRequest req = new AyRequest();
req.Action = "我想获取服务器时间";
req.Ip = GetAddressIP();
req.ReqDateTime = DateTime.Now;
req.UserName = "AY";
var _1 = client.GetServerTime(req);
Console.WriteLine("连接ID:"+_1.ConnectionId);
Console.WriteLine("服务端时间为:"+_1.ServerDate);
Console.WriteLine("客户端的IP是:"+_1.ClientIp);
}
Console.ReadLine();
}
/// <summary>
/// 获取本地IP地址信息
/// </summary>
static string GetAddressIP()
{
///获取本地的IP地址
string AddressIP = string.Empty;
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
AddressIP = _IPAddress.ToString();
}
}
return AddressIP;
}
}
}
此时的Host端的控制台,也输出了操作日志,这行代码我在服务端的实现中写的。说明2个控制台不会互相影响的。
当然随着契约实体的复杂,当然还有其他的数据类型,当然支持几个实体类嵌套关系的。感兴趣的自己尝试。
KnownTypes特性,用于 标记元数据返回的子类类型,因为我们代码中写的返回值是父类。 实际返回是子类,需要明确标记类型
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
在AYUIModels程序集中新建个子类 AyResponseSub1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace AYUIModels
{
[DataContract]
public class AyResponseSub1:AyResponse
{
[DataMember]
public string Memo { get; set; }
}
}
AyResponseSub2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace AYUIModels
{
[DataContract]
public class AyResponseSub2:AyResponse
{
[DataMember]
public string Memo2 { get; set; }
}
}
修改实现,这个时候我们还没有标记 KnownTypes 特性
public AyResponse GetServerTime(AyRequest req)
{
//AyResponse res = new AyResponse();
//res.ServerDate = DateTime.Now.AddMinutes(1);//模拟时间
//res.ClientIp = req.Ip;
//res.ConnectionId = Guid.NewGuid().ToString();
////记录日志
//Console.WriteLine("客户端:"+req.Ip+":请求了操作:"+ req.Action);
AyResponseSub1 res = new AyResponseSub1();
res.ServerDate = DateTime.Now.AddHours(1);//模拟时间
res.ClientIp = req.Ip;
res.ConnectionId = Guid.NewGuid().ToString();
res.Memo = "测试来着";
//记录日志
Console.WriteLine("2.0 客户端:" + req.Ip + ":请求了操作:" + req.Action);
return res;
}
更新服务后,运行客户端,报错了
“System.ServiceModel.CommunicationException”类型的未经处理的异常在 mscorlib.dll 中发生
其他信息: 接收对 http://localhost:1111/AYUIProductService 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。有关详细信息,请参见服务器日志。
推荐您阅读更多有关于“”的文章
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 消息队列解耦是骗小孩儿的
- 那天有个小孩教我WCF【2/31】
- 那天有个小孩教我WCF【1/31】
- 那天有个小孩教我WCF【3/31】
- 那天有个小孩教我WCF【2/31】
- 那天有个小孩教我WCF【1/31】
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。