通过命令行(wsdl工具)更新动态Web引用




使用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;
        }
    }

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

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

.toolswsdl.exe /l:cs /n:ServerReference /o".ServerReferenceWeb ReferencesReferenceReference.cs" http://localhost:52956/MyService/MyService.asmx

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

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

我认为您无法使用wsdl.exe生成相同的代码。但是,如果您想要实现的主要目标是生成从app.config获取服务地址的代码,那么您可以将wsdl.exe与"/appsettingurlkey"开关一起使用。

你会得到这样的代码:

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"内容。

最新更新