【问题标题】:Configure service reference URL value from database从数据库配置服务参考 URL 值
【发布时间】:2018-02-04 08:33:11
【问题描述】:

我正在引用 WCF 服务,这是我在 Web.config 中的配置:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://tempserver/Service/v1/Service.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
        contract="UserService.IService" name="BasicHttpBinding_IService" />
    </client>
  </system.serviceModel>

我的问题是:有没有办法在数据库中配置端点地址? 我可以在数据库中存储“http://tempserver/Service/v1/Service.svc”吗?

【问题讨论】:

  • 可以存入数据库吗?当然。您需要从数据库中读取它,然后在您的代码中配置 URL,但可以这样做。
  • 当我执行以下操作时,我仍然会因为未在配置中指定而出错:String baseAddress = "URLfromDB"; BasicHttpBinding 绑定 = new BasicHttpBinding();使用 (ServiceHost host = new ServiceHost(typeof(ServiceClient))) { host.AddServiceEndpoint(typeof(OktaService), binding, baseAddress); }
  • 你不能在配置中指定它。需要从数据库中加载并在代码中配置。

标签: .net wcf configure endpoint service-reference


【解决方案1】:

在您编写了托管服务的代码的类中,您可以进行以下配置。

//Base Address for StudentService
        Uri httpBaseAddress = new Uri("http://localhost:0000/Service1");
        //Instantiate ServiceHost
        ServiceHost svcHost = new ServiceHost(typeof(Service1), httpBaseAddress);
        //Add Endpoint to Host
        svcHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "");
        //Open
        svcHost.Open();

如果 uri 在代码中硬编码,您可以从 DB 中提取此值。

【讨论】:

    猜你喜欢
    • 2013-04-13
    • 2019-06-21
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多