[英]Manually add binding at run-time
由於與我的具體情況有關的原因,我試圖從App.Config文件中刪除盡可能多的內容。 我要嘗試進入代碼的項目之一是與Web服務有關的信息。 我已經從App.Config中獲取了信息,並創建了BasicHttpBinding類:
System.ServiceModel.BasicHttpBinding dss = new System.ServiceModel.BasicHttpBinding();
dss.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.None;
dss.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
dss.Security.Transport.ProxyCredentialType = System.ServiceModel.HttpProxyCredentialType.None;
dss.Security.Transport.Realm = "";
dss.Security.Message.ClientCredentialType = System.ServiceModel.BasicHttpMessageCredentialType.UserName;
dss.Name = "DataServiceSoap";
dss.CloseTimeout = System.TimeSpan.Parse("00:01:00");
dss.OpenTimeout = System.TimeSpan.Parse("00:01:00");
dss.ReceiveTimeout = System.TimeSpan.Parse("00:10:00");
dss.SendTimeout = System.TimeSpan.Parse("00:10:00");
dss.AllowCookies = false;
dss.BypassProxyOnLocal = false;
dss.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard;
dss.MaxBufferSize = 655360;
dss.MaxBufferPoolSize = 524288;
dss.MaxReceivedMessageSize = 655360;
dss.MessageEncoding = System.ServiceModel.WSMessageEncoding.Text;
dss.TextEncoding = new System.Text.UTF8Encoding();
dss.TransferMode = System.ServiceModel.TransferMode.Buffered;
dss.UseDefaultWebProxy = true;
dss.ReaderQuotas.MaxDepth = 32;
dss.ReaderQuotas.MaxStringContentLength = 8192;
dss.ReaderQuotas.MaxArrayLength = 16384;
dss.ReaderQuotas.MaxBytesPerRead = 4096;
dss.ReaderQuotas.MaxNameTableCharCount = 16384;
之后,我創建了一個Uri來指向Web服務的地址:
Uri baseAddress = new Uri("http://localservice/dataservice.asmx");
最終如何添加客戶端端點地址和綁定? 我是否必須開放渠道,或者是否有一個易於實現的課程來解決這個問題?
這是使用ChannelFactory進行編程的一種簡單方法。
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("Your uri here");
ChannelFactory<IContract> factory = new ChannelFactory<IContract>(binding, address);
IContract channel = factory.CreateChannel();
channel.YourMethod();
((ICommunicationObject)channel).Close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.