简体   繁体   English

ASP.net-从共享同一接口的不同URL /主机连接到多个Web服务?

[英]ASP.net - connect to multiple Web Services from different URL's/host that share the same interface?

I'd like my ASP.NET application to be able to talk to any number of different hosts, all providing a Web Service that has exactly the same interface, but on different domains/ASMX URL's. 我希望我的ASP.NET应用程序能够与任意数量的不同主机通信,所有主机都提供具有完全相同的接口,但在不同域/ ASMX URL上的Web服务。 I've found here a solution that allows me to generate a class for one web service, but the URL address/prefix/namespaces are hardcoded in the method attributes and I don't know how to change them (related question here ). 我在这里找到一种解决方案,可以让我为一个Web服务生成一个类,但是URL地址/前缀/命名空间是硬编码在方法属性中的,并且我不知道如何更改它们( 此处是相关问题)。 Are there any other solutions? 还有其他解决方案吗?

One possible solution is using DynWsib - HERE . 一种可能的解决方案是使用DynWsib- HERE Please note this does not work with WCF. 请注意,这不适用于WCF。

You can then invoke at runtime. 然后可以在运行时调用。 Binaries are created and cached for each url. 为每个URL创建并缓存二进制文件。 Function below is basic idea. 下面的功能是基本思想。 Change as needed. 根据需要进行更改。

public object InvokeWebserviceCall(string wsdUrl, string actionUrl, string functionName,
        string domain, string username, string password, params object[] parameters)
    {
        ///todo: validate input

        var proxy = new DynamicWebServiceProxy();

        //credentials if needed
        if (!string.IsNullOrEmpty(domain))
        {
            proxy.Credentials = new NetworkCredential(username, password, domain);
        }
        else if (!string.IsNullOrEmpty(username))
        {
            proxy.Credentials = new NetworkCredential(username, password);
        }

        proxy.EnableMessageAccess = true;
        proxy.Wsdl = wsdUrl;

        //get type name
        var type = proxy.ProxyAssembly.GetTypes().SingleOrDefault(t => t.BaseType == typeof(SoapHttpClientProtocolExtended));

        if (type != null)
        {
            proxy.TypeName = type.Name;
        }

        proxy.MethodName = functionName;
        proxy.Url = new Uri(actionUrl);

        if (parameters != null)
        {
            parameters.ToList().ForEach(proxy.AddParameter);
        }

        object result = proxy.InvokeCall();

        return result;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM