2

I am trying to build a WCF service that will allow my WPF desktop clients to upload files to a server.

I adapted a code sample from The Code Project (WCF Streaming: Upload/Download Files Over HTTP) and I've looked at several SO posts as well, but can't seem to get this working.

When I execute the code, it fails with a null reference exception at the point that the server tries to read the stream that has been passed through the interface.

At this point, I am rather lost and don't know how to fix this up. Any suggestions are appreciated.

Code samples follow:

CustomerDocumentModel is the data element that I pass through the WCF interface with the stream to read the client side file:

[DataContract]
[KnownType(typeof(System.IO.FileStream))]
public class CustomerDocumentModel : IDisposable
{
    public CustomerDocumentModel()
    {
    }

    public CustomerDocumentModel(string documentName, string path)
    {
        DocumentName = documentName;
        Path = path;
    }

    [DataMember]
    public string DocumentName;

    [DataMember]
    public string Path;

    [DataMember]
    public System.IO.Stream FileByteStream;

    public void Dispose()
    { 
        if (FileByteStream != null)
        {
            FileByteStream.Close();
            FileByteStream = null;
        }
    }
}

IBillingService is the interface definition for my WCF service:

[ServiceContract]
public interface IBillingService
{
    // other methods redacted...

    [OperationContract]
    void UploadCustomerDocument(CustomerDocumentModel model);
}

The class BillingService implements the WCF service:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class BillingService : IBillingService
{
    // Other methods redacted ...

    public void UploadCustomerDocument(CustomerDocumentModel model)
    {
        string path = HttpContext.Current.Server.MapPath(
            String.Format("/Documents/{1}",
                model.DocumentName));

        using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            const int bufferSize = 4096;
            byte[] buffer = new byte[bufferSize];

            int size = 0;
            try
            {
                // The following Read() fails with a NullReferenceException
                while ((size = model.FileByteStream.Read(buffer, 0, bufferSize)) > 0)
                {
                    stream.Write(buffer, 0, size);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
            stream.Close();
            model.FileByteStream.Close();
            }
        }
    }
}

A few relevant bits from the web.config on my WCF web server:

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="2097151" useFullyQualifiedRedirectUrl="true" executionTimeout="360"/>
</system.web>

<system.serviceModel>
    <serviceHostingEnvironment
        aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" />
    <bindings>
        <basicHttpBinding>
            <binding name="userHttps" transferMode="Streamed" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
                <serviceDebug includeExceptionDetailInFaults="true" />
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

The client is a WPF/MVVM app that creates a CustomerDocumentModel model, uses an OpenFileDialog to Open() the file stream and then passes the model to the UploadCustomerDocument method on WCF Service.

If I am missing any relevant details, please ask.

4

2 回答 2

4

我知道这个对您的问题的回复很晚,我相信您也一定已经解决了您的问题。这可能对其他人有帮助:-)

使用 Messagecontract 而不是 Datacontract,并且只有一个具有数据类型 Stream 的 MessageBodyMember,其余所有参数都是 MessageHeader。这是示例:

[MessageContract]

    public class CustomerDocumentModel : IDisposable
    {

        public CustomerDocumentModel(string documentName, string path)
        {
            DocumentName = documentName;
            Path = path;
        }

        [MessageHeader]
        public string DocumentName{get;set;}

        [MessageHeader]
        public string Path{get;set;}

        [MessageBodyMember]
        public System.IO.Stream FileByteStream{get;set;}

        public void Dispose()
        { 
            if (FileByteStream != null)
            {
                FileByteStream.Close();
                FileByteStream = null;
            }
        }
    }

注意:确保您的配置传输模式为 StreamedResponse,您可能还希望将 MessageEncoding 更改为 MTOM 以获得更好的性能。

public void UploadCustomerDocument(CustomerDocumentModel model)
{
        var filename = //your file name and path;
        using (var fs = new FileStream(filename, FileMode.Create))

        {
               model.FileByteStream.CopyTo(fs);
        }
}
于 2012-03-30T19:28:01.513 回答
2

您的数据类型是导致流式传输失败的原因。这在 MSDN 上有记录:http: //msdn.microsoft.com/en-us/library/ms731913.aspx 相关段落是:

流传输的限制

使用流传输模式会导致运行时强制执行附加限制。

流式传输中发生的操作最多可以与一个输入或输出参数签订合同。该参数对应于消息的整个主体,并且必须是 Message、Stream 的派生类型或 IXmlSerializable 实现。有一个操作的返回值相当于有一个输出参数。

某些 WCF 功能(例如可靠的消息传递、事务和 SOAP 消息级安全性)依赖于缓冲消息进行传输。使用这些功能可能会减少或消除使用流式传输所获得的性能优势。要保护流式传输,请仅使用传输级安全性或使用传输级安全性和仅身份验证消息安全性。

SOAP 标头始终被缓冲,即使传输模式设置为流式传输也是如此。消息的标头不得超过 MaxBufferSize 传输配额的大小。有关此设置的详细信息,请参阅传输配额。

于 2011-10-03T14:54:43.860 回答