快上网建站品牌

13518219792
  • 首页
  • 关于我们
    • 如何选择
    • 选择理由
  • 案例作品
    • 网站建设
    • 优化推广
    • 微信开发
    • 电商托管
  • 服务项目
    • 网站建设
    • 移动端/APP
    • 微信/小程序
    • 技术支持
    • 其它服务
  • 建站知识
    • 成都网站建设
    • 成都做网站
    • 成都网站设计
  • 网站售后
    • 成都网站运营
    • 成都网站维护
    • 成都网站推广
  • 客服中心
  • 全国分站

WCF服务加载实际应用方法详解

WCF开发工具中有很多比较重要的操作技术是需要我们去熟练的掌握,以至于在实际应用中获得些帮助。今天我们就为大家介绍一下有关WCF服务加载在实现的过程中出现的一些问题的解决方法。

创新互联公司专注于赤峰林西企业网站建设,自适应网站建设,购物商城网站建设。赤峰林西网站建设公司,为赤峰林西等地区提供建站服务。全流程按需网站建设,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

如果用self-host的方式来实现WCF服务加载的话,我们一般是用这样的代码:ServiceHost host1 = new ServiceHost(typeof(UserService)); 问题是,如果当你的服务很多的时候这样做是不胜其烦的,今天在看Ingo Rammer(大名鼎鼎的《Advanced .Net Remoting》作者)的Blog的时候,看到了他解决这一问题的方法:

一.服务配置在app.config或web.config中:

 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Configuration;
  5. using System.ServiceModel.Configuration;
  6. using System.ServiceModel;
  7. public class ServiceHostGroup
  8. {
  9. static List< ServiceHost> _hosts = new List< ServiceHost>();
  10. private static void OpenHost(Type t)
  11. {
  12. ServiceHost hst = new ServiceHost(t);
  13. hst.Open();
  14. _hosts.Add(hst);
  15. }
  16. public static void StartAllConfiguredServices()
  17. {
  18. Configuration conf = 
  19. ConfigurationManager.OpenExeConfiguration(Assembly.
    GetEntryAssembly().Location);
  20. ServiceModelSectionGroup svcmod = 
  21. (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
  22. foreach (ServiceElement el in svcmod.Services.Services)
  23. {
  24. Type svcType = Type.GetType(el.Name);
  25. if (svcType == null) 
  26. throw new Exception("Invalid Service Type " + el.Name + 
    " in configuration file.");
  27. OpenHost(svcType);
  28. }
  29. }
  30. public static void CloseAllServices()
  31. {
  32. foreach (ServiceHost hst in _hosts)
  33. {
  34. hst.Close();
  35. }
  36. }
  37. }

WCF服务加载解决问题方法步骤之二.服务配置在外部配置文件中:

Services.XML:

 
 
 
  1. < configuredServices>
  2. < service type="ServiceImplementation.ArticleService, 
    ServiceImplementation" />
  3. < /configuredServices>

ServiceHostBase.cs:

 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Configuration;
  5. using System.ServiceModel.Configuration;
  6. using System.ServiceModel;
  7. using System.Xml;
  8. using System.Xml.Serialization;
  9. using System.IO;
  10. public class ServiceHostGroup
  11. {
  12. static List< ServiceHost> _hosts = new List< ServiceHost>();
  13. private static void OpenHost(Type t)
  14. {
  15. ServiceHost hst = new ServiceHost(t);
  16. Type ty = hst.Description.ServiceType;
  17. hst.Open();
  18. _hosts.Add(hst);
  19. }
  20. public static void StartAllConfiguredServices()
  21. {
  22. ConfiguredServices services = ConfiguredServices.
    LoadFromFile("services.xml");
  23. foreach (ConfiguredService svc in services.Services)
  24. {
  25. Type svcType = Type.GetType(svc.Type);
  26. if (svcType == null) throw new Exception("Invalid Service Type " 
    + svc.Type + " in configuration file.");
  27. OpenHost(svcType);
  28. }
  29. }
  30. public static void CloseAllServices()
  31. {
  32. foreach (ServiceHost hst in _hosts)
  33. {
  34. hst.Close();
  35. }
  36. }
  37. }
  38. [XmlRoot("configuredServices")]
  39. public class ConfiguredServices
  40. {
  41. public static ConfiguredServices LoadFromFile(string filename)
  42. {
  43. if (!File.Exists(filename)) return new ConfiguredServices();
  44. XmlSerializer ser = new XmlSerializer(typeof(ConfiguredServices));
  45. using (FileStream fs = File.OpenRead(filename))
  46. {
  47. return (ConfiguredServices) ser.Deserialize(fs);
  48. }
  49. }
  50. [XmlElement("service", typeof(ConfiguredService))]
  51. public List< ConfiguredService> Services = new List
    < ConfiguredService>();
  52. }
  53. public class ConfiguredService
  54. {
  55. [XmlAttribute("type")]
  56. public string Type;
  57. }

以上就是对WCF服务加载中遇到的相关问题的解决方法。


本文题目:WCF服务加载实际应用方法详解
网页链接:http://csdahua.cn/article/cdihpdd.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流

其他资讯

  • 幸福工厂服务器怎么创建?架设工厂服务器教程
  • 5G室内覆盖性能及关键问题分析
  • milocloud好用吗?(ucloud的云服务器效果怎样)
  • 云服务器连接超时是什么原因?云服务器系统影响速度吗
  • 营销型网站建设的流量转化应该怎么做?

行业动态

企业网站建设的重要性!

现在虽然是移动互联网时代,但企业网站依然重要,包含PC站点,移动站。可以说企业网站关系企业的未来发展和前途,尤其对中小企业更是如此,一些中小企业老板,对自己的名片很在乎,因为这是个门面。...

服务项目

  • 网站建设

    查看详情
  • 移动端/APP

    查看详情
  • 微信/小程序

    查看详情
  • 技术支持

    查看详情
  • 其它服务

    查看详情
  • 更多服务项目

    用我们的专业和诚信赢得您的信赖,从PC到移动互联网均有您想要的服务!

    获取更多

联系吧 在百度地图上找到我们

电话:13518219792

如遇占线或暂未接听请拨:136xxx98888

业务咨询 技术咨询 售后服务
网站制作
手机网站制作
温江网站制作
温江网站制作
广安网站制作公司
网站建设
三台网站建设
德阳网站建设
成都网站建设公司
高端品牌网站建设
网站设计
高端网站设计
成都网站设计
梓潼网站设计
企业网站设计
联系我们
电话:13518219792
邮箱:631063699@qq.com
地址:成都青羊区锦天国际1002号
网址:www.csdahua.cn

微信二维码

  • 友情链接
  • 四川网站建设
  • 成都广告招牌制作
  • 成华区商标专利
  • 四川IDC服务
  • 成都外贸网站建设公司
  • whjierui.cn
  • 400电话办理
  • EPS装饰条
  • 品牌官网设计
  • 雅安服务器托管

Copyright © 2002-2023 www.csdahua.cn 快上网建站品牌 QQ:244261566 版权所有 备案号:蜀ICP备19037934号

  • 在线咨询
  • 13518219792
  • 微信二维码

  • 移动版官网