C# 尝试使用 WCF 服务返回文件时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/991480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error when trying to consume WCF service returning file
提问by user38230
One of my methods returns byte[], of a file. All other methods return either string objects or custom objects
我的方法之一返回文件的字节 []。所有其他方法返回字符串对象或自定义对象
I can view the WSDL via a browser and as i have used WCfExtras, I can even view the documentation.
我可以通过浏览器查看 WSDL,因为我使用了 WCfExtras,我什至可以查看文档。
In my test app, C# web application, I add the reference to my svc, hosted on a test server. I get an error as such:
在我的测试应用程序 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.
This is my app.config
这是我的 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>
Any ideas on how I would resolve this? Should I have 2 different bindings, one for methods returning non bytes[] and Mtom for method returning byte[]? If so how does one apply bindings per method exposed?
关于我如何解决这个问题的任何想法?我应该有 2 种不同的绑定,一种用于返回非字节 [] 的方法,而 Mtom 用于返回字节 [] 的方法?如果是这样,如何为每个公开的方法应用绑定?
Tried running it with WcfTestClient.exe and this is the error
尝试使用 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.
回答by Jacob
On the client app, do you have a reference to the WCFExtras assembly? The error appears to be saying that the binding is not recognized, which is, I assume, because of the extensions introduced by WCFExtras. Try adding the same extensions section to your client's config file as is in the server's config file.
在客户端应用程序上,您是否有对 WCFExtras 程序集的引用?该错误似乎是说无法识别绑定,我认为这是由于 WCFExtras 引入的扩展。尝试将与服务器配置文件中相同的扩展部分添加到客户端的配置文件中。
回答by Shiraz Bhaiji
Your binding is basicHttpBinding, but your binding configuration is wsHttpMTOMBinding.
您的绑定是 basicHttpBinding,但您的绑定配置是 wsHttpMTOMBinding。
This looks like a mismatch, however it could be just the names that you have given things.
这看起来不匹配,但它可能只是您给事物命名的名称。
Try opening the config file in the WCF config tool, this should tell you (refuse to open the file), if there are inconsistencies in your config file.
尝试在 WCF 配置工具中打开配置文件,这应该告诉您(拒绝打开文件),如果您的配置文件中存在不一致。
Hope this helps
希望这可以帮助
Shiraz
设拉子
回答by user38230
This can't be a true answer, but after I removed WCfExtras references from my app.config file and deployed, things seem to work.
这不是一个正确的答案,但是在我从 app.config 文件中删除 WCfExtras 引用并部署之后,事情似乎正常了。
This is what I was trying to achieve, but have abandoned for time being
这就是我试图实现的目标,但暂时放弃了
If anyone has an idea of how and what I configured wrong, please advise on this column.
如果有人知道我配置错误的方式和原因,请在本专栏中提供建议。
Many Thanks
非常感谢
回答by user38230
The answer for me to be able to use the WCF Test Client is to set the set singleFile="true" in the web config like this.
我能够使用 WCF 测试客户端的答案是像这样在 web 配置中设置 set singleFile="true" 。
<behavior name="Sample.WsdlSampleEndpointBehavior">
<wsdlExtensions location="http://localhost:8731/TestApp/Service1/" singleFile="true" />
</behavior>
回答by user700846
I have the same problem. I was hosting the wcf on remote win2008 server on IIS. The solution that has worked for me was setting the host name when adding web site and don't leave it blank after setting all other data for hosting!
我也有同样的问题。我在 IIS 上的远程 win2008 服务器上托管 wcf。对我有用的解决方案是在添加网站时设置主机名,并且在设置所有其他托管数据后不要将其留空!