2010-06-18 105 views
0

因此,我有一個負責管理其他服務的中央Web服務。這些服務在主要的WS中註冊了他們的URL,導致他們自己的Web服務。使用URL調用Web服務方法

我現在需要做的是從中央Web服務調用子Web服務。我搜索了谷歌如何做到這一點,但我能找到的是this

我想註冊任何Web服務,而不是創建Web引用,正如在我找到的解決方案中所建議的。

如何在不使用Web引用的情況下完成此操作?

回答

0

艾爾卡,

如果您使用的web服務,比其他WCF,你可以改變你在web.config是要到web服務的URL,你也可以在代碼中通過在URL上改變這個你代理。

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

你也可以創建自己的Web服務代理,並從那裏處理Web服務的重定向。

+0

感謝您的快速回答。儘管我仍然對此感到困惑 - 我是否必須創建一個Web服務引用來執行您所說的內容? – Alka 2010-06-18 13:07:46

+0

你可以使用上面的解決方案,如果你看看web引用代碼,你會發現一個名爲reference.cs的文件,它是webservice使用的代碼,如果你想創建自己的web服務代理,它是一個很好的起點。 – Iain 2010-06-18 14:06:20

0

您可能在開發時添加一個Web引用(這將允許Visual Studio發現Web服務並具有可用的Intellisense)。

但是,在您的代碼中,您可以動態創建對象。

假設您需要使用名爲TestSoapClient的對象來訪問您的Web服務。如果你想從Web引用的URL創建它,你只是做

TestSoapClient testSoapClient = new TestSoapClient(); 

該代碼將使用默認的URL(即你指出,當你添加你的網站參考之一)。

如果要動態地創建TestSoapClient對象使用在運行時指定,走的是這樣一個URL:

 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); 

這樣的Web引用URL的值和值在配置文件將不會在運行時使用。

0

好的,問題解決了。

我對此的解決方案是使用Web引用並將代理URL更改爲我想要的服務。這樣我可以動態訪問我的Web服務。

感謝您的回答。