2

我希望我的 WCF 服务返回多部分/表单数据。尽管响应正文确实包含所需的内容,但是当我想查看 Content-type: multipart/form-data 时,响应标头表明正文是 application/octet-stream。

代码从以下单元测试开始。

    [TestMethod, Owner(TEST_AUTHOR), TestCategory("MIME Testing")]
    public void TempWcfService_PostMimeRequest_ExpectSuccess()
    {
        //Arrange
        HttpClient client = new HttpClient();

        HttpMultipartMimeForm form = new HttpMultipartMimeForm();
        form.Add("Key1", "Value1");
        form.Add("Key2", "Value2");

        //Act
        HttpResponseMessage response = client.Post(TEST_HOST_URL + "/mime-test", form.CreateHttpContent());
        response.EnsureStatusIsSuccessful();

        Stream responseStream = response.Content.ReadAsStream();
        string sResponse = responseStream.ToString();

        //Assert
        Console.Write(sResponse);
    }

上述测试使用了一个 WCF 服务,其界面如下所示。

namespace TempWcfService
{
    /// <summary>
    /// Interface added to support http without the WCF client class
    /// </summary>
    [ServiceContract]
    public interface ITempPing
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "ping")]
        [Description("Pings the service for availability")]
        string ping();

        [OperationContract]
        [WebInvoke(UriTemplate = "mime-test", 
                   Method = "POST",
                   BodyStyle = WebMessageBodyStyle.Bare)]
        [Description("Tests a simple MIME request")]
        Stream TestMultipartMime(Stream data);

    }
}

以及实施...

namespace TempWcfService
{
    /// <summary>
    /// Implementation added to support http without the WCF client class
    /// </summary>
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class TempPing : ITempPing
    {
        public string ping()
        {
            return "TempPing ping() successful";
        }

        public Stream TestMultipartMime(Stream data)
        {
            // Re-construct the multipart/form-data content
            MultipartFormData multipartFormData = new MultipartFormData(WebOperationContext.Current.IncomingRequest, data);
            HttpMultipartMimeForm responseForm = new HttpMultipartMimeForm(multipartFormData.Items);
            responseForm.Add("ErrorCode", "Success");
            responseForm.Add("ErrorMessage", "None");

            HttpResponseMessage responseMsg = new HttpResponseMessage();
            responseMsg.StatusCode = System.Net.HttpStatusCode.OK;
            responseMsg.Content = responseForm.CreateHttpContent();
            responseMsg.Headers.Add("Accept", "multipart/form-data");
            responseMsg.Headers.ContentType = "multipart/form-data; boundary=" + multipartFormData.Boundary;

            return responseMsg.Content.ReadAsStream();
        }

    }
}

WCF 似乎在“帮助我”,方法是根据使用 ReadAsStream() 返回的内容,使用“application/octet-stream”的内容类型覆盖代码中指定的 ContentType。有没有办法让 Content-Type 标头指示多部分/表单数据?

4

1 回答 1

1

您需要在传出上下文中设置内容类型:

public Stream TestMultipartMime(Stream data)
{
    // Re-construct the multipart/form-data content
    MultipartFormData multipartFormData = new MultipartFormData(WebOperationContext.Current.IncomingRequest, data);
    HttpMultipartMimeForm responseForm = new HttpMultipartMimeForm(multipartFormData.Items);
    responseForm.Add("ErrorCode", "Success");
    responseForm.Add("ErrorMessage", "None");

    HttpResponseMessage responseMsg = new HttpResponseMessage();
    responseMsg.StatusCode = System.Net.HttpStatusCode.OK;
    responseMsg.Content = responseForm.CreateHttpContent();

    WebOperationContext.Current.OutgoingResponse.ContentType = "multipart/form-data; boundary=" + multipartFormData.Boundary;
    WebOperationContext.Current.OutgoingResponse.Headers["Accept"] = "multipart/form-data";

    return responseMsg.Content.ReadAsStream();
}

请注意,您不需要Accept响应中设置标头- 此标头用于客户端请求告诉服务器它想要接收哪些内容类型。

于 2012-12-31T19:08:36.793 回答