3

我正在实现一个由 IIS 托管的 WCF 服务,它模拟调用者。当我在 Web.config 文件中有服务端点配置时,一切都按预期工作。

我想以编程方式设置服务端点,但我遗漏了一些东西,因为调用者没有被模拟(端点工作正常,除了那个小细节)。有什么方法可以捕获从 web.config 代码中创建的服务端点,以便在调试时我可以找到这个和我以编程方式创建的有什么区别?

谢谢,

基督教

4

1 回答 1

1

您可以使用默认服务主机工厂从代码中的 web.config 访问端点(并可能将调试器附加到 IIS 进程以查看它包含的内容)。

    public class MyServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new MyServiceHost(serviceType, baseAddresses);
        }
    }

    public class MyServiceHost : ServiceHost
    {
        public MyServiceHost(Type serviceType, Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        protected override void OnOpening()
        {
            // At this point you have access to the endpoint descriptions
            foreach (var endpoint in this.Description.Endpoints)
            {
                Console.WriteLine("Endpoint at {0}", endpoint.Address.Uri);
                Binding binding = endpoint.Binding;
                BindingElementCollection elements = binding.CreateBindingElements();
                foreach (var element in elements)
                {
                    Console.WriteLine("  {0}", element);
                }
            }

            base.OnOpening();
        }
    }

并在 .svc 文件中指定Factory="YourNamespace.MyServiceHostFactory"属性。

于 2012-10-25T05:51:39.240 回答