4

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

如果我执行以下操作(基于如何以编程方式修改 WCF app.config 端点地址设置?

        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。在没有定义端点的情况下,我无法找到删除原始端点或创建服务的方法。

从配置应用程序写入 app.config 不是一种选择 - 我需要以编程方式从单独的 XML 配置文件中提取配置的值....

有什么想法吗?

编辑:该服务作为 Windows 服务运行并公开一个 HTTP 端点,它没有作为托管在 IIS 中的 Web 服务运行 - 如果这完全改变了事情的话

4

4 回答 4

2

好吧,我没有丰富的 WCF 背景,但这行得通吗?

m_SvcHost = new ServiceHost(this);
m_SvcHost.Description.Endpoints.Clear(); // <-- added

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

m_SvcHost.Open();
于 2010-01-13T20:27:56.677 回答
2

贾斯汀,

这对你有帮助吗?此代码将允许您响应您在 CreateServiceHost() 方法中列出的任何地址。

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

    protected override void ApplyConfiguration()
    {
        base.ApplyConfiguration();
    }
}

public class CRSyncServiceFactory : ServiceHostFactory
{
    protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        Uri newServiceAddress = new Uri("http://someaddress.com/CRSyncService.svc");
        CRSyncServiceHost newHost = new CRSyncServiceHost(serviceType, newServiceAddress);
        return newHost;
    }
}

<%@ ServiceHost Language="C#" Debug="true" Service="CRSyncService" Factory="CRSyncServiceFactory" CodeBehind="CRSyncService.svc.cs" %>
于 2010-01-13T19:28:09.123 回答
1

通过结合 gWiz 和 Dylan 的答案,我想出了一种方法来做到这一点,尽管我没有进行足够彻底的测试,无法知道我是否通过这些更改破坏了任何其他功能。

基本上,我添加了这个类:

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)
    {

    }
}

这会跳过 ServiceHost“ApplyConfiguration”调用,并且(现在可能是不必要的,因为如果未加载配置,则应该没有端点)清除端点。然后我执行以下操作:

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 配置的端点

谢谢!

于 2010-01-13T20:44:08.250 回答
0

你根本不需要配置部分,我不相信 - 即你可以在代码中完成这一切。如果您将这些内容留在 .config 中,那么它将与您在代码中编写的内容一起使用。

如果你想要一个或另一个,我认为你必须删除一个或另一个。

于 2010-01-13T19:16:18.227 回答