配置WCF的服务模型

时间:2011-10-03 15:26:23

标签: c# .net wcf

我有一个非常基本的Web服务,使用WCF(C#,. NET 4.0)来返回一个问候消息。

IIS 7下的部署并运行它是可以的,但是当我通过CMD svcutil.exe http://localhost:4569/Service.svc?wsdl来测试我得到的web服务时:

  

远程服务器返回错误:415无法处理该消息   因为内容类型'aplication / soap + xml charset = utf8'不是   期望的类型'text / xml charset = utf8'

当尝试添加服务引用(创建客户端)时,我得到了

  

远程主机元数据强行关闭现有连接   包含无法解析的引用:   的 'http://本地主机:4569 / Service.svc'。内容类型   应用/肥皂+ xml的;服务不支持charset = utf-8   http://localhost:4569/Service.svc。客户端和服务绑定   可能不匹配。远程服务器返回错误:(415)不能   处理消息因为内容类型'application / soap + xml;   charset = utf-8'不是预期的类型'text / xml;字符集= 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" %>        

1 个答案:

答案 0 :(得分: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>
相关问题