WCF不支持的媒体类型

时间:2012-08-29 11:56:18

标签: c# wcf web-services

我有一个WCF网络服务来接收文件,但是当我进行一个帖子调用总是返回我当我尝试发送文件时,未报告的媒体类型multipart-form-data,如果我不发送文件并将内容类型设置为xml,服务获取我没关系并创建文件pruebffa.jpg罚款 这是我的web.config:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="streamBinding" transferMode="Streamed" messageEncoding="Mtom" maxReceivedMessageSize="615424521" openTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00"/>
        </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
    <services>
        <service name="WcfService3.Service1" behaviorConfiguration="WcfService3.Service1Behavior">
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="streamBinding" contract="WcfService3.IService1">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="WcfService3.Service1Behavior">
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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>
</system.serviceModel>

代码:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(BodyStyle=WebMessageBodyStyle.Bare, UriTemplate="")] 
    void UploadImage(Stream image);
}

public class Service1 : IService1
{
    public void UploadImage(Stream image)
    {
        var buf = new byte[1024];
        var path = Path.Combine(@"C:\", "pruebffa.jpg");
        int len = 0;
        using (var fs = File.Create(path))
        {
            while ((len = image.Read(buf, 0, buf.Length)) > 0)
            {
                fs.Write(buf, 0, len);
            }
        }
    }
}

如何启用接受multipart / form-data?

1 个答案:

答案 0 :(得分:0)

由于这是POST操作,您应该使用WebInvoke而不是WebGet

[OperationContract]     
[WebInvoke(..)]      
void UploadImage(Stream image);