如何将图像数据发送到 WCF 服务

发布于 2024-12-14 02:40:44 字数 4292 浏览 1 评论 0原文

我已经搜索并尝试了所有以前发布的建议解决方案,但没有任何运气。我显然做错了一些我无法弄清楚的事情,我希望有人能够在这里帮助我。我的问题是这样的:

我有一个 WCF 服务和一个 WCF 客户端。当我调用服务并尝试传递字节数组(将图像上传到服务)时,我收到一个非常模糊的目标调用异常错误。因此,我连接了跟踪,跟踪日志告诉我消息配额大小超出了限制,但我修改了两端的配置(app.config 和 web.config)以包含非常大的限制。问题是,它告诉我超出了 65536 的限制,但正如您从下面的配置中看到的那样,它远远大于该数量,所以就好像我的客户端正在使用其他一些配置值或者我只是没有配置这是对的,它忽略了我所拥有的。有人可以帮助我吗?

应用程序配置: 在此处输入图像描述

Web.Config: 在此处输入图像描述

如果你们能提供任何帮助,我们将不胜感激。我已经被这个问题困扰了两天了。

谢谢大家。

这是实际的配置代码:

<system.serviceModel>
    <bindings>            
        <wsHttpBinding>
            <binding name="WSHttpBinding_IDataSyncService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                    maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost/DataSyncWCF/DataSyncService.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDataSyncService"
            contract="IDataSyncService" name="WSHttpBinding_IDataSyncService">
            <identity>
              <dns value="localhost" />
            </identity>
      </endpoint>
    </client>
</system.serviceModel> 

Web.Config:

<services>
  <service behaviorConfiguration="DataSyncWCF.Service1Behavior"
    name="DataSyncWCF.DataSyncService">
    <endpoint address="" binding="wsHttpBinding" bindingName="WSHttpBinding_IDataSyncService"
      contract="DataSyncWCF.IDataSyncService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<bindings>      
  <wsHttpBinding>
    <binding name="DataSyncWCF.Service1Binding" maxBufferPoolSize="2147483647"
      maxReceivedMessageSize="2147483647" allowCookies="true">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="None">            
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="DataSyncWCF.Service1Behavior">          
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

这是我调用 WCF 服务的代码:

VehicleImage vi = new VehicleImage();
System.IO.FileStream fs = new System.IO.FileStream(@"C:\images\1FAHP35N18W1589_01.jpg",        System.IO.FileMode.Open, System.IO.FileAccess.Read);
int len = (int)fs.Length;
vi = new VehicleImage();
vi.Image = new Byte[len];            
fs.Read(vi.Image, 0, len);   

// Here's the call to the WCF Service. It never makes it to the Service because of the message size limit error.
ResponseContract rc = client.SyncImage(vi);

I've searched and tried all previous posted suggested solutions and have not had any luck. I am obviously doing something wrong which I just can't figure out and I'm hoping someone will be able to help me here.My problem is this:

I have a WCF Service, and a WCF client. When I call the service and try to pass a byte array (uploading an image to the service), I get a Target Invokation exception error which is very vague. So, I hooked up tracing and the trace log is telling me the message quota size exceeded the limit, but I have modified the configs on both ends (app.config & web.config) to contain a really large limit. The problem is, it's telling me that the limit of 65536 was exceeded, but as you can see from my configs below it's way larger than that amount, so it's as if my client is using some other configuration values or I just didn't configure it right and it's ignoring what I have. Can someone help me?

App.Config:
enter image description here

Web.Config:
enter image description here

Any help you guys could offer would be greatly appreciated. I've been stuck on this problem now for two days.

Thanks everyone.

Here's the actual configuration code:

<system.serviceModel>
    <bindings>            
        <wsHttpBinding>
            <binding name="WSHttpBinding_IDataSyncService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                    maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost/DataSyncWCF/DataSyncService.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDataSyncService"
            contract="IDataSyncService" name="WSHttpBinding_IDataSyncService">
            <identity>
              <dns value="localhost" />
            </identity>
      </endpoint>
    </client>
</system.serviceModel> 

Web.Config:

<services>
  <service behaviorConfiguration="DataSyncWCF.Service1Behavior"
    name="DataSyncWCF.DataSyncService">
    <endpoint address="" binding="wsHttpBinding" bindingName="WSHttpBinding_IDataSyncService"
      contract="DataSyncWCF.IDataSyncService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<bindings>      
  <wsHttpBinding>
    <binding name="DataSyncWCF.Service1Binding" maxBufferPoolSize="2147483647"
      maxReceivedMessageSize="2147483647" allowCookies="true">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="None">            
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="DataSyncWCF.Service1Behavior">          
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

Here's my code calling the WCF Service:

VehicleImage vi = new VehicleImage();
System.IO.FileStream fs = new System.IO.FileStream(@"C:\images\1FAHP35N18W1589_01.jpg",        System.IO.FileMode.Open, System.IO.FileAccess.Read);
int len = (int)fs.Length;
vi = new VehicleImage();
vi.Image = new Byte[len];            
fs.Read(vi.Image, 0, len);   

// Here's the call to the WCF Service. It never makes it to the Service because of the message size limit error.
ResponseContract rc = client.SyncImage(vi);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2024-12-21 02:40:44

WCF 中的一切都受到严格限制(节流),这是有原因的。

我在你的配置中看到很多 2G 号码,这通常不是一个好主意。您可能错过了一些,我无法这么快发现 SerializationLimit (大约名称)。

在 WCF 中有两种处理大消息的基本方法:MTOM 和流式传输。

Everything in WCF is heavily limited (throttled), with reason.

I see a lot of 2G numbers in your config, that would not be a good idea in general. And you may have missed a few, I can't spot a SerializationLimit (approx name) so quickly.

There are 2 basic ways to deal with large messages in WCF: MTOM and streaming.

一江春梦 2024-12-21 02:40:44

在 web.config wsHttpBinding 中,设置 maxReceivedMessageSize:

<binding name="www" maxReceivedMessageSize="2147483647">

In your web.config wsHttpBinding, set the maxReceivedMessageSize:

<binding name="www" maxReceivedMessageSize="2147483647">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文