更改服务url

本文关键字:url 服务 | 更新日期: 2023-09-27 18:27:54

在我的web应用程序中,我有一个服务引用(而不是web服务引用)。

[DebuggerStepThrough]
[GeneratedCode("System.ServiceModel", "4.0.0.0")]
public partial class LoginClient : ClientBase<Login.LoginClient>, Login.LoginSvc
{            
    public LoginClient() {
        EndpointAddress address = new EndpointAddress(ConfigurationManager.AppSettings["login_svc"]);
        this.Endpoint.Binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        this.Endpoint.Address = address; 
    }
}

基本上,我在AppSettings中有"login_svc"。然而,它抛出了一个流动的异常:

我不想将服务配置添加到web.config system.servicemodel…。相反,我只想使用url的appsettings。我该怎么做?

更改服务url

与其在构造函数中尝试这样做,不如在客户端应用程序中实例化代理时只使用一个已经可用的重载构造函数。例如,

        MyService.Service1Client proxy = new MyService.Service1Client(
            new BasicHttpBinding(), 
            new EndpointAddress("<YOUR ENDPOINT ADDRESS>"));

此外,不建议像这样编辑自动生成的代码,因为如果更新服务引用,那么这些更改将丢失,因为此时会重新生成reference.cs文件。当然,您可以复制Reference.cs,并将其作为项目中的一个文件进行管理。目前还不清楚你是否在这么做,只是想提一下以防万一。