0

我创建了一个 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”。内容类型应用程序/soap+xml;charset=utf-8 不受 service 支持http://mydomain:1121/Rest/RestServiceImpl.svc。客户端和服务绑定可能不匹配。远程服务器返回错误:(415)无法处理消息,因为内容类型'application/soap+xml; charset=utf-8' 不是预期的类型 'text/xml; charset=utf-8'.. 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

那么如何才能解决。

4

1 回答 1

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" />
于 2013-02-27T11:56:30.107 回答