【发布时间】:2011-10-03 15:26:23
【问题描述】:
我有一个使用 WCF(C#、.NET 4.0)的非常基本的 Web 服务,用于返回一个 hello 消息。
在 IIS 7 下部署并运行它是可以的,但是当我通过 CMD 执行svcutil.exe @987654321@ 来测试我得到的 web 服务时:
远程服务器返回错误:415 无法处理该消息 因为内容类型 'aplication/soap+xml charset=utf8' 不是 预期类型 'text/xml charset=utf8'
当尝试添加服务引用(以创建客户端)时,我得到了
现有连接被远程主机元数据强行关闭 包含无法解析的引用: 'http://localhost:4569/Service.svc'。内容类型 应用程序/肥皂+xml; charset=utf-8 不受服务支持 http://localhost:4569/Service.svc。客户端和服务绑定 可能不匹配。远程服务器返回错误:(415)不能 处理消息,因为内容类型 'application/soap+xml; charset=utf-8' 不是预期的类型 'text/xml; charset=utf-8'..
我很确定问题出在我的 Web.config 文件中:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Service">
<endpoint name="soap"
address="http://localhost:4569/Service.svc"
binding="basicHttpBinding"
contract="IService" />
<endpoint name="mex" address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</services>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
无论如何,这是我的代码:
IService.cs:
[ServiceContract]
public interface IService
{
[OperationContract]
string getMessage();
}
我的service.cs有方法
public class Service : IService
{
public string getMessage()
{
return "Ola servico";
}
}
我真的不知道发生了什么,经过一些研究做了一些测试,但没有成功。
Service.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
【问题讨论】:
-
你没有向我们展示你的配置的有趣(和重要)部分!服务器端
<system.serviceModel>中的<services>部分和*.svc 文件的内容!请添加这些 - 否则我们最多只能猜测...... -
这是整个配置文件吗?服务、绑定和端点是否在代码中定义?
-
错误消息表明您正在混合使用 SOAP 和 REST。您似乎想要检索 WSDL,但看起来好像您正在使用 REST 绑定 (
webHttpBinding) .... -
@marc_s 我试过了,编辑了问题,但没有用..
-
当您说“运行正常”时,您的意思是在浏览器中,您是否尝试通过 wcftestclient 连接到它?另外,这可能是一个愚蠢的问题,但是您的 getMessage() 是否实现了服务合同?