WCF SOAP服务无法处理消息,因为它发送多部分消息并期望'text / xml;字符集= UTF-8'

时间:2015-04-24 12:37:31

标签: c# wcf soap utf-8 multipart

我遇到WCF Soap 1.1配置问题。我用basicHttpBinding创建了一个WCF服务。 Bellow配置。该服务是WCF_SOAP_REST_SERVICE.BiasImpl:

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServBehave" name="WCF_SOAP_REST_SERVICE.Service">
        <endpoint address="soapService" binding="basicHttpBinding" contract="WCF_SOAP_REST_SERVICE.Service" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
      <service behaviorConfiguration="ServBehave" name="WCF_SOAP_REST_SERVICE.BiasImpl">
        <endpoint address="soapService" binding="basicHttpBinding" bindingConfiguration="basicHttp"
          contract="WCF_SOAP_REST_SERVICE.BiasImpl" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
     <bindings>
        <basicHttpBinding>
          <binding name="basicHttp" bypassProxyOnLocal="true" maxBufferSize="2147483647"
    maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
             useDefaultWebProxy="false" messageEncoding="Mtom" >
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
      maxArrayLength="2147483647" maxBytesPerRead="2147483647"
      maxNameTableCharCount="2147483647" />
          </binding>
        </basicHttpBinding>
     </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServBehave">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

我正在使用SoapUI测试服务。问题是,如果我发送小包,我可以测试服务。我可以测试它并使用Visual Studio进行调试,但是当我尝试发送更大的包时(例如,普通请求可能包含大约500kb,因为它将包含多个图像),请求使用多部分包发送,并且WCF结合配置为接受'text / xml;字符集= UTF-8' 。所以我在SoapUI中得到了这个错误

Fri Apr 24 08:16:31 EDT 2015:DEBUG:Receiving response: HTTP/1.1 415 Cannot process the message because the content type 'multipart/related; type="text/xml"; start="<rootpart@soapui.org>"; boundary="----=_Part_0_1561749008.1429877791373"' was not the expected type 'text/xml; charset=utf-8'.

这是由svcutil.exe生成的服务接口。 (wsdl是预定义的,无法更改)

[System.CodeDom.Compiler.GeneratedCode("System.ServiceModel", "4.0.0.0")]
[ServiceContract(Namespace="http://my.custom.url/codification/", ConfigurationName="Codification")]
public interface Codification{


.
.
.

    [OperationContract(Action="CheckQuality"/*, ReplyAction="*"*/)]
    [FaultContract(typeof(my.custom.url.CodificationFaultDetail), Action="CheckQuality", Name="CodificationFault")]
    [XmlSerializerFormat()]
    [ServiceKnownType(typeof(ResponseTemplate))]
    [ServiceKnownType(typeof(RequestTemplate))]
    CheckQualityResponse CheckQuality(CheckQuality request);

.
.
.

}

这是实施:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CodificationImpl : Codification
{
    .
    .
    .

    public CheckQualityResponse CheckQuality(CheckQuality request)
    {
        //implementation goes here
    }

    .
    .
    .
}

CheckQuality类:

[System.Diagnostics.DebuggerStepThrough()]
[System.CodeDom.Compiler.GeneratedCode("System.ServiceModel", "4.0.0.0")]
[MessageContract(WrapperName="CheckQuality", WrapperNamespace="http://my.custom.url/codification/", IsWrapped=true)]
public partial class CheckQuality
{

    [MessageBodyMember(Namespace="http://my.custom.url/codification/", Order=0)]
    [System.Xml.Serialization.XmlElement(IsNullable=true)]
    public CheckQualityCheckQualityRequest CheckQualityRequest;

    public CheckQuality()
    {
    }

    public CheckQuality(CheckQualityCheckQualityRequest CheckQualityRequest)
    {
        this.CheckQualityRequest = CheckQualityRequest;
    }
}

我必须在WCF服务绑定中配置什么才能接受多部分消息?

1 个答案:

答案 0 :(得分:0)

请找到以下适用于SOAPUI的WCF服务

服务

[ServiceContract]
    public interface IImageService
    {
        [OperationContract]
        bool CheckQualityOfImage(CheckQuality objCheckQuality);
    }


    [DataContract]
    public class CheckQuality
    {
        [DataMember]
        public MemoryStream ImageData { get; set; }
        [DataMember]
        public string ImageFileName{ get; set; }
    }

以下界面的实施:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class ImageService : IImageService
    {
        public bool CheckQualityOfImage(CheckQuality objCheckQuality)
        {
            if (objCheckQuality != null)
            {
                if (objCheckQuality.ImageData.Length > 0 && ! String.IsNullOrEmpty(objCheckQuality.ImageFileName))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
    }

服务器上的Web.config

<system.serviceModel>
    <bindings>
    <basicHttpBinding>
        <binding name="noSecurity" maxReceivedMessageSize="2147483647">
              <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                            maxDepth="64" maxNameTableCharCount="2147483647" />
              <security mode="None"></security>
            </binding>
    </basicHttpBinding>
    </bindings>
    <services>
          <service name="XMLService.ImageService" behaviorConfiguration="default">
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="noSecurity" contract="XMLService.IImageService" />
          </service>
    </services>
    <behaviors>
        <behavior name="default">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />      
        </behavior>
    </behaviors>
</system.serviceModel>

现在我的SOAPUI请求如附带的屏幕截图所示

Success

Empty request without file

确保匹配SOAPUI中的请求属性。可以找到有关将SOAPUI与附件一起使用的更多信息here

注意:我对CheckQuality类中的属性做了一些假设,并且我使用了6MB文件进行上传