Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字节数组从WPF传递到WebApi_Wpf_Asp.net Web Api - Fatal编程技术网

将字节数组从WPF传递到WebApi

将字节数组从WPF传递到WebApi,wpf,asp.net-web-api,Wpf,Asp.net Web Api,tl;dr从WPF应用程序向WebAPI服务方法传递高达1MBish的二进制数据的最佳方法是什么 我目前正在尝试将二进制数据从WPF应用程序传递到WebAPI web服务,结果是可变的。小于100k的小文件通常可以正常工作,但如果文件太大,成功的几率就会降低 一个标准的OpenFileDialog,然后File.ReadAllBytes将byte[]参数传递到WPF中的客户机方法中。这总是成功的,然后我通过PostAsync调用和ByteArrayContent参数将数据发布到WebAPI 这是

tl;dr从WPF应用程序向WebAPI服务方法传递高达1MBish的二进制数据的最佳方法是什么

我目前正在尝试将二进制数据从WPF应用程序传递到WebAPI web服务,结果是可变的。小于100k的小文件通常可以正常工作,但如果文件太大,成功的几率就会降低

一个标准的OpenFileDialog,然后File.ReadAllBytes将byte[]参数传递到WPF中的客户机方法中。这总是成功的,然后我通过PostAsync调用和ByteArrayContent参数将数据发布到WebAPI

这是正确的方法吗?我从一个PostJSONAsync调用开始,并将字节[]传递给它,但认为ByteArrayContent似乎更合适,但两者都不能可靠地工作

WPF中的客户机方法

起初,我认为这很可能是由于在我的开发pc上Windows 7上运行IIS Express的限制造成的,但在运行server 2012的临时服务器上,我们也遇到了同样的问题

任何关于如何实现这一点的建议都会很好,甚至仅仅是一个从WPF上传文件到WebAPI的基本示例都会很好,因为我在那里发现的大多数代码都与从多部分表单网页上传文件有关

非常感谢您的帮助。

tl;dr它是我们WebApi服务中代码的一个单独部分,导致它出错,duh

啊,这太尴尬了

事实证明,我们的问题在于我们在WebApiConfig.RegisterHttpConfiguration config中注册的一个请求记录器类,而我忘记了这个类

它通过async将请求内容作为StringContent读取,然后尝试将其记录到ncarcharmax字段中的数据库中。这本身可能没问题,但我猜当LoggingHandler和主WebApi控制器都试图通过异步访问请求内容时,所有奇怪的问题都开始出现了

删除LoggingHandler立即修复了这个问题,现在我们可以上传高达100MB的文件而没有任何问题。为了更永久地修复它,我想我需要重写LoggingHandler来设置它试图记录/忽略某些内容类型的最大内容大小限制

这是值得怀疑的,但我希望有一天这对某人有用

public class LoggingHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        LogRequest(request);

        return base.SendAsync(request, cancellationToken).ContinueWith(task =>
        {
            var response = task.Result;

            // ToDo: Decide if/when we need to log responses
            // LogResponse(response);

            return response;
        }, cancellationToken);
    }

    private void LogRequest(HttpRequestMessage request)
    {
        (request.Content ?? new StringContent("")).ReadAsStringAsync().ContinueWith(x =>
        {
            try
            {
                var callerId = CallerId(request);
                var callerName = CallerName(request);

                // Log request
                LogEntry logEntry = new LogEntry
                {
                    TimeStamp = DateTime.Now,
                    HttpVerb = request.Method.ToString(),
                    Uri = request.RequestUri.ToString(),
                    CorrelationId = request.GetCorrelationId(),
                    CallerId = callerId,
                    CallerName = callerName,
                    Controller = ControllerName(request),
                    Header = request.Headers.ToString(),
                    Body = x.Result
                };

                ...........
[HttpPost]
[Route("api/productTest/{productTestId}/mcuFirmware")]
public async Task<bool> UploadMcuFirmware(int productTestId)
{
    bool result = false;

    try
    {
        Byte[] mcuFirmwareBytes = await Request.Content.ReadAsByteArrayAsync();

        ....
    }
  <security>
    <requestFiltering>
    <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
<httpRuntime targetFramework="4.5" maxRequestLength="2097152"/>
Specified argument was out of the range of valid values. Parameter name: offset
at System.Web.HttpInputStream.Seek(Int64 offset, SeekOrigin origin)\r\n   
at System.Web.HttpInputStream.set_Position(Int64 value)\r\n   at System.Web.Http.WebHost.SeekableBufferedRequestStream.SwapToSeekableStream()\r\n   at System.Web.Http.WebHost.Seek
Message = "An error occurred while communicating with the remote host. The error code is 0x800703E5."

InnerException = {"Overlapped I/O operation is in progress. (Exception from HRESULT: 0x800703E5)"}

at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)\r\n   
at System.Web.Hosting.IIS7WorkerRequest.ReadEntityCoreSync(Byte[] buffer, Int32 offset, Int32 size)\r\n   
at System.Web.Hosting.IIS7WorkerRequ...
public class LoggingHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        LogRequest(request);

        return base.SendAsync(request, cancellationToken).ContinueWith(task =>
        {
            var response = task.Result;

            // ToDo: Decide if/when we need to log responses
            // LogResponse(response);

            return response;
        }, cancellationToken);
    }

    private void LogRequest(HttpRequestMessage request)
    {
        (request.Content ?? new StringContent("")).ReadAsStringAsync().ContinueWith(x =>
        {
            try
            {
                var callerId = CallerId(request);
                var callerName = CallerName(request);

                // Log request
                LogEntry logEntry = new LogEntry
                {
                    TimeStamp = DateTime.Now,
                    HttpVerb = request.Method.ToString(),
                    Uri = request.RequestUri.ToString(),
                    CorrelationId = request.GetCorrelationId(),
                    CallerId = callerId,
                    CallerName = callerName,
                    Controller = ControllerName(request),
                    Header = request.Headers.ToString(),
                    Body = x.Result
                };

                ...........