如何在DLL中嵌入Web服务设置?

时间:2009-03-17 17:52:22

标签: .net web-services configuration

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

DLL项目中引用了一个Web服务。当我将Web服务添加到项目中时,一大堆配置信息被添加到'app.config'文件中。

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

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

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

  

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

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

//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中硬编码这些值,我不建议......

答案 2 :(得分:0)

实际上,复制和粘贴是解决方案。这就是.NET一直有效的方式。任何外部(库)代码的配置设置必须进入“执行”应用程序的配置文件。这将是控制台或Windows服务应用程序的 应用程序 .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所示,您应该在代码中配置内容,而不是在不可配置的配置文件中。