繁体   English   中英

在运行时手动添加绑定

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM