在运行时更改Web服务URL

时间:2014-05-20 17:41:13

标签: c# web-services asmx

我正在开发一个应用程序,它将根据用户的位置连接到各种Web服务。

例如:

如果用户在城市" A",应用程序将连接到Web服务:" 192.168.1.1:8010",如果他去了城市" ; B",将连接到" 192.168.1.1:8020",在城市" C"将是" 192.168.1.1:8030"等等。

IP地址始终相同,端口号会发生变化。

简单的事情吧?是的,但我无法找到一种方法让它在运行时工作!

当我改变" URL" Web服务的参数(myWS.Url)(" URL Behaivor == Dynamic")在运行时,服务器只返回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";
}

本主题中的解决方案(What should be modified to change the URL of a web service in C#?)对我不起作用,因为正如我所说,当我更改" 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>

1 个答案:

答案 0 :(得分:0)

我正在使用WCF绑定(Visual Studio - &gt; 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;
    }
}

}