简体   繁体   中英

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. 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 ). Are there any other solutions?

One possible solution is using DynWsib - HERE . Please note this does not work with WCF.

You can then invoke at runtime. Binaries are created and cached for each 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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