在运行时手动添加绑定

发布于 2024-10-30 19:58:25 字数 1939 浏览 1 评论 0原文

由于与我的具体情况相关的原因,我尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红ご颜醉 2024-11-06 19:58:25

这是使用 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();

Here is an easy way to do that programmatically using a 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文