2011-10-03 147 views
0

我有一个非常基本的Web服务使用WCF(C#,.NET 4.0),以返回一个hello消息。配置WCF服务模型

IIS 7下的部署和运行是好的,但是当我做svcutil.exe http://localhost:4569/Service.svc?wsdl通过CMD测试web服务,我得到:

the remote server returned an error: 415 cannot proccess the message because the content type 'aplication/soap+xml charset=utf8' was not the expected type 'text/xml charset=utf8'

当尝试添加服务引用(创建一个客户端)我得到

An existing connection was forcibly closed by the remote host Metadata contains a reference that cannot be resolved: 'http://localhost:4569/Service.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:4569/Service.svc . 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'..

我敢肯定,这个问题是在我的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" %>   
+2

您没有向我们展示**有趣的**(和**重要的**)部分的配置!服务器端的''中的''部分和* .svc文件的内容!请添加那些 - 否则我们只能猜测最多.... –

+1

是整个配置文件?是代码中定义的服务,绑定和终结点? – MLT

+0

错误消息表明您正在混合使用SOAP和REST。似乎你想检索一个WSDL,但它会显示,如果你使用REST绑定('webHttpBinding').... –

回答

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> 
+0

我试过DonAndre,但它不工作。无论如何编辑我的问题与服务部分, – Tiago

+0

配置不按照您发布的方式工作。行为之前需要关闭服务。您的soap端点的地址应该是空字符串,因为它指的是URL中的.svc之后的内容。 – Andreas

+0

@我知道,代码甚至没有这种方式编译,这是一个错误,但在我的代码是正确的。 – Tiago