Chinaunix首页 | 论坛 | 博客
  • 博客访问: 265033
  • 博文数量: 54
  • 博客积分: 1425
  • 博客等级: 上尉
  • 技术积分: 541
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-27 23:26
文章分类

全部博文(54)

文章存档

2018年(2)

2015年(3)

2014年(6)

2013年(5)

2012年(5)

2011年(7)

2010年(14)

2009年(1)

2008年(3)

2007年(6)

2006年(1)

2005年(1)

我的朋友

分类: C#/.net

2018-03-25 09:42:17

工作原理:首先由客户端通过数据协定类NewsCL构造自定义类实例数据,并发送给WCF服务器端,服务器端WCF程序反序列化出NewsSV类实例数据,并修改实例数据的Title内容,限定在长度10内,并对实例数据的PublishDate加1天,然后返回给客户端,客户端收到数据后在屏幕上打印输出。
服务器端程序
IService.cs

点击(此处)折叠或打开

  1. using System;
  2. using System.ServiceModel;


  3. namespace ConsoleAppWcfNews
  4. {
  5.     [ServiceContract(Name ="news_sv",Namespace ="")]
  6.     public interface IService
  7.     {
  8.         [OperationContract(Name = "postNews", Action = "post_act", ReplyAction = "post_rpy")]
  9.          NewsSV postNews(NewsSV obj);


  10.     }
  11. }
MyService.cs

点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceModel;
  5. using System.ServiceModel.Channels;
  6. using System.Text;
  7. using System.Threading.Tasks;

  8. namespace ConsoleAppWcfNews
  9. {
  10.     class MyService:IService
  11.     {
  12.         public NewsSV postNews(NewsSV obj)
  13.         {
  14.             string msg;
  15.             
  16.             obj.Title = obj.Title.Substring(0, 10);
  17.             obj.PublishDate=obj.PublishDate.AddDays(1);
  18.             
  19.             msg = $"新闻标题:{obj.Title}\n新闻发布时间:{obj.PublishDate}\n";
  20.             
  21.             Console.WriteLine("新闻发布成功,概要如下:\n" + msg);
  22.             return obj;
  23.         }
  24.     }
  25. }
NewsSV.cs

点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.Serialization;
  7. namespace ConsoleAppWcfNews
  8. {
  9.     [DataContract(Namespace = "test12")]
  10.     public class NewsSV:IExtensibleDataObject
  11.     {
  12.         [DataMember]
  13.         public string Title { get; set; }
  14.         //[DataMember]
  15.         //public string Content { get; set; }
  16.         [DataMember]
  17.         public DateTime PublishDate { get; set; }
  18.         public ExtensionDataObject ExtensionData { get; set; }
  19.     }
  20. }
Program.cs

点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceModel;
  5. using System.Text;
  6. using System.Threading.Tasks;

  7. namespace ConsoleAppWcfNews
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Uri svaddress = new Uri("");
  14.             using (ServiceHost host = new ServiceHost(typeof(MyService)))
  15.             {
  16.                 host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), svaddress);
  17.                 host.Open();
  18.                 Console.WriteLine("服务已运行,请等待客户端调用。");
  19.                 Console.Read();
  20.             }
  21.         }
  22.     }
  23. }

客户端程序
IService.cs

点击(此处)折叠或打开

  1. using System;
  2. using System.ServiceModel;

  3. namespace ConsoleAppNewClient
  4. {

  5.     [ServiceContract(Name = "news_sv", Namespace = "")]
  6.     public interface IService
  7.     {
  8.         [OperationContract(Name = "postNews", Action = "post_act", ReplyAction = "post_rpy")]
  9.         NewsCL postNews(NewsCL obj);

  10.     }
  11. }
NewsCL.cs

点击(此处)折叠或打开

  1. using System;
  2. using System.Runtime.Serialization;

  3. namespace ConsoleAppNewClient
  4. {
  5.     [DataContract(Namespace = "test12")]
  6.     public class NewsCL
  7.     {
  8.         [DataMember]
  9.         public string Title { get; set; }
  10.         [DataMember]
  11.         public string Content { get; set; }
  12.         [DataMember]
  13.         public DateTime PublishDate { get; set; }
  14.         
  15.     }
  16. }
Program.cs

点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceModel;
  5. using System.Text;
  6. using System.Threading.Tasks;

  7. namespace ConsoleAppNewClient
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //using(ClientReq cr=new ConsoleAppNewClient.ClientReq())
  14.             //{
  15.             // News n = new News
  16.             // {
  17.             // Title = "标题测试公元2018年",
  18.             // Content = "WCF编程权威指南内容部分",
  19.             // PublishDate = new DateTime(2016, 8, 9)
  20.             // };
  21.             // cr.PostNews(n);
  22.             // Console.Read();
  23.             //}
  24.             NewsCL obj = new NewsCL
  25.             {
  26.                 Title = "标题测试公元2018年",
  27.                 Content = "WCF编程权威指南内容部分",
  28.                 PublishDate = new DateTime(2016, 8, 9)
  29.             };
  30.             Uri svaddr = new Uri("");
  31.             IService sv = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(svaddr));

  32.             Console.WriteLine($"上传{obj.Title}--{obj.Content}--{obj.PublishDate}");

  33.             NewsCL retsv = sv.postNews(obj);

  34.             Console.WriteLine($"下载{retsv.Title}--{retsv.Content}--{retsv.PublishDate}");

  35.             ((IClientChannel)sv).Close();
  36.             Console.Read();
  37.         }
  38.     }
  39. }

阅读(1033) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~