从app.config到代码的BasicHTTPBinding

时间:2015-11-24 09:11:20

标签: c# wcf

我坚持实现以前在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>

我开始以编程方式设置绑定,但由于没有在app.config中设置的_binding中的方法,因此速度非常快?!

<!-- 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);

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

如何在BasicHTTPBinding中访问这些设置?或者我的概念是否错误?

_binding.maxDepth // ... etc.

不是BasicHTTPBinding的成员。

1 个答案:

答案 0 :(得分: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文件的情况下工作:)