如何编程设置一个WCF服务的单个端点 [英] How to programatically set a single endpoint for a WCF service

查看:139
本文介绍了如何编程设置一个WCF服务的单个端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许WCF服务的用户配置,包括服务监听的IP地址和端口号。用户有一个单独的配置应用程序,允许将设置这些值,但我遇到的问题是,在app.config必须有,以创建一个新的ServiceHost的条目定义端点...但我的终点正在在一个单独的配置文件中定义,然后必须在运行时编程的约束。

I'm trying to allow for user configuration of a WCF service, including the IP and port number that the service listens on. The user has a separate config application that allows for these values to be set, but the problem I am running into is that the app.config MUST have an endpoint defined in order to create a new ServiceHost entry...but my endpoint is being defined in a separate configuration file and must then be bound programatically at runtime.

如果我这样做了以下(基于的如何以编程方式修改WCF app.config中的端点地址设置

If I do the following (based on How to programatically modify WCF app.config endpoint address setting?:

        m_SvcHost = new ServiceHost(this);

        if (Config.ServiceEndpoint != null && Config.ServiceEndpoint != String.Empty)
        {
            m_SvcHost.AddServiceEndpoint(typeof(IMyService),
                new BasicHttpBinding(),
                Config.ServiceEndpoint);
        }

        m_SvcHost.Open();



服务将侦听中的两者的app.config定义的URI,并且所述配置文件中定义的URI。没有。方式,我可以找到删除原来的端点或创建没有定义端点服务

the service will listen on BOTH the URI defined in the app.config, AND the URI defined in the configuration file. There is no way that I can find to remove the original endpoint or to create the service without an endpoint defined.

写作从配置应用程序在app.config是不是一种选择 - 我需要从单独的XML配置文件编程拉配置值....

Writing to the app.config from the configuration application is not an option - I need to pull the configured value programatically from the separate XML config file....

什么想法?

编辑:服务运行作为Windows服务,并公开HTTP端点,它不运行在IIS托管的Web服务 - 如果,在所有

the service runs as a Windows Service and exposes an HTTP endpoint, it is not running as a web service hosted in IIS - if that changes things at all

推荐答案

通过结合gWiz和迪伦的答案,我想出了一个办法做到这一点,虽然我没有测试不够彻底知道我是否已经打破这些变化的任何其他功能。

By combining both gWiz and Dylan's answers I came up with a way to do this, though I haven't tested thoroughly enough to know if I have broken any other functionality with these changes.

基本上,我加入这个类:

Basically, I added this class:

public class MobileMonitoringSvcHost : ServiceHost
{
    protected override void ApplyConfiguration()
    {
        // skip this line to not apply default config - unsure of other ramifications of doing this yet...
        base.ApplyConfiguration();

        base.Description.Endpoints.Clear();
    }

    public MobileMonitoringSvcHost(object singletonInstance, params Uri[] baseAddresses) : base(singletonInstance, baseAddresses)
    {

    }
}

这将跳过的ServiceHostApplyConfiguration呼叫,(可能是不必要的,因为现在如果配置ISN ŧ装应该没有终点)清除端点。然后,我做到以下几点:

This skips the ServiceHost "ApplyConfiguration" call and (likely needlessly for now because if the config isn't loaded there should be no endpoints) clears the endpoints. Then I do the following:

m_SvcHost = new MySvcHost(this);


        if (Config.ServiceEndpoint != null && Config.ServiceEndpoint != String.Empty)
        {
            //m_SvcHost.Description.Endpoints.Clear();


            m_SvcHost.AddServiceEndpoint(typeof(IMobileMonitoringSvc),
                new BasicHttpBinding(),
                Config.ServiceEndpoint);
        }


        // open the svchost and allow incoming connections
        m_SvcHost.Open();

这不会导致服务的外部配置的端点只能听,而不是在app.config配置的端点

This does cause the service to only listen on the externally configured endpoint and not the app.config configured endpoint

谢谢!

这篇关于如何编程设置一个WCF服务的单个端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆