在运行时手动添加绑定
由于与我的具体情况相关的原因,我尝试从 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");
最终如何添加客户端端点地址和绑定?我是否必须打开渠道,或者是否有一个更容易实现的类来解决这个问题?
For reasons that pertain to my specific situation, I'm trying to remove as much as possible from a App.Config file. One of the items in there that I'm trying to move into code is information pertaining to a web service. I've taken the information from the App.Config and created a BasicHttpBinding class :
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;
After that, I created a Uri to point to the address of the web service:
Uri baseAddress = new Uri("http://localservice/dataservice.asmx");
How do I eventually add the client endpoint address and binding? Do I have to open up channels or is there an easier class to implement that takes care of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用 ChannelFactory 以编程方式执行此操作的简单方法。
Here is an easy way to do that programmatically using a ChannelFactory.