How can I dynamically switch web service addresses in .NET without a recompile?

后端 未结 11 957
小蘑菇
小蘑菇 2020-11-28 09:11

I have code that references a web service, and I\'d like the address of that web service to be dynamic (read from a database, config file, etc.) so that it is easily changed

11条回答
  •  失恋的感觉
    2020-11-28 09:50

    I know this is an old question, but our solution is much simpler than what I see here. We use it for WCF calls with VS2010 and up. The string url can come from app settings or another source. In my case it is a drop down list where the user picks the server. TheService was configured through VS add service reference.

    private void CallTheService( string url )
    {
       TheService.TheServiceClient client = new TheService.TheServiceClient();
       client.Endpoint.Address = new System.ServiceModel.EndpointAddress(url);
       var results = client.AMethodFromTheService();
    }
    

提交回复
热议问题