WCF流媒体文件传输基于.NET 4流媒体、文件传输、WCF、NET

2023-09-04 01:12:54 作者:海森溺浅你笑我止于你心

我需要WCF流媒体文件传送一个很好的例子。

I need a good example on WCF Streaming File Transfer.

我已经发现了几个,想他们,但该职位是旧的,我对.NET 4和IIS 7 wokding所以出现了一些问题。

I have found several and tried them but the posts are old and I am wokding on .net 4 and IIS 7 so there are some problems.

你能不能给我一个很好的和及时更新的那个例子。

Can you gives me a good and up-to-date example on that.

推荐答案

使用一些技术的发布二进制数据发送到一个RESTful服务如下回答细节。

The following answers detail using a few techniques for a posting binary data to a restful service.

发布二进制数据发送到一个RESTful应用 What是二进制数据传输到一个HTTP REST API服务的好办法? Bad我们考虑使用Web服务来传输大量的有效载荷? Post binary data to a RESTful application What is a good way to transfer binary data to a HTTP REST API service? Bad idea to transfer large payload using web services?

下面code是如何编写一个RESTful WCF服务的样本,是并不完整,但不给你指示,并在那里你可以开始。

The following code is a sample of how you could write a RESTful WCF service and is by no means complete but does give you an indication on where you could start.

样品服务,注意,这是不会生产准备code。

Sample Service, note that this is NOT production ready code.

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class FileService
{
    private IncomingWebRequestContext m_Request;
    private OutgoingWebResponseContext m_Response;

    [WebGet(UriTemplate = "{appName}/{id}?action={action}")]
    public Stream GetFile(string appName, string id, string action)
    {
        var repository = new FileRepository();
        var response = WebOperationContext.Current.OutgoingResponse;
        var result = repository.GetById(int.Parse(id));

        if (action != null && action.Equals("download", StringComparison.InvariantCultureIgnoreCase))
        {
            response.Headers.Add("Content-Disposition", string.Format("attachment; filename={0}", result.Name));
        }

        response.Headers.Add(HttpResponseHeader.ContentType, result.ContentType);
        response.Headers.Add("X-Filename", result.Name);

        return result.Content;
    }

    [WebInvoke(UriTemplate = "{appName}", Method = "POST")]
    public void Save(string appName, Stream fileContent)
    {
        try
        {
            if (WebOperationContext.Current == null) throw new InvalidOperationException("WebOperationContext is null.");

            m_Request = WebOperationContext.Current.IncomingRequest;
            m_Response = WebOperationContext.Current.OutgoingResponse;

            var file = CreateFileResource(fileContent, appName);

            if (!FileIsValid(file)) throw new WebFaultException(HttpStatusCode.BadRequest);

            SaveFile(file);

            SetStatusAsCreated(file);
        }
        catch (Exception ex)
        {
            if (ex.GetType() == typeof(WebFaultException)) throw;
            if (ex.GetType().IsGenericType && ex.GetType().GetGenericTypeDefinition() == typeof(WebFaultException<>)) throw;

            throw new WebFaultException<string>("An unexpected error occurred.", HttpStatusCode.InternalServerError);
        }
    }

    private FileResource CreateFileResource(Stream fileContent, string appName)
    {
        var result = new FileResource();

        fileContent.CopyTo(result.Content);
        result.ApplicationName = appName;
        result.Name = m_Request.Headers["X-Filename"];
        result.Location = @"C:\SomeFolder\" + result.Name;
        result.ContentType = m_Request.Headers[HttpRequestHeader.ContentType] ?? this.GetContentType(result.Name);
        result.DateUploaded = DateTime.Now;

        return result;
    }

    private string GetContentType(string filename)
    {
        // this should be replaced with some form of logic to determine the correct file content type (I.E., use registry, extension, xml file, etc.,)
        return "application/octet-stream";
    }

    private bool FileIsValid(FileResource file)
    {
        var validator = new FileResourceValidator();
        var clientHash = m_Request.Headers[HttpRequestHeader.ContentMd5];

        return validator.IsValid(file, clientHash);
    }

    private void SaveFile(FileResource file)
    {
        // This will persist the meta data about the file to a database (I.E., size, filename, file location, etc)
        new FileRepository().AddFile(file);
    }

    private void SetStatusAsCreated(FileResource file)
    {
        var location = new Uri(m_Request.UriTemplateMatch.RequestUri.AbsoluteUri + "/" + file.Id);
        m_Response.SetStatusAsCreated(location);
    }
}

示例客户端,注意,这是不会生产准备code。

Sample Client, note that this is NOT production ready code.

// *********************************
// Sample Client
// *********************************
private void UploadButton_Click(object sender, EventArgs e)
{
    var uri = "http://dev-fileservice/SampleApplication"
    var fullFilename = @"C:\somefile.txt";
    var fileContent = File.ReadAllBytes(fullFilename);

    using (var webClient = new WebClient())
    {
        try
        {
            webClient.Proxy = null;
            webClient.Headers.Add(HttpRequestHeader.ContentMd5, this.CalculateFileHash());
            webClient.Headers.Add("X-DaysToKeep", DurationNumericUpDown.Value.ToString());
            webClient.Headers.Add("X-Filename", Path.GetFileName(fullFilename));
            webClient.UploadData(uri, "POST", fileContent);

            var fileUri = webClient.ResponseHeaders[HttpResponseHeader.Location];
            Console.WriteLine("File can be downloaded at" + fileUri);
        }
        catch (Exception ex)
        {
            var exception = ex.Message;
        }
    }
}

private string CalculateFileHash()
{
    var hash = MD5.Create().ComputeHash(File.ReadAllBytes(@"C:\somefile.txt"));
    var sb = new StringBuilder();

    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("x2"));
    }

    return sb.ToString();
}

private void DownloadFile()
{
    var uri = "http://dev-fileservice/SampleApplication/1" // this is the URL returned by the Restful file service

    using (var webClient = new WebClient())
    {
        try
        {
            webClient.Proxy = null;
            var fileContent = webClient.DownloadData(uri);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}