【问题标题】:How can I embed web service settings in a DLL?如何在 DLL 中嵌入 Web 服务设置?
【发布时间】:2010-10-13 21:51:07
【问题描述】:

我正在用 VB.NET 编写一个 DLL,它将被 ASP.NET 网站引用。

DLL 项目中包含对 Web 服务的引用。当我将 Web 服务添加到项目中时,会在“app.config”文件中添加大量配置信息。

为了让它在主机网站上运行,我必须将“app.config”文件中的信息复制到网站的“web.config”中。

有人知道如何将这些设置嵌入到我编译的 DLL 中吗?我不希望我的 DLL 的使用者必须将此设置块插入到他们的 web.config 文件中。

【问题讨论】:

    标签: .net web-services configuration


    【解决方案1】:

    实际上,复制和粘贴是解决方案。这就是 .NET 一直以来的工作方式。任何外部(库)代码的配置设置必须进入“执行”应用程序的配置文件。这可能是控制台或 Windows 服务应用程序的 application.exe.config 文件,或者是 ASP.NET、WCF 或其他 Web 的 web.config 文件之一基于应用程序。

    If app.config for a DLL should be in the “main config”… what do we do with WCF References in DLLs?

    总体思路是,需要设置的库代码可以被多个正在执行的应用程序使用。在这种情况下,每个应用程序的设置会有所不同

    如果您的设置在 DLL 中,那么您的 DLL 的使用者将无法编辑设置。

    如果您有不允许更改的“设置”,那么正如https://stackoverflow.com/a/686126/76337https://stackoverflow.com/a/655452/76337 所示,您应该在代码中进行配置,而不是在不可配置的配置文件中。

    【讨论】:

      【解决方案2】:

      有谁知道我如何嵌入 这些设置到我编译的 DLL 中?一世 不希望我的 DLL 的消费者 必须插入此设置块 进入他们的 web.config 文件

      是的,您可以在代码中设置地址。 在生成的代理类上,您有 Url 属性(用于旧式 asp.net Web 服务代理)或 Endpoint.Address 属性(用于 WCF 生成的 Web 服务代理)。

      //MyWebService is a non-WCF generated proxy
      MyWebService ws = new MyWebService();
      ws.Url = "http://whatever/";
      

      //MyWebServiceSoapClient is a WCF generated proxy
      MyWebServiceSoapClient svc = new MyWebServiceSoapClient();
      svc.Endpoint.Address = "http://whatever";
      

      不过,这可能意味着将这些值硬编码到您的 dll 中,我不建议这样做...

      【讨论】:

        【解决方案3】:

        “svc.Endpoint.Address”需要一个 System.ServiceModel.EndpointAddress 类型的对象,所以实际上需要的是...

        Dim address As New System.ServiceModel.EndpointAddress("http://Whatever")
        Dim binding As New System.ServiceModel.BasicHttpBinding()
        
        ' The binding object is already set up with the default values normally found in the
        ' <bindings> section of the App.Config, but can be overriden here, eg...
        binding.ReceiveTimeout = new System.TimeSpan(0,20,0)
        
        Dim ws As New MyWebServiceSoapClient(binding, address)
        

        【讨论】:

          猜你喜欢
          • 2023-03-10
          • 2010-10-24
          • 1970-01-01
          • 2016-01-15
          • 1970-01-01
          • 1970-01-01
          • 2012-05-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多