消耗Wcf休息服务问题

时间:2013-02-26 09:49:08

标签: wcf c#-4.0 rest

我创建了一个Rest WCF服务。

服务合同

[ServiceContract]
public interface IPracticeService
{
    [OperationContract]
    int AddInt(int value1, int value2);

    [OperationContract]
    double AddDouble(double value1, double value2);

    [OperationContract]
    string Hello();

    [OperationContract]
    Person GetPerson();
}

public class PracticeService : IPracticeService
{
    public int AddInt(int value1, int value2)
    {
        return value1 + value2;
    }

    [OperationBehavior]
    public double AddDouble(double value1, double value2)
    {
        return value1 + value2;
    }

    public string Hello()
    { 
        return "hello";
    }

    [WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json)]
    public Person GetPerson()
    {
        Person p = new Person();
        p.Name = "Abc";
        p.Age = 5;
        return p;
    }

Web Config 

<system.serviceModel>
<services>
  <service name="RestService.IRestServiceImpl" behaviorConfiguration="ServiceBehaviour">
    <endpoint address="*" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web"></endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <!-- 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="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web"></behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

所以当我想要添加ServiceRefrence时在客户端它给我错误。

The HTML document does not contain Web service discovery information.

元数据包含无法解析的引用:“http://mydomain:1121/Rest/RestServiceImpl.svc”。 内容类型application / soap + xml;服务http://mydomain:1121/Rest/RestServiceImpl.svc不支持charset = utf-8。客户端和服务绑定可能不匹配。 远程服务器返回错误:(415)无法处理消息,因为内容类型为'application / soap + xml; charset = utf-8'不是预期的类型'text / xml;字符集= UTF-8' .. 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

那么如何解决呢。

1 个答案:

答案 0 :(得分:0)

您需要像这样修改配置

<system.serviceModel>
<services>
  <service name="PracticeService.IPracticeService" behaviorConfiguration="ServiceBehaviour">
    <endpoint address="*" binding="webHttpBinding" contract="PracticeService.IPracticeService" behaviorConfiguration="web"></endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <!-- 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="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web"></behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />