首页 文章

使用Wcf Service读取然后保存文件

提问于
浏览
1

我正在创建第一个使用Stream的WCF服务 . 我想要的最终产品是从我的硬盘中选择一个文件并将其保存到数据库中 . 因此,我要完成的第一步是阅读选定的.jpg文件 .

我的 Contract 如下:

namespace Web
{
    interface name "IStreamingService" 
    [ServiceContract]
    public interface IStreamingService
    {
        [OperationContract]
        Stream GetStream(string data);
    }
}

我的IService是:

public class StreamingService : IStreamingService
    {
        public Stream GetStream(string data)
        {
            string filePath = data;       
            try
            {
                FileStream imageFile = File.OpenRead(filePath);
                return imageFile;
            }
            catch (IOException ex)
            {
                Console.WriteLine(
                    String.Format("An exception was thrown while trying to open file {0}", filePath));
                Console.WriteLine("Exception is: ");
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
    }

我的Web.Config文件看起来像这样,添加了 httpRuntime maxRequestLength="2147483647" 以允许流式传输大文件 .

<!-- language: xaml -->
   <?xml version="1.0"?>
     <configuration>

      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <!-- ADDED ASP.NET doesn’t know about WCF and it has it’s own limits for the request size, now increased to match maxReceivedMessageSize. -->
        <httpRuntime maxRequestLength="2147483647"/>
      </system.web>
      <system.serviceModel>
        <behaviors>
         <serviceBehaviors>
            <behavior>
              <!-- 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>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>

    </configuration>

然后我添加了服务参考的控制台应用程序 . 我已经更改了app.config,因此transferedMode =“Streamed”和maxReceivedMessageSize =“2147483647”再次允许传输大文件 .

最后我的计划是:

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string data = @"C:/Users/Admin/Desktop/IMG_0038.JPG";

            StreamingServiceClient serviceHost = new StreamingServiceClient();

            serviceHost.GetStream(data);
        }
    }
}

当我运行应用程序时,我收到错误 The remote server returned an unexpected response: (400) Bad Request.

任何人都可以指出我正确的方向,所以我可以继续前进,所以一旦我读了文件,我就可以保存它 .

1 回答

  • 2

    你有两个选择 . 第一种是使用二进制编码的消息传输机制之一 . 如果您不担心与其他平台的互操作性(IOW,.NET-to-.NET通信),您可以选择二进制编码器,或者如果您想与其他非.NET系统通信,则可以使用MTOM . 在这种情况下,您可以将签名更改为字节数组,并确保传输实际字节,而不是流 . 根据图像的大小和一次传送的数量,这可能会或可能不会给系统中的内存带来压力 .

    第二种选择是使用流媒体 . 在这种情况下,您只能使用Basic HTTP绑定(不支持WS HTTP绑定,不支持流式传输) . 您将使用 Stream 返回参数保留消息签名,但设置绑定以支持流式传输 .

    Niraj Bhatt有一个很棒的博客文章, Headers 为“MTOM vs. Streaming vs. Compression – Large attachments over WCF”,详细介绍了如何实现上述每个选项,以及有关何时可能比另一个更有用的更多信息 .

相关问题