C# 在运行时更改web服务URL

C# 在运行时更改web服务URL,c#,web-services,asmx,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地址总是相同的,更改的是端口号 很简单吧?是的,但我找不到一种方法让它在运行时工作 当我在运行时更改Web服务的“URL”参数(myWS.URL)(“URL Behaivor==Dynamic”)时

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

例如:

如果用户位于城市“A”,应用程序将连接到Web服务:“192.168.1.1:8010”,如果用户位于城市“B”,将连接到“192.168.1.1:8020”,城市“C”将连接到“192.168.1.1:8030”,依此类推

IP地址总是相同的,更改的是端口号

很简单吧?是的,但我找不到一种方法让它在运行时工作

当我在运行时更改Web服务的“URL”参数(myWS.URL)(“URL Behaivor==Dynamic”)时,服务器只返回null,将参数更改为其原始值,通信就会恢复

同样的事情,如果在编译之前进行了更改,编译到城市“A”,仅在城市“A”中有效,编译到城市“B”,仅在城市“B”中有效

该应用程序最初是使用VS2008和CF3.5为Windows CE 5.0开发的,但在VS2013中也使用了。NETFramework4.0和Windows7“问题”仍然存在

有人经历过吗

根据要求,以下是我代码的一小部分:

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";
}
本主题()中的解决方案不适用于我,因为正如我所说,当我更改“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>

http://192.168.1.1:8010/WSR.apw
我正在使用WCF绑定(Visual Studio->添加服务引用)根据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;
    }
}
公共静态类ServiceClientFactory
{
公共静态HttpBindingBase构建绑定(字符串端点URL)
{
if(endpointUrl.ToLower().StartsWith(“https”))
{
var binding=新的BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType=HttpClientCredentialType.Ntlm;
binding.MaxReceivedMessageSize=int.MaxValue-1;
返回绑定;
}
其他的
{
var binding=新的BasicHttpBinding(仅BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType=HttpClientCredentialType.Ntlm;
binding.MaxReceivedMessageSize=int.MaxValue-1;
返回绑定;
}
}
公共静态TClient CreateClient(字符串端点、字符串用户名、字符串密码)
其中TClient:ClientBase
TChannel:class在哪里
{
var客户端=(TClient)
Activator.CreateInstance(
类型(TClient),
构建绑定(端点),
新端点地址(端点));
if(null==client.ClientCredentials)
抛出新异常(
Format(“初始化[{0}]客户端时出错。客户端凭据对象为空”,
typeof(TClient.Name));
//这是用于设置基本身份验证的
client.ClientCredentials.Windows.ClientCredential=
新网络凭据(
用户名,
密码);
client.ClientCredentials.Windows.AllowedImpersonationLevel=
TokenImpersonationLevel.委派;
返回客户;
}
}

}

您是否正试图将点击端口80的用户重定向到其他端口?端口是否动态?是否有一些代码可以帮助您找到问题。ASMX是一种遗留技术,不应用于新的开发。WCF或ASP.NET Web API应用于Web服务客户端和服务器的所有新开发。一个提示:微软已经退出了MSDN。我放弃了让它以这种方式工作,我更改了webservices代码,将所有的城市作为一个参数传递给用户。这不是最佳实践,但它是有效的。谢谢大家。