【问题标题】:How to programatically set a single endpoint for a WCF service如何以编程方式为 WCF 服务设置单个终结点
【发布时间】:2011-01-04 19:03:01
【问题描述】:

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

如果我执行以下操作(基于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。在没有定义端点的情况下,我无法找到删除原始端点或创建服务的方法。

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

有什么想法吗?

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

【问题讨论】:

    标签: c# wcf endpoints


    【解决方案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 配置的端点

    谢谢!

    【讨论】:

      【解决方案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();
      

      【讨论】:

      • 它确实清除了端点,然后只添加了我想要的那个,但是即使端点集合中只列出了外部配置的端点,服务仍然会在两个端口上做出响应。
      【解决方案3】:

      贾斯汀,

      这对您有帮助吗?此代码将允许您响应您在 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" %>
      

      【讨论】:

      • 我最初并没有提到该服务是 Windows 服务,并且不在 IIS 中托管 - 我尝试这样做只是发现 CreateServiceHost 不适用于 Windows 服务
      【解决方案4】:

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

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

      【讨论】:

      • 我的问题是我希望服务仅响应通过配置应用程序配置的 IP/端口,而不是 app.config 中列出的 URI ...但我无法删除端点配置在 app.config 中,否则我无法实例化新的 ServiceHost 条目(因为当时没有配置端点)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 2021-08-09
      • 2012-02-08
      相关资源
      最近更新 更多