C#.NET Winform承载WCF RESTful API (App.config 方式)
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
请求报文:
C#.NET Winform承载WCF RESTful API (App.config 方式)的更多相关文章
- 关于RESTFUL API 安全认证方式的一些总结
常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...
- 关于 RESTFUL API 安全认证方式的一些总结
常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...
- RESTFUL API 安全认证方式
一般基于REST API 安全设计常用方式有: HTTP Basic Basic admin:admin Basic YWRtaW46YWRtaW4= Authorization: Basic YWR ...
- C# winform 发布的时候没有app.config去哪儿了?
有时候winform发布的时候app.config不见了? 1.我们来到生成文件的目录下 找到后缀是 .config 的文件右击,打开,也可以用其他方式打卡,我这里使用的是sublime这个文本编辑器 ...
- yii2 restful api——app接口编程实例
<?php namespace common\components; use common\models\Cart; use common\models\User; use Yii; use y ...
- yii2 restful api --app接口编程
转 http://www.yiichina.com/tutorial/1143yii2中restful url访问配置, 登陆接口access-token验证类 [ 2.0 版本 ] 登陆接口acce ...
- winform 承载 WCF 注意,可能不是工作在多线程模式下
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMo ...
- Winform 数据库连接app.config文件配置 数据库连接字符串
1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...
- openstack操作之二 restful api
Restful api 是openstack各服务调用的接口,简单理解为可以通过网络去调用的函数.postman是一款前端调用工具,测试后端接口的时候往往是使用该工具去验证.在openstack的使用 ...
随机推荐
- 一文搞懂 SAE 日志采集架构
简介: 本文将着重介绍了 SAE 提供了多种日志采集方案,以及相关的架构,场景使用特点,点击下文,立即查看吧- 作者:牛通(奇卫) 日志,对于一个程序的重要程度不言而喻.无论是作为排查问题的手段, ...
- 做ToB软件质量保障的这两年
简介:自己算是阿里的老兵了,从实习开始一直投身在 toB 业务的质量保障领域内,不能说是资深的专家,但所经历的.感受的业务特点和体会还是具有一定的代表性,希望能通过这篇文章,总结一下过往,并能和已经 ...
- 微信不再提供小程序打开App?借助H5为App引流的方式你必须知道!
简介: 2021年5月14日App开发者领域发布了一条重要消息:微信开放平台为了提升用户体验,将于2021年5月20日(后来延期到2021年5月27日)起不再提供"小程序打开App技术服务& ...
- [FAQ] 钉钉 Excel 回车键不能换行 ? 在线编辑如何换行
Win 端表格换行:AIT+ENTER Mac 端表格换行:AIT OPTION+ENTER Tool:ChatAI Refer:钉钉技巧 Refer:https://www.dingtalk.c ...
- [FAQ] swagger-php 支持 Authorization Bearer token 校验的用法
@OA\SecurityScheme 可以是 Controller 层面也可以是 Action 层面. 类型 type="apiKey". in="header" ...
- [FAQ] uni-app 导航路由切换时如何强制刷新页面?
使用 this.$forceUpdate() 强制刷新页面. Refer:uni-app自定义导航 Link:https://www.cnblogs.com/farwish/p/13870801.ht ...
- [FAQ] Vue iframe 的 src 是链接地址却加载了相对路径 ?
iframe 的 src 是链接, 但是加载的实际链接是相对路径,只有一种可能:链接地址不正确. 检查链接有没有少符号,常见错误:http//,http:/ Refer:Vue的iframe错误 Li ...
- [FAQ] SSH 免密登录主机/服务器 怎么操作 ?
1. 生成公私钥对,保存好. 命令:ssh-keygen -t rsa -C "xxx" 2. 将公钥传到远程主机的 ~/.ssh/authorized_keys 之中. 命令:s ...
- Competition Set - Codeforces
这里记录的是这个账号的比赛情况. Codeforces Round 942 (Div. 1) Solved:6/8,AB1B2CDE1 2645-> A 题意:现有 \(a_i\) 张写有 \( ...
- windows10 64 解决 exec:"gcc" executable file not found in %PATH%报错问题
1.下载编译好的包 https://sourceforge.net/projects/mingw-w64/files/mingw-w64/ 这里我选择64位的 2.解压完之后,配置环境变量. 解压完到 ...