C# 在运行时手动添加绑定
出于与我的具体情况相关的原因,我正在尝试从App.Config文件中删除尽可能多的内容。其中的一项是关于web服务的信息,我试图将其转换为代码。我从App.Config获取了信息,并创建了一个BasicHttpBinding类:C# 在运行时手动添加绑定,c#,.net,service,app-config,C#,.net,Service,App Config,出于与我的具体情况相关的原因,我正在尝试从App.Config文件中删除尽可能多的内容。其中的一项是关于web服务的信息,我试图将其转换为代码。我从App.Config获取了信息,并创建了一个BasicHttpBinding类: System.ServiceModel.BasicHttpBinding dss = new System.ServiceModel.BasicHttpBinding(); dss.Security.Mode = System.ServiceModel.
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();
BasicHttpBinding=new BasicHttpBinding();
EndpointAddress=新的EndpointAddress(“此处为您的uri”);
ChannelFactory工厂=新的ChannelFactory(绑定,地址);
IContract channel=factory.CreateChannel();
channel.YourMethod();
((ICommunicationObject)通道).Close();
这里有一个简单的方法,可以使用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();
BasicHttpBinding=new BasicHttpBinding();
EndpointAddress=新的EndpointAddress(“此处为您的uri”);
ChannelFactory工厂=新的ChannelFactory(绑定,地址);
IContract channel=factory.CreateChannel();
channel.YourMethod();
((ICommunicationObject)通道).Close();