【问题标题】:Configure Service Model for WCF为 WCF 配置服务模型
【发布时间】: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" %>        

【问题讨论】:

  • 你没有向我们展示你的配置的有趣(和重要)部分!服务器端&lt;system.serviceModel&gt; 中的&lt;services&gt; 部分和*.svc 文件的内容!请添加这些 - 否则我们最多只能猜测......
  • 这是整个配置文件吗?服务、绑定和端点是否在代码中定义?
  • 错误消息表明您正在混合使用 SOAP 和 REST。您似乎想要检索 WSDL,但看起来好像您正在使用 REST 绑定 (webHttpBinding) ....
  • @marc_s 我试过了,编辑了问题,但没有用..
  • 当您说“运行正常”时,您的意思是在浏览器中,您是否尝试通过 wcftestclient 连接到它?另外,这可能是一个愚蠢的问题,但是您的 getMessage() 是否实现了服务合同?

标签: c# .net wcf


【解决方案1】:

您的配置中没有定义服务和端点。尝试添加

<services>
  <service name="Service"> <!-- name should match the name in your .svc file (if you open it with a text editor) -->
    <endpoint name="soap" address="" binding="basicHttpBinding" contract="IService" />
    <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>

【讨论】:

  • 我试过 DonAndre,但它不起作用。无论如何,用服务部分编辑了我的问题,
  • 配置不像您发布的那样工作。服务需要在行为之前关闭。您的soap端点的地址应该是一个空字符串,因为它指的是URL中.svc之后的内容。
  • @我知道,代码甚至不能这样编译,放在那里是错误的,但在我的代码中是正确的。
  • @Tiago 我看到您还启用了“runAllManagedModulesForAllRequests”,请尝试禁用它并再次查看。也许错误来自模块之一。另外,请使用 IIS 使用的版本更新您问题中的 web.config。除了您的肥皂端点的地址是绝对的而不是相对的,我看不出配置有问题。
猜你喜欢
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 2013-06-10
  • 2011-10-04
  • 2017-12-17
  • 2014-03-28
相关资源
最近更新 更多