工作原理:首先由客户端通过数据协定类NewsCL构造自定义类实例数据,并发送给WCF服务器端,服务器端WCF程序反序列化出NewsSV类实例数据,并修改实例数据的Title内容,限定在长度10内,并对实例数据的PublishDate加1天,然后返回给客户端,客户端收到数据后在屏幕上打印输出。
服务器端程序
IService.cs
-
using System;
-
using System.ServiceModel;
-
-
-
namespace ConsoleAppWcfNews
-
{
-
[ServiceContract(Name ="news_sv",Namespace ="")]
-
public interface IService
-
{
-
[OperationContract(Name = "postNews", Action = "post_act", ReplyAction = "post_rpy")]
-
NewsSV postNews(NewsSV obj);
-
-
-
}
-
}
MyService.cs
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.ServiceModel;
-
using System.ServiceModel.Channels;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
namespace ConsoleAppWcfNews
-
{
-
class MyService:IService
-
{
-
public NewsSV postNews(NewsSV obj)
-
{
-
string msg;
-
-
obj.Title = obj.Title.Substring(0, 10);
-
obj.PublishDate=obj.PublishDate.AddDays(1);
-
-
msg = $"新闻标题:{obj.Title}\n新闻发布时间:{obj.PublishDate}\n";
-
-
Console.WriteLine("新闻发布成功,概要如下:\n" + msg);
-
return obj;
-
}
-
}
-
}
NewsSV.cs
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
using System.Runtime.Serialization;
-
namespace ConsoleAppWcfNews
-
{
-
[DataContract(Namespace = "test12")]
-
public class NewsSV:IExtensibleDataObject
-
{
-
[DataMember]
-
public string Title { get; set; }
-
//[DataMember]
-
//public string Content { get; set; }
-
[DataMember]
-
public DateTime PublishDate { get; set; }
-
public ExtensionDataObject ExtensionData { get; set; }
-
}
-
}
Program.cs
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.ServiceModel;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
namespace ConsoleAppWcfNews
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Uri svaddress = new Uri("");
-
using (ServiceHost host = new ServiceHost(typeof(MyService)))
-
{
-
host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), svaddress);
-
host.Open();
-
Console.WriteLine("服务已运行,请等待客户端调用。");
-
Console.Read();
-
}
-
}
-
}
-
}
客户端程序
IService.cs
-
using System;
-
using System.ServiceModel;
-
-
namespace ConsoleAppNewClient
-
{
-
-
[ServiceContract(Name = "news_sv", Namespace = "")]
-
public interface IService
-
{
-
[OperationContract(Name = "postNews", Action = "post_act", ReplyAction = "post_rpy")]
-
NewsCL postNews(NewsCL obj);
-
-
}
-
}
NewsCL.cs
-
using System;
-
using System.Runtime.Serialization;
-
-
namespace ConsoleAppNewClient
-
{
-
[DataContract(Namespace = "test12")]
-
public class NewsCL
-
{
-
[DataMember]
-
public string Title { get; set; }
-
[DataMember]
-
public string Content { get; set; }
-
[DataMember]
-
public DateTime PublishDate { get; set; }
-
-
}
-
}
Program.cs
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.ServiceModel;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
namespace ConsoleAppNewClient
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
//using(ClientReq cr=new ConsoleAppNewClient.ClientReq())
-
//{
-
// News n = new News
-
// {
-
// Title = "标题测试公元2018年",
-
// Content = "WCF编程权威指南内容部分",
-
// PublishDate = new DateTime(2016, 8, 9)
-
// };
-
// cr.PostNews(n);
-
// Console.Read();
-
//}
-
NewsCL obj = new NewsCL
-
{
-
Title = "标题测试公元2018年",
-
Content = "WCF编程权威指南内容部分",
-
PublishDate = new DateTime(2016, 8, 9)
-
};
-
Uri svaddr = new Uri("");
-
IService sv = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(svaddr));
-
-
Console.WriteLine($"上传{obj.Title}--{obj.Content}--{obj.PublishDate}");
-
-
NewsCL retsv = sv.postNews(obj);
-
-
Console.WriteLine($"下载{retsv.Title}--{retsv.Content}--{retsv.PublishDate}");
-
-
((IClientChannel)sv).Close();
-
Console.Read();
-
}
-
}
-
}
阅读(1099) | 评论(0) | 转发(0) |