服务端是CS程序,客户端(调用者)是BS程序

一、代码结构:

二、服务接口Contract和实体类Domain

INoticeService:

using Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace Contract
{
/// <summary>
/// 通知公告
/// </summary>
[ServiceContract]
public interface INoticeService
{
/// <summary>
/// 获取一条公告信息
/// </summary>
[OperationContract]
Notice GetNotice();
}
}

Notice:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Domain
{
/// <summary>
/// 通知公告
/// </summary>
public class Notice
{
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 数据
/// </summary>
public byte[] Data { get; set; }
}
}

三、服务WcfService和宿主WcfHost

NoticeService:

using Contract;
using Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WcfService
{
/// <summary>
/// 通知公告服务
/// </summary>
public class NoticeService : INoticeService
{
/// <summary>
/// 获取一条公告信息
/// </summary>
public Notice GetNotice()
{
Notice notice = new Notice();
notice.Title = "测试标题";
notice.Content = "测试内容";
notice.Data = new byte[];
for (int i = ; i < ; i++)
{
notice.Data[i] = (byte);
}
return notice;
}
}
}

Program:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WcfService; namespace WcfServer
{
class Program
{
static void Main(string[] args)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Service";
string[] fileArr = Directory.GetFiles(path);
int port = int.Parse(ConfigurationManager.AppSettings["ServerPort"]); foreach (string fileName in fileArr)
{
string url = string.Format("http://localhost:{0}/Service/{1}", port, Path.GetFileName(fileName));
Uri[] uri = new Uri[] { new Uri(url) }; ServiceHost host = new ServiceHost(typeof(NoticeService), uri);
host.Open();
} Console.WriteLine("服务成功启动");
Console.Read();
}
}
}

服务端App.config配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="ServerPort" value="8998"/>
</appSettings>
<system.serviceModel>
<services>
<service name="WcfService.NoticeService" behaviorConfiguration="ServiceBehavior" >
<endpoint contract="Contract.INoticeService" address="" binding="basicHttpBinding" bindingConfiguration="ServiceBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="ServiceBinding" sendTimeout="00:00:20" maxReceivedMessageSize="2147483646">
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

四、客户端

NoticeProxy:

using Contract;
using Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace WcfClient
{
/// <summary>
/// 通知公告
/// </summary>
public class NoticeProxy : INoticeService
{
private ChannelFactory<INoticeService> channelFactory;
private INoticeService server; public NoticeProxy()
{
channelFactory = new ChannelFactory<INoticeService>("NoticeService");
server = channelFactory.CreateChannel();
} /// <summary>
/// 获取一条公告信息
/// </summary>
public Notice GetNotice()
{
return server.GetNotice();
}
}
}

客户端调用服务:

using Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WcfClient; namespace WinformClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnTest_Click(object sender, EventArgs e)
{
try
{
NoticeProxy proxy = new NoticeProxy();
Notice notice = proxy.GetNotice();
MessageBox.Show(notice.Data.Length.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

客户端App.config配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="ServiceBinding" maxReceivedMessageSize="2147483646">
<readerQuotas maxArrayLength="65242880" maxStringContentLength="65242880"/>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint name="NoticeService" contract="Contract.INoticeService" address="http://localhost:8998/Service/NoticeService.svc" behaviorConfiguration="ServiceBehavior" binding="basicHttpBinding" bindingConfiguration="ServiceBinding" />
</client>
</system.serviceModel>
</configuration>

最新文章

  1. Linux主机上使用交叉编译移植u-boot到树莓派
  2. 深入理解Spring--动手实现一个简单的SpringIOC容器
  3. centos 研究
  4. java URL实现调用其他系统发送报文并获取返回数据
  5. POJ 2559 Largest Rectangle in a Histogram(单调栈)
  6. 关于int,long,long long
  7. Beautiful 疑问小记
  8. 使用socket.io开发简单群聊功能
  9. SharePoint 使用PowerShell恢复误删的网站集
  10. PHP如何让apache支持.htaccess 解决Internal Server Error The server …错误
  11. spring管理事务需要注意的
  12. django获取ajax的post复杂对象
  13. maven安装配置及使用maven创建一个web项目
  14. db mysql / mysql cluster 5.7.19 / my.cnf / thread_pool_stall_limit
  15. Spring+Struts+Mybatis+Shiro整合配置
  16. mysql5.6.13通用二进制格式安装并使用amoeba实现对mysql5.6数据库读写分离
  17. CF876 F 思维 枚举
  18. SQL数据插入字符串时转义函数
  19. 42. Trapping Rain Water *HARD*
  20. 【python爬虫】常见面试题之http协议问题总结

热门文章

  1. 好记性不如烂笔头-linux学习笔记3mysql数据库导入导出
  2. three3D地图
  3. SpinBlur - 旋转模糊
  4. Kubuntu中thunderbird最小化到任务栏
  5. php闭包bindTo方法用法
  6. Region在connection前后进行“交并差”等操作的异同
  7. Windows c++面向对象与可视化编程的基础知识
  8. HBase 系列(二)安装部署
  9. 基于Web Service的客户端框架搭建四:终结篇
  10. PHPCMS V9 模块开发 二次开发实例 留言本