1


我在使用 WSDL.exe 工具更新动态 Web 引用时遇到问题。

当我在 VS 中使用“更新 Web 参考”时,一切都按预期工作。下面是生成的代码(Reference.cs 文件的一部分):

    public MyService() {
        this.Url = global::ServerReference.Properties.Settings.Default.ServerReference_Reference_MyService;
        if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
            this.UseDefaultCredentials = true;
            this.useDefaultCredentialsSetExplicitly = false;
        }
        else {
            this.useDefaultCredentialsSetExplicitly = true;
        }
    }

我从应用程序属性中获取必要的信息,然后将这些信息存储在配置文件中,因此可以在不重建应用程序的情况下进行更改。

但是,当我使用以下命令时:

.\tools\wsdl.exe /l:cs /n:ServerReference /o".\ServerReference\Web References\Reference\Reference.cs" http://localhost:52956/MyService/MyService.asmx

它是在 Reference.cs 文件中使用固定 URL 地址创建的。

有人知道我应该如何更改命令以实现与Visual Studio 中相同的Reference.cs文件吗?

4

1 回答 1

1

我认为您不能使用 wsdl.exe 生成相同的代码。但是,如果您想要实现的主要目标是生成从 app.config 获取服务地址的代码,那么您可以使用带有“/appsettingurlkey”开关的 wsdl.exe。

您将获得的代码将是这样的:

public WebService1() {
    string urlSetting = System.Configuration.ConfigurationManager.AppSettings["ConfigKeyForServiceUrl"];
    if ((urlSetting != null)) {
        this.Url = urlSetting;
    }
    else {
        this.Url = "http://localhost:65304/WebService1.asmx";
    }
}

请注意,它通过 Settings 类从“appSettings”而不是“applicationSettings”中读取,因此您必须修改您的 app.config。而且它也不包含“UseDefaultCredentials”的东西。

于 2013-03-20T10:28:55.983 回答