C#.NET 操作Windows服务承载WCF
Windows服务的制作、安装可以参考这篇:
C#.NET 操作Windows服务(安装、卸载) - runliuv - 博客园 (cnblogs.com)
本篇会在这个解决方案基础上,继续修改。
一、制作WCF
我们在原有解决方案上添加一个“WCF 服务库”,为名“WcfYeah”。
在WcfYeah中额外引用System.ServiceModel.Web.dll程序集。
修改IService1.cs:
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; }
}
}
}
修改Service1.cs:
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;
} }
}
增加一个WCFServer.cs:
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();
}
}
}
}

二、在Windows服务中调用WCF
在windows服务库中引用WcfYeah这个项目,再引用System.ServiceModel.dll 和System.ServiceModel.Web.dll程序集。
修改windows服务的OnStart和OnStop方法,完整内容如下:
using CommonUtils;
using System;
using System.ServiceModel;
using System.ServiceProcess;
using WcfYeah; namespace ADemoWinSvc
{
public partial class Service1 : ServiceBase
{
WCFServer aw = null;
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
try
{
if (aw == null)
{
aw = new WCFServer();
}
if (aw.host.State == CommunicationState.Opening || aw.host.State == CommunicationState.Opened)
{
GLog.WLog("[1]服务已启动。");
return;
} aw.Start(); GLog.WLog("[2]服务已启动");
}
catch (Exception ex)
{
GLog.WLog("OnStart ex:" + ex.Message);
}
} protected override void OnStop()
{
try
{
aw.Close(); GLog.WLog("服务已停止");
}
catch (Exception ex)
{
GLog.WLog("OnStop ex:" + ex.Message);
}
}
}
}
在windows服务库中添加一个应用配置文件(App.config),用来配置WCF服务,内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <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>

编译ADemoWinSvc项目,将编译好的ADemoWinSvc.exe、ADemoWinSvc.exe.config和WcfYeah.dll这3个文件复制到WINFORM程序编译输出目录,与Windows服务操作.exe文件放在一起。
用管理员权限启动Windows服务操作.exe,点击安装按钮。

进入Logs文件夹,查看Logs文件,如果显示“[2]服务已启动”,说明windows服务安装成功,WCF正常启动。

使用POSTMAN调用。
地址:http://localhost:16110/myapi/geta
方法:POST
请求报文:
C#.NET 操作Windows服务承载WCF的更多相关文章
- Windows服务承载WCF
Source文件 ------------------------- using System; using System.Collections.Generic; using System.Linq ...
- C# 操作windows服务[启动、停止、卸载、安装]
主要宗旨:不已命令形式操作windows服务 static void Main(string[] args) { var path = @"E:\开发辅助项目\WCF\WCF.Test\WC ...
- WCF开发实战系列四:使用Windows服务发布WCF服务
WCF开发实战系列四:使用Windows服务发布WCF服务 (原创:灰灰虫的家http://hi.baidu.com/grayworm) 上一篇文章中我们通过编写的控制台程序或WinForm程序来为本 ...
- C#通过SC命令和静态公共类来操作Windows服务
调用的Windows服务应用程序网址:http://www.cnblogs.com/pingming/p/5115304.html 一.引用 二.公共静态类:可以单独放到类库里 using Syste ...
- cmd命令行和bat批处理操作windows服务(转载)
一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32srv ...
- C#开发人员能够可视化操作windows服务
使用C#开发自己的定义windows服务是一个很简单的事.因此,当.我们需要发展自己windows它的服务.这是当我们需要有定期的计算机或运行某些程序的时候,我们开发.在这里,我有WCF监听案例,因为 ...
- SC命令---安装、开启、配置、关闭 cmd命令行和bat批处理操作windows服务
一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32s ...
- 基于Windows服务的WCF
(1)创建WCF 代码示例: [ServiceContract] public interface ILimsDBService { [OperationContract] int ExecuteSq ...
- 编写寄宿于windows服务的WCF服务
由于业务中有些任务需要在后台静默长期运行,或者有些服务队响应的要求比较苛刻,这样的WCF服务就不适合寄宿于IIS中.IIS每隔一段时间w3wp进程会闲置超时,造成服务的运行停止,因此这种耗时或者定时任 ...
随机推荐
- Azure Bicep(三)变量控制
一,引言 当我们在使用 Azure Bicep 的时候会出现以下几个问题: 1)文件中有很多地方会重用很多相同的值 2)输入参数可以在统一的地方进行修改 带着这些问题,我们开始今天的内容,学习如何在 ...
- 使用YApi搭建API接口管理工具(docker安装)
使用YApi搭建API接口管理工具(docker安装) 工具描述 YApi 是高效.易用.功能强大的 api 管理平台,旨在为开发.产品.测试人员提供更优雅的接口管理服务.可以帮助开发者轻松创建.发布 ...
- c语言中for循环 和嵌套for循环
for循环:for( ; ; )里面是bai3个语句,两个分号.第一个语句是开始前执行,第二个语句是判断真假,如果真,就执行后面(大括号内)的代码.第三个语句是每次执行完毕后执行的东西,通常第三个语句 ...
- Shiro反序列化的检测与利用
1. 前言 Shiro 是 Apache 旗下的一个用于权限管理的开源框架,提供开箱即用的身份验证.授权.密码套件和会话管理等功能. 2. 环境搭建 环境搭建vulhub 3. 如何发现 第一种情况 ...
- px,dp sp是像素、尺寸、尺寸
px:即像素,1px代表屏幕上一个物理的像素点:px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小可能不同,如下图所示(图片来自android developer guide, ...
- 机器学习:KNN
KNN:K-nearst neighbors 简介: k-近邻算法采用测量不同特征值之间的距离来进行分类,简而言之为:人以类聚,物以群分 KNN既可以应用于分类中,也可用于回归中:在分类的预测是,一般 ...
- MiniFly四轴飞行器之部分系统及电源分析
最近硬件四轴很火,了解了很久,还是选择了MiniFly,主要还是资料多,后边可以有人讨论,不像很多就是建了个群,研究问题还是在论坛方便很多. 四轴终于拿到手,功能很强大,主要是还支持二次开发,可以研究 ...
- linux Segmentation faults 段错误详解
什么是段错误 下面是来自 Answers.com 的定义: A segmentation fault (often shortened to segfault) is a particular err ...
- ARM 链接配置.lds文件学习<转>
本文由Jacky原创,来自http://blog.chinaunix.net/u1/58780/showart.php?id=462971 对于.lds文件,它定义了整个程序编译之后的连接过程,决定了 ...
- cm0 逆向分析
目录 cm0 逆向分析 前言 Strings工具复习 String工具使用说明 Strings工具解cm0题 cm0 逆向分析 前言 Emmmmm,我假装你看到这里已经学过了我的<恶意代码分析实 ...