引用就不说明,直接贴上:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Configuration;
using System.Collections;

public class WCFServiceHelper
{

private static List<ServiceHost> _ServiceHost = new List<ServiceHost>();

/// <summary>
/// 启动WCF服务
/// </summary>
public static void ServiceStart()
{

//配置的服务地址,自己配置
string WCFServiceAddress = ConfigurationManager.AppSettings["WCFServiceAddress"];

/* 配置的格式如

<appSettings>
<add key="WCFServiceAddress" value="net.tcp://127.0.0.1:8888"/>
</appSettings>

*/

//获取配置文件的服务名称,就是那些接口,形式看具体需求去配

/* 如果多个服务要逗号分隔开,My.IService1,My.IService2服务接口,My.Service.Service1,My.Service.Service2服务实现

<WCFServices>
<add key="My.IService1,My.IService2" value="My.Service.Service1,My.Service.Service2"/>

多个就继续往下配置

</WCFServices>

*/
IDictionary WCFDict = ConfigurationManager.GetSection("WCFServices") as IDictionary;//获取自定义的节点
if (WCFDict != null)
{
string ServiceName = string.Empty;
NetTcpBinding Binding;
string[] Services;
string[] IServices;
ServiceHost host;

foreach (DictionaryEntry dict in WCFDict)//这里面遍历绑定服务
{

ServiceName = dict.Value.ToString();
Services = ServiceName.Split(',')[0].Split('.');
IServices = dict.Key.ToString().Split(',')[0].Split('.');

Binding = new NetTcpBinding("TcpSet");//绑定协议

Binding.ReceiveTimeout = new TimeSpan(0, 30, 0);

Binding.Security.Mode = SecurityMode.None;

host = new ServiceHost(Type.GetType(ServiceName));
host.AddServiceEndpoint(Type.GetType(dict.Key.ToString()), Binding, string.Format("{0}/{1}", WCFServiceAddress, Services[Services.Length - 1]));

try
{
host.Open();

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(string.Format("{0}启动成功", string.Format("{0}/{1}", WCFServiceAddress, Services[Services.Length - 1])));
_ServiceHost.Add(host);
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Format("{0}启动失败,失败原因:{1}", string.Format("{0}/{1}", WCFServiceAddress, Services[Services.Length - 1]),exception.ToString()));
}
}
}
}

/// <summary>
/// 停止WCF服务
/// </summary>
public static void ServiceStop()
{
foreach (ServiceHost host in _ServiceHost)
{
host.Close();
}
}
}

/*绑定的配置

<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="TcpSet" maxBufferSize="2147483647" receiveTimeout="00:30:00" sendTimeout="00:30:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxConnections="100000">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647 " maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>

*/

启动WCF多个服务方法的更多相关文章

  1. IOS调用WCF提供的服务方法,但是方法的参数是WCF那边自定义的对象,这样有办法调用么,如果可以IOS应该怎么传参呢?请问有了解的么,

    最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想到其中碰到不少问题,在网上搜索很久一直没有想到 ...

  2. Appium+Python app自动化测试之脚本启动和停止Appium服务

    研究了一段时间的Appium android app的自动化测试,工作中需要连接多台手机终端同时执行测试用例,我实现的方式是获取用例中需要执行用例的设备id个数以及实际连接到的设备数(通过adb de ...

  3. 利用反射,批量启动WCF服务

    对于WCF的宿主启动来说,有好多方法,单独启动也很简单,可以根据业务需要来自由选择(单独启动方法这里就不做解释) 对于业务服务比较多的时候,往往需要多个服务来承载系统,但是如果将服务启动单独写代码启动 ...

  4. centos 6 与 centos 7 服务开机启动、关闭设置的方法

    简单说明下 centos 6 与 centos 7 服务开机启动.关闭设置的方法: centos 6 :使用chkconfig命令即可. 我们以apache服务为例: #chkconfig --add ...

  5. 关于在安装MySQL时报错"本地计算机上的mysql服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止"的解决方法

    首先将你下载的MySQL安装或者解压(对应安装版和解压版),下载地址http://dev.mysql.com/downloads/mysql/ 然后复制你安装目录中的my-default.ini,更改 ...

  6. Windows无法启动 VMware Workstation server服务解决方法

    Windows无法启动VMware Workstation server服务, 可以通过删除datastores.xml文件来解决. 具体操作步骤如下: 1.在系统盘目录下,找到C:\ProgramD ...

  7. [WCF编程]8.服务实例的生命周期

    一.服务实例的生命周期概览 我们已经直到,通过显式调用Close方法或等待默认的超时时间到来,都可以释放服务实例.但是,在会话连接里,经常需要按一定顺序调用方法. 二.分步操作 会话契约的操作有时隐含 ...

  8. WCF初探-10:WCF客户端调用服务

    创建WCF 服务客户端应用程序需要执行下列步骤: 获取服务终结点的服务协定.绑定以及地址信息 使用该信息创建 WCF 客户端 调用操作 关闭该 WCF 客户端对象 WCF客户端调用服务存在以下特点: ...

  9. 跟我一起学WCF(11)——WCF中队列服务详解

    一.引言 在前面的WCF服务中,它都要求服务与客户端两端都必须启动并且运行,从而实现彼此间的交互.然而,还有相当多的情况希望一个面向服务的应用中拥有离线交互的能力.WCF通过服务队列的方法来支持客户端 ...

随机推荐

  1. VBA中如何动态定义数组

    利用 dim Arr()as string这样声明,一旦赋值后,数组大小也就固定了.如果要改变数组大小,要用redim 命令redim arr(10) 加preserve 可以不清空数组,保持原有数据 ...

  2. 让VS 2010在调试字符串时,支持Json数据格式友好显示

    阅读本文如果对Microsoft.VisualStudio.DebuggerVisualizers的用法不熟悉的,可以参考这篇文章.http://www.cnblogs.com/devil0153/a ...

  3. 机器学习笔记——支持向量机 (SVM)

    声明: 机器学习系列主要记录自己学习机器学习算法过程中的一些参考和总结,其中有部分内容是借鉴参考书籍和参考博客的. 目录: 什么支持向量机(SVM) SVM中必须知道的概念 SVM实现过程 SVM核心 ...

  4. mvc4 分离Controller 出现 未找到路径“/”的控制器或该控制器未实现 IController

    一般MVC项目都会把Controller 分离出来独立类库,以前用mvc3一直这样做,测试发布都能够正常运行,这次用了mvc4,发现会报错:HTTP 404.您正在查找的资源(或者它的一个依赖项)可能 ...

  5. Ubuntu下使用SVN

      SVN作为日常开发中不可缺少的工具,今天终于开始在Ubuntu下使用了. 1.首先需要安装SVN.Ubuntu下的SVN安装十分简单,sudo apt-get install subversion ...

  6. 构建最小的docker容器

    创建一个最小的基本镜像: tar cv --files-from /dev/null | sudo docker import - skycn/base 建一个hello.go: package ma ...

  7. Find out files transfered via Bluetooth

    The case was about business secret and forensic guy did a physical acquisition from a smart phone. H ...

  8. leetcode 217

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  9. 我现在的vimrc配置文件

    runtime! debian.vim "设置编码 set encoding=utf- set fencs=utf-,ucs-bom,shift-jis,gb18030,gbk,gb2312 ...

  10. Web后台技术趋势

    今天使用Google Trend比较了一下服务器端的程序开发语言技术ASP/ASP.NET Core, PHP, Node.Js的变化趋势,发现一下特点. ASP.NET最近几年一直再下降. ASP和 ...