【问题标题】:BasicHTTPBinding from app.config to code从 app.config 到代码的 BasicHTTPBinding
【发布时间】:2023-03-21 16:05:01
【问题描述】:

我坚持实现以前在 app.config 中定义的 BasicHTTPBinding(在类库中不存在)。我希望这些设置是预定义的,因此可以在我的 .dll 中进行硬编码。

这是我的 app.config 所说的:

<!-- language: xaml -->
<bindings>
  <basicHttpBinding>
    <binding name="ImageServerServiceSoapBinding" messageEncoding="Mtom" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
        <message clientCredentialType="UserName" algorithmSuite="Default"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://xx.xx.xx.xx:xx/xyz" binding="basicHttpBinding" bindingConfiguration="ImageServerServiceSoapBinding" contract="xyz.ImageServer" name="ImageServerPort"/>
</client>

我开始以编程方式设置绑定,但由于我的 _binding 中没有在 app.config 中设置的方法而卡住了?!

<!-- language: c# -->
System.ServiceModel.Channels.Binding _binding = new System.ServiceModel.BasicHttpBinding();
System.ServiceModel.EndpointAddress _address = new System.ServiceModel.EndpointAddress(@"http://xx.xx.xx.xx:xxx/xyz");

_binding.Name = "ImageServerServiceSoapBinding";
_binding.CloseTimeout = new TimeSpan(0, 1, 0);
_binding.OpenTimeout = new TimeSpan(0, 1, 0);
_binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
_binding.SendTimeout = new TimeSpan(0, 1, 0);

this._Client = new ImageServerClient(_binding, _address);

_Settings.TraverseData.TraverseWidth = double.Parse(_Client.getCameraInfo(d2els_selectedcam).traverseLength);

由于从绑定到客户端 (text/xml; charset=utf-8) 的内容类型 (multipart/related;) 不兼容,最后一行失败?!

如何访问 BasicHTTPBinding 中的这些设置?还是我有错误的概念?

_binding.maxDepth // ... etc.

不是 BasicHTTPBinding 的成员。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    对于所有面临同样问题的人来说:

    谷歌花了很长时间才在框架中找到在 app.config 中简单命名的基类。上面的 app.config 代码如下所示:

            System.ServiceModel.BasicHttpBinding _binding = new System.ServiceModel.BasicHttpBinding();
            System.ServiceModel.EndpointAddress _address = new System.ServiceModel.EndpointAddress(@"http://xx.xx.xx.xx:xxx/xyz");
    
            _binding.Name = "ImageServerServiceSoapBinding";
            _binding.CloseTimeout = new TimeSpan(0, 1, 0);
            _binding.OpenTimeout = new TimeSpan(0, 1, 0);
            _binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            _binding.SendTimeout = new TimeSpan(0, 1, 0);
    
            _binding.MessageEncoding = System.ServiceModel.WSMessageEncoding.Mtom;
            _binding.AllowCookies = false;
            _binding.BypassProxyOnLocal = false;
            _binding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard;
            _binding.MaxBufferSize = Int32.MaxValue;
            _binding.MaxBufferPoolSize = Int16.MaxValue;
            _binding.MaxReceivedMessageSize = Int32.MaxValue;
            _binding.TextEncoding = System.Text.Encoding.UTF8;
            _binding.TransferMode = System.ServiceModel.TransferMode.Buffered;
            _binding.UseDefaultWebProxy = true;
    
            System.Xml.XmlDictionaryReaderQuotas _readerquotas = new System.Xml.XmlDictionaryReaderQuotas();
    
            _readerquotas.MaxDepth = Int32.MaxValue;
            _readerquotas.MaxStringContentLength = Int32.MaxValue;
            _readerquotas.MaxArrayLength = Int32.MaxValue;
            _readerquotas.MaxBytesPerRead = Int32.MaxValue;
            _readerquotas.MaxNameTableCharCount = Int32.MaxValue;
    
            _binding.ReaderQuotas = _readerquotas;
    
            _binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.None;
            _binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
            _binding.Security.Transport.ProxyCredentialType = System.ServiceModel.HttpProxyCredentialType.None;
            _binding.Security.Transport.Realm = "";
    
            _binding.Security.Message.ClientCredentialType = System.ServiceModel.BasicHttpMessageCredentialType.UserName;
            _binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
    
            this._Client = new ImageServerClient(_binding, _address);
    

    我的问题是我认为 ImageServerClient 函数需要一个

    System.ServiceModel.Channels.Binding
    

    但也接受了一个

    System.ServiceModel.BasicHttpBinding. 
    

    后者包含所有预期的方法;现在无需 app.config 文件即可连接到服务器 :)

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2021-07-08
      • 2010-12-23
      • 2012-02-16
      • 1970-01-01
      相关资源
      最近更新 更多