因此,我有一个负责管理其他服务的中央Web服务。这些服务在主要的WS中注册了他们的URL,导致他们自己的Web服务。使用URL调用Web服务方法
我现在需要做的是从中央Web服务调用子Web服务。我搜索了谷歌如何做到这一点,但我能找到的是this。
我想注册任何Web服务,而不是创建Web引用,正如在我找到的解决方案中所建议的。
如何在不使用Web引用的情况下完成此操作?
因此,我有一个负责管理其他服务的中央Web服务。这些服务在主要的WS中注册了他们的URL,导致他们自己的Web服务。使用URL调用Web服务方法
我现在需要做的是从中央Web服务调用子Web服务。我搜索了谷歌如何做到这一点,但我能找到的是this。
我想注册任何Web服务,而不是创建Web引用,正如在我找到的解决方案中所建议的。
如何在不使用Web引用的情况下完成此操作?
艾尔卡,
如果您使用的web服务,比其他WCF,你可以改变你在web.config是要到web服务的URL,你也可以在代码中通过在URL上改变这个你代理。
如
var testuri = "http://a_web_server/PostCode1/PostCodeWebService.asmx";
proxy.Url = testuri;
你也可以创建自己的Web服务代理,并从那里处理Web服务的重定向。
您可能在开发时添加一个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的值和值在配置文件将不会在运行时使用。
好的,问题解决了。
我对此的解决方案是使用Web引用并将代理URL更改为我想要的服务。这样我可以动态访问我的Web服务。
感谢您的回答。
感谢您的快速回答。尽管我仍然对此感到困惑 - 我是否必须创建一个Web服务引用来执行您所说的内容? – Alka 2010-06-18 13:07:46
你可以使用上面的解决方案,如果你看看web引用代码,你会发现一个名为reference.cs的文件,它是webservice使用的代码,如果你想创建自己的web服务代理,它是一个很好的起点。 – Iain 2010-06-18 14:06:20