在运行时更改web服务URL
本文关键字:服务 URL web 运行时 | 更新日期: 2023-09-27 18:04:11
我正在开发一个应用程序,它将根据用户的位置连接到各种Web服务。
例如:
如果用户在城市"A",应用程序将连接到Web服务:"192.168.1.1:8010",如果他去城市"B",将连接到"192.168.1.1:8020",在城市"C"将连接到"192.168.1.1:8030",以此类推。
IP地址始终不变,改变的是端口号
很简单吧?是的,但是我找不到让它在运行时工作的方法!
当我在运行时更改Web服务("URL行为==动态")的"URL"参数(myWS.Url)时,服务器只返回null,将参数更改为原始值,通信恢复。
同样,如果在编译前做了更改,则编译到城市"A",仅在城市"A"中有效,编译到城市"B",仅在城市"B"中有效。
该应用程序最初是使用VS2008和CF3.5为Windows CE 5.0开发的,但即使在VS2013中使用。. NET Framework 4.0和Windows 7的"问题"仍然发生。
有人经历过吗?
按照要求,这里是我的一小部分代码:
WSR.WSR myWS = new WSR.WSR();
//WSR added as a Web Reference
if (City_A)
{
myWS.Url = "http://192.168.1.1:8010/WSR.apw";
}
else
{
myWS.Url = "http://192.168.1.1:8020/WSR.apw";
}
本主题的解决方案(应该修改什么来改变c#中web服务的URL ?)不适合我,因为,正如我所说,当我改变"URL行为"属性时,没有在我的app.config文件中创建入口点。
下面是app.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="TrocaWebService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
<system.serviceModel>
<bindings />
<client />
</system.serviceModel>
<userSettings>
<TrocaWebService.Properties.Settings>
<setting name="TrocaWebService_WSR_WSR" serializeAs="String">
<value>http://192.168.1.1:8010/WSR.apw</value>
</setting>
</TrocaWebService.Properties.Settings>
</userSettings>
</configuration>
我正在使用WCF绑定(Visual Studio -> Add Service Reference)来针对WSDL创建代理类。然后在运行时,我将使用以下代码创建一个ClientBase
(它有一个端点(即url)):
public static class ServiceClientFactory
{
public static HttpBindingBase BuildBinding(string endpointUrl)
{
if (endpointUrl.ToLower().StartsWith("https"))
{
var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
binding.MaxReceivedMessageSize = int.MaxValue - 1;
return binding;
}
else
{
var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
binding.MaxReceivedMessageSize = int.MaxValue - 1;
return binding;
}
}
public static TClient CreateClient<TClient, TChannel>(string endpoint, string username, string password)
where TClient : ClientBase<TChannel>
where TChannel : class
{
var client = (TClient)
Activator.CreateInstance(
typeof (TClient),
BuildBinding(endpoint),
new EndpointAddress(endpoint));
if (null == client.ClientCredentials)
throw new Exception(
string.Format("Error initializing [{0}] client. Client Credentials object was null",
typeof(TClient).Name));
//This is for setting Basic Auth
client.ClientCredentials.Windows.ClientCredential =
new NetworkCredential(
username,
password);
client.ClientCredentials.Windows.AllowedImpersonationLevel =
TokenImpersonationLevel.Delegation;
return client;
}
}
}