1.新建一个名为“WindowsForms承载WCF”的WINFORM程序。

2.在解决方案里添加一个“WCF 服务库”的项目,名为“WcfYeah”。

3.修改IService1文件,内容如下:

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web; namespace WcfYeah
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
CompositeType geta(CompositeType composite);
} // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
// 可以将 XSD 文件添加到项目中。在生成项目后,可以通过命名空间“WcfYeah.ContractType”直接使用其中定义的数据类型。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello "; [DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
} [DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}

4.修改Service1,内容如下:

using System;
using System.ServiceModel; namespace WcfYeah
{
// 调整 ServiceBehavior,使其支持并发
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall, UseSynchronizationContext = false)]
public class Service1 : IService1
{
public CompositeType geta(CompositeType composite)
{
CompositeType myret = new CompositeType();
try
{
if(composite==null)
myret.StringValue = "[0]输入实体为空:" + DateTime.Now.ToString();
else
myret.StringValue = "[1]输入实体不为空:" + DateTime.Now.ToString();
}
catch (Exception ex)
{
myret.StringValue = "发生异常:" + ex.Message;
}
return myret;
} }
}

5.在WCF项目中增加一个WCFServer类,内容如下:

using System.Net;
using System.ServiceModel.Web;
using System.Threading; namespace WcfYeah
{
public class WCFServer
{
public WebServiceHost host = null; public WCFServer()
{
#region 优化调整
//对外连接数,根据实际情况加大
if (ServicePointManager.DefaultConnectionLimit < 100)
System.Net.ServicePointManager.DefaultConnectionLimit = 100; int workerThreads;//工作线程数
int completePortsThreads; //异步I/O 线程数
ThreadPool.GetMinThreads(out workerThreads, out completePortsThreads); int blogCnt = 100;
if (workerThreads < blogCnt)
{
// MinThreads 值不要超过 (max_thread /2 ),否则会不生效。要不然就同时加大max_thread
ThreadPool.SetMinThreads(blogCnt, blogCnt);
}
#endregion //注意:这里是实现类,不是接口,否则会报:ServiceHost 仅支持类服务类型。
host = new WebServiceHost( typeof(WcfYeah.Service1));
} public void Start()
{
host.Open();
} public void Close()
{
if (host != null)
{
host.Close();
}
}
}
}

6.修改WINFORM程序的APP.CONFIG,完整内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup> <system.serviceModel>
<services>
<service name="WcfYeah.Service1" behaviorConfiguration="behaviorThrottled">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:16110/myapi/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- 除非完全限定,否则地址相对于上面提供的基址-->
<endpoint address="" binding="webHttpBinding" contract="WcfYeah.IService1" bindingConfiguration="WebHttpBinding_IService">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding_IService" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorThrottled">
<serviceThrottling
maxConcurrentCalls="96"
maxConcurrentSessions="600"
maxConcurrentInstances="696"
/>
<!-- 为避免泄漏元数据信息,
请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="False" httpsGetEnabled="False"/>
<!-- 要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false
以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>

7.回到WINFORM程序,在FORM LOAD中启动这个WCF,FormClosing中关闭这个WCF。

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 WcfYeah; namespace WindowsForms承载WCF
{
public partial class Form1 : Form
{
WCFServer wcfs = null; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
try
{
wcfs = new WCFServer();
wcfs.Start();
lblTip.Text = "服务已运行";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (wcfs != null)
{
wcfs.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

8.用管理员权限启动这个WINFORM程序。

9.使用POSTMAN调用。

地址:http://localhost:16110/myapi/geta

请求报文:

{
    "BoolValue": true,
    "StringValue": "11"
}
响应报文:
{
    "BoolValue": true,
    "StringValue": "输入实体不为空:2021/11/30 9:38:41"
}
 

C#.NET Winform承载WCF RESTful API (App.config 方式)的更多相关文章

  1. 关于RESTFUL API 安全认证方式的一些总结

    常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...

  2. 关于 RESTFUL API 安全认证方式的一些总结

    常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...

  3. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

  4. RESTFUL API 安全认证方式

    一般基于REST API 安全设计常用方式有: HTTP Basic Basic admin:admin Basic YWRtaW46YWRtaW4= Authorization: Basic YWR ...

  5. C# winform 发布的时候没有app.config去哪儿了?

    有时候winform发布的时候app.config不见了? 1.我们来到生成文件的目录下 找到后缀是 .config 的文件右击,打开,也可以用其他方式打卡,我这里使用的是sublime这个文本编辑器 ...

  6. yii2 restful api——app接口编程实例

    <?php namespace common\components; use common\models\Cart; use common\models\User; use Yii; use y ...

  7. yii2 restful api --app接口编程

    转 http://www.yiichina.com/tutorial/1143yii2中restful url访问配置, 登陆接口access-token验证类 [ 2.0 版本 ] 登陆接口acce ...

  8. winform 承载 WCF 注意,可能不是工作在多线程模式下

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMo ...

  9. Winform 数据库连接app.config文件配置 数据库连接字符串

    1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...

  10. openstack操作之二 restful api

    Restful api 是openstack各服务调用的接口,简单理解为可以通过网络去调用的函数.postman是一款前端调用工具,测试后端接口的时候往往是使用该工具去验证.在openstack的使用 ...

随机推荐

  1. 牛客网-SQL专项训练23

    ①假设创建新用户nkw,现在想对于任何IP的连接,仅拥有user数据库里面的select和insert权限,则列表语句中能够实现这一要求的语句是(B) 解析: 考察知识点-数据库授权命令: GRANT ...

  2. Understand Abstraction and Interface

    Foreword 抽象和接口是Java中的两个关键字,也是两种最基本的优化软件项目手段.为什么说它们是一种优化项目的手段? 人分三六九等,不同等级的人,所接触的事和处理的事是不一样的.同理,项目也分大 ...

  3. 中仑网络全站 Dubbo 2 迁移 Dubbo 3 总结

    简介: 中仑网络在 2022 年完成了服务框架从 Dubbo 2 到 Dubbo 3 的全站升级,深度使用了应用级服务发现.Kubernetes 原生服务部署.服务治理等核心能力.来自中仑网络的技术负 ...

  4. DTCC 2020 | 阿里云程实:云原生时代的数据库管理

    简介: 随着云原生技术的不断发展,数据库也逐渐进入了云原生时代.在云原生时代,如何高效.安全且稳定地管理云上与云下的数据库成为摆在企业面前的一大难题.在第十一届中国数据库技术大会(DTCC2020)上 ...

  5. Serverless在游戏运营行业进行数据采集分析的最佳实践

    简介: 这个架构不光适用于游戏运营行业,其实任何大数据采集传输的场景都是适用的,目前也已经有很多客户正在基于Serverless的架构跑在生产环境,或者正走在改造Serverless 架构的路上. 众 ...

  6. [FAQ] gormV2 Too many connections

    gormV2 中不再有v1的 db.Close() 方法. 取而代之的 close 方式是如下: sqlDB, err := DB.DB() sqlDB.Close() https://github. ...

  7. [FAQ] 夏玉米 按规则查询域名靠谱吗 ?

    很早就有一个网站叫 夏玉米,可以按规则查询和注册域名,那么它真如我们想的那样 可以找到好域名吗? 虽然看起来很好用,实际上夏玉米的查询只是针对它自己的数据库,不包含未在其平台注册的域名,所以大家要失望 ...

  8. three.js实现相机碰撞,相机不穿墙壁、物体

    大家好,本文实现了相机碰撞检测,使相机不穿墙壁.物体,并给出了思路和代码,感谢大家~ 关键词:数字孪生.three.js.Web3D.WebGL.相机碰撞.游戏相机 我正在承接Web3D数字孪生项目, ...

  9. Java开启异步的两种方式

    二.Java开启异步的两种方式 1.注解开启:@Async 1.1.配置异步的线程池 必须配置异步线程池,否则异步不会生效. @EnableAsync 注解:指定异步线程池.不指定默认使用:Simpl ...

  10. appium测试混合应用

    最近用appium测试公司APP,APP是原生+H5的模式,测试过程中发现大部分H5的页面使用原生的方式可以进行操作,只有少部分H5页面的按钮虽然在uiautomatorviewer的界面能解析出来, ...