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

时间:2009-06-13 20:44:17

标签: c# wcf

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

我可以通过浏览器查看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>

关于如何解决此问题的任何想法?我应该有2个不同的绑定,一个用于返回非字节[]的方法和用于返回byte []的方法的Mtom?如果是这样,每个方法如何应用绑定?

尝试使用WcfTestClient.exe运行它,这是错误

rror: Cannot import wsdl:portTypeDetail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporterError: Schema with target namespace 'http://tempuri.org/' could not be found.XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='ISearchService']Error: Cannot import wsdl:bindingDetail: There was an error importing a wsdl:portType that the wsdl:binding is dependent on.XPath to wsdl:portType: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='ISearchService']XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:binding[@name='BasicHttpBinding_ISearchService']Error: Cannot import wsdl:portDetail: There was an error importing a wsdl:binding that the wsdl:port is dependent on.XPath to wsdl:binding: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:binding[@name='BasicHttpBinding_ISearchService']XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:service[@name='SearchService']/wsdl:port[@name='BasicHttpBinding_ISearchService']Warning: No code was generated.If you were trying to generate a client, this could be because the metadata documents did not contain any valid contracts or servicesor because all contracts/services were discovered to exist in /reference assemblies. Verify that you passed all the metadata documents to the tool.Warning: If you would like to generate data contracts from schemas make sure to use the /dataContractOnly option.

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

在客户端应用程序上,您是否有对WCFExtras程序集的引用?该错误似乎表明绑定未被识别,我认为这是因为WCFExtras引入了扩展。尝试将相同的扩展部分添加到客户端的配置文件中,就像在服务器的配置文件中一样。

答案 2 :(得分:0)

您的绑定是basicHttpBinding,但您的绑定配置是wsHttpMTOMBinding。

这看起来像是不匹配,但它可能只是你给出的东西的名字。

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

希望这有帮助

设拉子

答案 3 :(得分:0)

这不是一个真正的答案,但是在我从app.config文件中删除WCfExtras引用并部署后,事情似乎有效。

这是我想要实现的目标,但暂时放弃了

WCF Extras

如果有人知道我配置错误的方式和内容,请在此栏目中提供建议。

非常感谢

答案 4 :(得分:0)

能够使用WCF测试客户端的答案是在Web配置中设置set singleFile =“true”,如下所示。

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