由于与我的具体情况有关的原因,我试图尽可能地从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;
之后,我创建了一个指向Web服务地址的Uri:
Uri baseAddress = new Uri("http://localservice/dataservice.asmx");
如何最终添加客户端端点地址和绑定?我是否需要打开频道或者是否有更容易实施的课程来处理这个问题?
答案 0 :(得分:3)
这是使用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();