开发者

Calling web service methods using URLs

So, i have a central web service that is responsible for managing other services. These services register in the main 开发者_如何学CWS with their URL, leading to their own web service.

what i need to do now is call the child web services from the central web service. I've searched google on how to do this but all i could find was this.

I would like to register any web service and not create a web reference, as suggested in the solution i've found.

How is this done without using a web reference?


Alka,

if you are using webservices, other than WCF, you can change the URL of the webservice you are going to in the web.config, you can also change this in code via the URL on your proxy.

eg

 var testuri = "http://a_web_server/PostCode1/PostCodeWebService.asmx";
 proxy.Url = testuri;

You could also create your own webservice proxy and handle the webservice redirection from there.


You probably could add a web reference when developing (that will allow visual studio to discover the web service and have a working Intellisense).

In your code however, you can create the object dynamically.

Say you need to use an object called TestSoapClient to access your web service. If you want to create it with the URL from the web reference, you would just do

TestSoapClient testSoapClient = new TestSoapClient();

That code would use the default URL (i.e. the one that you pointed to when you added your web reference).

If you want to create the TestSoapClient object dynamically using a URL that you specify at runtime, go with something like this :

        XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
        readerQuotas.MaxDepth = 32;
        readerQuotas.MaxStringContentLength = 8192;
        readerQuotas.MaxArrayLength = 16384;
        readerQuotas.MaxBytesPerRead = 4096;
        readerQuotas.MaxNameTableCharCount = 16384;

        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.Name = BindingName;
        basicHttpBinding.CloseTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.OpenTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
        basicHttpBinding.SendTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.AllowCookies = false;
        basicHttpBinding.BypassProxyOnLocal = false;
        basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        basicHttpBinding.MaxBufferSize = 65536;
        basicHttpBinding.MaxBufferPoolSize = 524288;
        basicHttpBinding.MaxReceivedMessageSize = 65536;
        basicHttpBinding.MessageEncoding = WSMessageEncoding.Text;
        basicHttpBinding.TextEncoding = Encoding.UTF8;
        basicHttpBinding.TransferMode = TransferMode.Buffered;
        basicHttpBinding.UseDefaultWebProxy = true;
        basicHttpBinding.ReaderQuotas = readerQuotas;
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
        basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

        EndpointAddress endpointAddress = new EndpointAddress("YourDynamicUrl");

        TestSoapClient testSoapClient = new TestSoapClient(basicHttpBinding, endpointAddress);

That way the value of the web reference URL and the values in the config file won't be used at runtime.


Ok, problem solved.

My solution to this was using a web reference and change the proxy URL to the service i want. This way i can dynamically access my web services.

Thanks for your answers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜