Winform项目中纯代码创建WCF服务
接口:
[ServiceContract(CallbackContract = typeof(IViewCallback), SessionMode = SessionMode.Required)]
public interface IViewService
{
[OperationContract]
void ServiceTest();
}
类:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ViewService : IViewService
{
//在ViewLibary中的ViewManager中注册
public event Action<ClientData> ServiceTestEvent;//用于客户端测试与服务端的WCF连接事件
private ClientData GetClientData()
{
OperationContext context = OperationContext.Current;
MessageProperties properties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
return new ClientData(endpoint.Address, endpoint.Port);
}
void IViewService.ServiceTest()
{
ClientData client = GetClientData();
ServiceTestEvent?.Invoke(client);
}
}
public class ClientData
{
private string _IP;
private int _Port;
public string IP { get { return _IP; } set { _IP = value; } }
public int Port { get { return _Port; } set { _Port = value; } } public ClientData(string ip, int port)
{
_IP = ip;
_Port = port;
}
}
参数定义:
private ServiceHost _WcfHost = null;
private ViewService _MainService = null;
private int _MetaPort = 58942;
private int _DataPort = 58946;
public ServiceHost WcfHost { get { return _WcfHost; } }
public ViewService MainService { get { return _MainService; } }
public int MetaPort { get { return _MetaPort; } }
public int DataPort { get { return _DataPort; } }
初始化服务端:
private void InitialHost()
{
#region TCP
//Uri tcpa = new Uri(string.Format("net.tcp://127.0.0.1:{0}/A/ViewService/", DataPort));
//_WpfHost = new ServiceHost(MainService, tcpa);
//NetTcpBinding tcpb = new NetTcpBinding();
//ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
//_WpfHost.Description.Behaviors.Add(mBehave);
//_WpfHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
//_WpfHost.AddServiceEndpoint(typeof(IViewService), tcpb, tcpa);
#endregion
#region HTTP
Uri baseAddress = new Uri(string.Format("http://localhost:{0}/A/ViewService/", MetaPort));
_WcfHost = new ServiceHost(MainService, baseAddress);//MainService用于处理客户端请求
string tcpAddress = string.Format("net.tcp://127.0.0.1:{0}/A/ViewService/", DataPort);
NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.Security.Mode = SecurityMode.None;
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
_WcfHost.Description.Behaviors.Add(mBehave);
WcfHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
WcfHost.AddServiceEndpoint(typeof(IViewService), tcpBinding, tcpAddress);
#endregion
ServiceDebugBehavior debugBehavior = WcfHost.Description.Behaviors.Find<ServiceDebugBehavior>();
if (debugBehavior == null)
{
debugBehavior = new ServiceDebugBehavior();
debugBehavior.IncludeExceptionDetailInFaults = true;
WcfHost.Description.Behaviors.Add(debugBehavior);
}
else
{
debugBehavior.IncludeExceptionDetailInFaults = true;
}
}
启动:
private void Main()
{
if (WcfHost.State == CommunicationState.Created)
{
try
{
WcfHost.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Winform项目中纯代码创建WCF服务的更多相关文章
- STS热部署,springboot项目中修改代码不用重新启动服务
方法如下: 1.在pom文件中引入 devtools 依赖: <dependency> <groupId>org.springframework.boot</grou ...
- WCF入门教程(二)如何创建WCF服务
WCF入门教程(二)从零做起-创建WCF服务 通过最基本的操作看到最简单的WCF如何实现的.这是VS的SDK默认创建的样本 1.创建WCF服务库 2.看其生成结构 1)IService1.cs(协议) ...
- WCF入门(五)---创建WCF服务
使用Microsoft Visual Studio2012创建WCF服务,理解如下所有必要的编码,更好地创建WCF服务的概念,这里做一个简单的任务. 启动Visual Studio 2012. 单击新 ...
- VS2010中使用Jquery调用Wcf服务读取数据库记录
VS2010中使用Jquery调用Wcf服务读取数据库记录 开发环境:Window Servere 2008 +SQL SERVE 2008 R2+ IIS7 +VS2010+Jquery1.3.2 ...
- WCF入门教程(二)从零做起-创建WCF服务
通过最基本的操作看到最简单的WCF如何实现的.这是VS的SDK默认创建的样本 1.创建WCF服务库 2.看其生成结构 1)IService1.cs(协议) 定义了协议,具体什么操作,操作的参数和返回值 ...
- NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...
- iOS开发 纯代码创建UICollectionView
转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html iOS开发 纯代码创建UICollectionView 习惯了使用xi ...
- 快速创建WCF服务和svcutil.exe工具使用
先简单的创建WCF服务: 系统会自动加上IService1接口 和 Service1 实现类 分别在IService1 和Service1 加上2段代码. [ServiceContract] publ ...
- ios - 纯代码创建collectionView
开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...
- C# winform项目中ListView控件使用CheckBoxes属性实现单选功能
C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...
随机推荐
- 使用 shell 脚本自动申请进京证 (六环外)
问题背景 外地车辆进入北京,需要办理<进京证>,不办理证件驶入后会被执法设备抓拍,一次罚 100 扣 1 分,目前唯一的线上办理通道是下载<北京交警>App,注册后添加车辆,就 ...
- Linux内核数据管理利器--红黑树
目录 写在前面 1. 红黑树的原理 2. 红黑树操作 2.1 红黑树的节点插入 2.2 红黑树的节点删除 2.3 红黑树的查询操作 3. 红黑树操作实验 附录A: 实验代码 写在前面 本文通过两个方面 ...
- Apache Thrift 白皮书
介绍: 轻量级.跨语言. 简洁的抽象和实现:数据传输.序列化.应用逻辑处理. IDL及代码生成系统. 基本架构图如下: 28种语言支持:28 programming languages. 支持客户端及 ...
- 7 JavaScript循环语句
7 循环语句 在js中有三种循环语句. 首先是while循环. 它的逻辑和咱们python中的while几乎一模一样, 就是符号上有些许的区别. // 语法 while(条件){ 循环体 -> ...
- #根号分治,分块#洛谷 5309 [Ynoi2011] 初始化
题目传送门 分析 如果 \(x\) 比较大那么可以暴力修改,\(x\) 比较小的话可以用数组打标记 查询的时候对于暴力修改的部分可以分块,暴力修改的同时需要给块打标记 如果 \(x\) 比较小的情况, ...
- #计数#CF10C Digital Root
题目 定义\(d(x)\)为\(x\)的数位和嵌套,直至\(0\leq d(x)<10\) 询问在\([1\sim n]\)中有多少个三元组\((a,b,c)\)满足 \[ab\neq c,d( ...
- #排列组合#C 模拟比赛
分析 由于每个选手的得分独立,考虑按照选手的最高得分降序排序 如果当前枚举到选手\(i\),首先记录\(o_i\)表示在选手\(i\)之前最小得分不低于选手\(i\)的最高得分 (必选,等于必选当且仅 ...
- 2. Solving Linear Equations
2.1 Linear Equations Picture Row Picture 2 by 2 equations Two equations, Two unknowns \[\begin{matri ...
- 第一视角看方法调用时的jvm
关于比较学术的jvm每个内存区域我之前都写过,就不重复赘述了,这里附上链接:https://www.cnblogs.com/gmt-hao/p/13603534.html, https://www.c ...
- Windows wsl2支持systemd
背景 很多Linux发行版都是使用systemd来管理程序进程,但是在WSL中默认是用init来管理进程的. 为了符合长久的使用习惯,且省去不必要的学习成本,就在WSL的发行版(我这里安装的是Ubun ...