C# 尝试使用WCF服务返回文件时出错

C# 尝试使用WCF服务返回文件时出错,c#,wcf,C#,Wcf,我的一个方法返回文件的字节[]。所有其他方法返回字符串对象或自定义对象 我可以通过浏览器查看WSDL,正如我使用的WCfExtras一样,我甚至可以查看文档 在我的测试应用程序C#web应用程序中,我将引用添加到托管在测试服务器上的svc。我得到这样一个错误: The document was understood, but it could not be processed. - The WSDL document contains links that could not be

我的一个方法返回文件的字节[]。所有其他方法返回字符串对象或自定义对象

我可以通过浏览器查看WSDL,正如我使用的WCfExtras一样,我甚至可以查看文档

在我的测试应用程序C#web应用程序中,我将引用添加到托管在测试服务器上的svc。我得到这样一个错误:

     The document was understood, but it could not be processed.
  - The WSDL document contains links that could not be resolved.
  - There was an error downloading ...
  - Unable to connect to remote server
  - No connection could be made because the target machine actively refused it

    Content Type application/soap+xml; charset=utf-8 was not supported by service .  The client and service bindings may be mismatched.
    The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
    If the service is defined in the current solution, try building the solution and adding the service reference again.
这是我的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="TestApp.Properties.Settings.TestDBConnectionString"
      connectionString="Data Source=192.168.2.130;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;Password=xerox"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="TestApp.Service1Behavior"
        name="TestApp.SearchService">
        <endpoint address="" behaviorConfiguration="Sample.WsdlSampleEndpointBehavior"
          binding="basicHttpBinding" bindingConfiguration="WsHttpMtomBinding" contract="TestApp.ISearchService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/TestApp/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
       <endpointBehaviors>
        <behavior name="Sample.WsdlSampleEndpointBehavior">
          <wsdlExtensions location="http://localhost:8731/TestApp/Service1/"/>
        </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
        <behavior name="TestApp.Service1Behavior">
          <!-- 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="true" />
        </behavior>

      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <!-- Declare that we have an extension called WSDL Extras-->
        <add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </behaviorExtensions>
    </extensions>
    <bindings>
      <basicHttpBinding>
        <binding name="WsHttpMtomBinding" messageEncoding="Mtom" transferMode="StreamedResponse" />
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

在客户端应用程序上,是否有对WCFExtras程序集的引用?错误似乎是说绑定无法识别,我认为这是因为WCFExtras引入了扩展。尝试将与服务器配置文件中相同的扩展部分添加到客户端的配置文件中。

您的绑定是basicHttpBinding,但您的绑定配置是WSHttpTomBinding

这看起来是不匹配的,但是可能只是您给了一些东西的名称

尝试在WCF配置工具中打开配置文件,如果您的配置文件中存在不一致,这将告诉您(拒绝打开该文件)

希望这有帮助


设拉子

这不可能是一个真实的答案,但在我从app.config文件中删除了WCfExtras引用并部署之后,一切似乎都正常了

这就是我试图实现的目标,但我暂时放弃了

如果有人知道我配置错误的方式和原因,请在本专栏中提供建议


非常感谢

对于我来说,能够使用WCF测试客户端的答案是在web配置中设置set singleFile=“true”,如下所示

<behavior name="Sample.WsdlSampleEndpointBehavior">
      <wsdlExtensions location="http://localhost:8731/TestApp/Service1/" singleFile="true" />
 </behavior>


我也有同样的问题。我在IIS的远程win2008服务器上托管wcf。我的解决方案是在添加网站时设置主机名,在设置所有其他托管数据后不要将其留空

Jacob,我正在使用WCfTestClient.exe,VS2008的一部分来尝试调用和使用我的服务。不确定我将如何在WcfTestClient.exeI中添加该引用,这意味着您可以将扩展添加到客户端应用程序中。我想我是在假设您也在为web服务编写客户机,而您的问题是无法添加对它的引用。我对WcfTestClient.exe不是很熟悉。如果您发布正在生成的WSDL,我们可能会更容易提供帮助。Jacob将在一天结束或明天服务器对我可用时发布WSDL。Shiraz,我有一个方法返回文件内容,其返回类型为Bytes[]。我的所有其他方法都返回字符串和int,因此我使用了Mtom绑定配置。不确定是否可以对以字节[]形式返回大文件的方法应用不同的绑定配置,对返回字符串和INTS的方法应用不同的绑定配置。我想说的是,不能在同一个端点中组合basichttp和wshttp。在我们的一个项目中,我们的情况与您相同,我们刚刚使用了简单的BasicHttpBinding,消息大小设置得足够高。我们用它来发送高达30MB的文件。在内存使用方面,这不如使用MTOM有效,但我们有足够的内存,所以这不是一个问题。我建议您首先尝试使用basichttp绑定,而不使用MTOM。如果需要MTOM,请拆分合同,并检查是否使用wshttpbinding。
<behavior name="Sample.WsdlSampleEndpointBehavior">
      <wsdlExtensions location="http://localhost:8731/TestApp/Service1/" singleFile="true" />
 </behavior>