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 ...
随机推荐
- KingbaseES 通过触发器实现查看表的创建时间
从oracle迁移至KingbaseES的用户,经常会问在KingbaseES中怎么查询表的创建时间. 由于KingbaseES本身并不直接存储表的创建时间,所以获取这一信息通常需要依赖于间接方法或日 ...
- PyCharm字体大小快捷键设置(“ctrl+滚轮”实现字体的随时放大和缩小)
前言:我们在使用PyCharm工具编写Python代码的时候,希望能够随时放大缩小字体,而PyCharm默认是没有设置快捷键的,我们可以自己设置,下面就教大家如何设置. 分为两步设置: PyCharm ...
- GraphPro
GraphPro: Graph Pre-training and Prompt Learning for Recommendation 北京B区 / 032机 北京B区 / 224机 数据集介绍 本文 ...
- #线性dp#CF1110D Jongmah
题目 分析 考虑三个 \((i,i+1,i+2)\) 可以用 \((i,i,i)\) 和 \((i+1,i+1,i+1)\) 和 \((i+2,i+2,i+2)\) 代替, 所以这样的三元组本质上最多 ...
- #前缀和,后缀和#洛谷 4280 [AHOI2008]逆序对
题目传送门 分析 首先填的数字单调不降,感性理解 那可以维护\([a_1\sim a_{i-1}]\)的\(cnt\)后缀和以及 \([a_{i+1}\sim a_n]\)的\(cnt\)前缀和,那可 ...
- #模拟#洛谷 5957 [POI2017]Flappy Bird
题目 分析 小鸟所在坐标的奇偶性一定相同, 考虑每次维护一个可行区间表示小鸟在当前列可以进入的纵坐标区间, 那么它有\(x_i-x_{i-1}\)的纵坐标最大改变差,然后根据奇偶性以及限制区间缩小范围 ...
- ChatGPT商用网站源码+支持ai绘画(Midjourney)+GPT4.0+GPT3.5key绘画+Prompt角色+实时语音识别输入+后台一键版本更新!
ChatGPT商用网站源码+支持ai绘画(Midjourney)+GPT4.0+GPT3.5key绘画+Prompt角色+实时语音识别输入+后台一键版本更新! 1.网站系统源码介绍: 程序已支持Cha ...
- 使用site-maven-plugin在github上搭建公有仓库
目录 简介 前期准备 在maven中配置GitHub权限 配置deploy-plugin 配置site-maven-plugin 怎么使用这个共享的项目 总结 简介 Maven是我们在开发java程序 ...
- Hi3861 通过UART串口协议与其它开发板进行通信
一.搭建编译环境 1.下载虚拟机VMware和Ubuntu20.0.14 下载 VMware Workstation Pro | CN https://www.vmware.com/cn/produc ...
- Hypium框架使能ArkTS应用高效测试
原文链接:https://mp.weixin.qq.com/s/Ncc-x_4zy4wBZmSjknw1lQ,点击链接查看更多技术内容: HarmonyOS发布了声明式开发框架ArkUI,带来了 ...