让WCF只需要配置服务地址

本文介绍了一个使用 C# 实现的 WCF 服务宿主程序,该程序能够加载指定的服务和契约组件,并为每个服务端点提供基本的 HTTP 绑定。文章详细展示了如何动态创建并启动服务宿主,包括处理服务元数据行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

namespace NHG.BFP.Framework.Host
{
    internal class FramworkHostService
    {
        private System.Reflection.Assembly assmbly = null;
        private string serviceAddress = string.Empty;


        public void HostStartServer(string serviceAddress)
        {
            this.serviceAddress = serviceAddress;
            string strService = "NHG.BFP.Framework.Service";
            string strContract = "NHG.BFP.Framework.Contract";
            
            if (string.IsNullOrEmpty(strService) || string.IsNullOrEmpty(strContract))
            {
                NHG.BFP.Common.Log.Logger.PrintText("配置文件错误");
                Console.ReadKey();
            }


            NHG.BFP.Common.Log.Logger.PrintText("开始启动基础框架服务");
            assmbly = System.Reflection.Assembly.Load(strService);
            System.Reflection.Assembly asm = System.Reflection.Assembly.Load(strContract);
            if (assmbly != null && asm != null)
            {
                Type[] tContracts = asm.GetTypes();
                if (tContracts != null && tContracts.Length > 0)
                {
                    foreach (Type item in tContracts)
                    {
                        Type tService = GetTypeByFace(item) as Type;
                        if (tService == null) continue;


                        Uri httpBaseAddress = new Uri(string.Format("{0}/{1}", serviceAddress, item.Name));
                        ServiceHost serviceHost = new ServiceHost(tService, httpBaseAddress);
                        {
                            BasicHttpBinding httpBinding = new BasicHttpBinding();
                            serviceHost.AddServiceEndpoint(item, httpBinding, string.Empty);
                            ServiceMetadataBehavior areaBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                            {
                                if (areaBehavior == null)
                                {
                                    areaBehavior = new ServiceMetadataBehavior();
                                    areaBehavior.HttpGetEnabled = true;
                                    serviceHost.Description.Behaviors.Add(areaBehavior);
                                }
                                else
                                {
                                    areaBehavior.HttpGetEnabled = true;
                                }
                            }


                            serviceHost.Opened += delegate
                            {
                                NHG.BFP.Common.Log.Logger.PrintText(tService.Name + " 服务启动成功");
                            };


                            serviceHost.Open();
                        }
                    }
                    NHG.BFP.Common.Log.Logger.PrintText("基础框架服务启动完成", true);
                }
            }
        }


        private object GetTypeByFace(Type interfaceType)
        {
            object obj = null;
            if (assmbly != null)
            {
                Type[] tServices = assmbly.GetTypes();
                if (tServices != null && tServices.Length > 0)
                {
                    foreach (var type in tServices)
                    {
                        bool bExist = false;
                        foreach (var item in type.GetInterfaces())
                        {
                            if (item == interfaceType)
                            {
                                obj = type;
                                bExist = true;
                                break;
                            }
                        }
                        if (bExist) break;
                    }
                }
            }


            return obj;
        }


    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值