博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
启动WCF多个服务方法
阅读量:5311 次
发布时间:2019-06-14

本文共 2676 字,大约阅读时间需要 8 分钟。

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

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>

*/

 

转载于:https://www.cnblogs.com/yshj/p/3785363.html

你可能感兴趣的文章
MySQL基础3
查看>>
云计算数据与信息安全防护
查看>>
全局设置导航栏
查看>>
RxJS & Angular
查看>>
面向对象(多异常的声明与处理)
查看>>
MTK笔记
查看>>
ERROR: duplicate key value violates unique constraint "xxx"
查看>>
激活office 365 的启动文件
查看>>
无法根据中文查找
查看>>
[简讯]phpMyAdmin项目已迁移至GitHub
查看>>
转载 python多重继承C3算法
查看>>
【题解】 bzoj1597: [Usaco2008 Mar]土地购买 (动态规划+斜率优化)
查看>>
css文本溢出显示省略号
查看>>
git安装和简单配置
查看>>
面向对象:反射,双下方法
查看>>
鼠标悬停提示文本消息最简单的做法
查看>>
Java面向对象重要关键字
查看>>
课后作业-阅读任务-阅读提问-2
查看>>
面向对象设计中private,public,protected的访问控制原则及静态代码块的初始化顺序...
查看>>
fat32转ntfs ,Win7系统提示对于目标文件系统文件过大解决教程
查看>>