(.Net 4.5)将我的WCF服务转换为Rest-Service的问题

时间:2014-02-24 20:02:28

标签: c# json wcf rest .net-4.5

我有一个有一个方法GetCountries的工作WCF服务。此函数返回custom object DTOCountry的数组。然而,当我将我的方法转换为休息时,我似乎无法让它工作。此外,它继续推动我走向我的旧BasicHttpBinding(在我的WCF-Testclient中)。

有人能指出我做错了吗?

我的配置

端点(请注意链接的不同合同):

    <service name="Partywhere.WCFService.Services.PublicServices" behaviorConfiguration="WCFService.PublicBehavior">
    <endpoint address="" binding="basicHttpBinding" contract="Partywhere.WCFService.Services.IPW_UserService" behaviorConfiguration="WCFServiceEndpoint.PublicBehavior">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="GeoRest" binding="webHttpBinding" behaviorConfiguration="restfulBehavior" bindingConfiguration="MyWebHttp" contract="Partywhere.WCFService.Services.IPW_GeoService">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" behaviorConfiguration="WCFServiceEndpoint.PublicBehavior"/>
  </service>

行为:

<behaviors>
  <endpointBehaviors>
    <behavior name="WCFServiceEndpoint.PublicBehavior" />
    <behavior name="restfulBehavior">
      <webHttp></webHttp>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="WCFService.PublicBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

我的绑定:

<bindings>
  <basicHttpBinding>
    <binding name="Partywhere.WCFService.Services.PublicServices" maxBufferSize="500000000" maxBufferPoolSize="524288" maxReceivedMessageSize="500000000">
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
      </security>
    </binding>
  </basicHttpBinding>
  <mexHttpBinding>
    <binding name="mex"/>
  </mexHttpBinding>
  <webHttpBinding>
    <binding name="MyWebHttp" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" receiveTimeout="05:00:00" openTimeout="05:00:00" closeTimeout="05:00:00" sendTimeout="05:00:00">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </webHttpBinding>
</bindings>

我的界面

[ServiceContract]
public interface IPW_GeoService
{

    [OperationContract]
    DTOCountry[] GetAllCountries();
}

我的SVC

    [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetAllCountries")]
    public DTOCountry[] GetAllCountries()
    {
        try
        {
            return _myCountryBl.GetAllCountries().ToArray();
        }
        catch (Exception ex)
        {
            throw;
        }
    }

问题:有人能发现配置错误吗? 我的方法如下Http://localhost/PW_GeoService.svc/GeoRest/GetAllCountries

注意::尝试使用WebInvoke()作为WebGet()

注2::尝试装饰界面以及我的服务实施

更新:在寻址客户端时,我收到错误:400 Bad-Request。

2 个答案:

答案 0 :(得分:1)

尝试查看此documentation of WebGet attribute。在该示例中,此属性修饰接口。在您的代码中,您正在装饰实现。我认为这可能是你问题的根源。

我制作了一个简单的WCF应用程序,并尝试尽可能多地重用代码和配置。我已经创建了基本的WCF应用程序,可以在visual studio的菜单中找到。它起作用了。

这是我的代码: 服务合同:

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetAllCountries")]
        DTOCountry[] GetAllCountries();
    }

    [ServiceContract]
    public interface IService2
    {
        [OperationContract]
        DTOCountry[] GetAllCountries2();
    }

    [DataContract]
    public class DTOCountry
    {
        [DataMember]
        public string Name { get; set; }
    }
}

服务实施:

using System.ServiceModel;

namespace WcfService1
{
    [ServiceBehavior]
    public class Service1 : IService1, IService2
    {
        public DTOCountry[] GetAllCountries()
        {
            return new DTOCountry[2] { new DTOCountry { Name = "a" }, new DTOCountry { Name = "b" } };
        }

        public DTOCountry[] GetAllCountries2()
        {
            return new DTOCountry[2] { new DTOCountry { Name = "a" }, new DTOCountry { Name = "b" } };
        }
    }
}

和web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="WCFService.PublicBehavior">
        <endpoint binding="basicHttpBinding" address="basic" contract="WcfService1.IService2" behaviorConfiguration="WCFServiceEndpoint.PublicBehavior">
        </endpoint>
        <endpoint binding="webHttpBinding" address="rest" contract="WcfService1.IService1" behaviorConfiguration="restfulBehavior">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" behaviorConfiguration="WCFServiceEndpoint.PublicBehavior"/>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="MyWebHttp" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" receiveTimeout="05:00:00" openTimeout="05:00:00" closeTimeout="05:00:00" sendTimeout="05:00:00">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </webHttpBinding>
      <basicHttpBinding>
        <binding name="" maxBufferSize="500000000" maxBufferPoolSize="524288" maxReceivedMessageSize="500000000">
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WCFServiceEndpoint.PublicBehavior" />
        <behavior name="restfulBehavior">
          <webHttp></webHttp>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="WCFService.PublicBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

我在IE11中提出了以下请求:

http://localhost:57925/Service1.svc/rest/GetAllCountries

,回复是

{"GetAllCountriesResult":[{"Name":"a"},{"Name":"b"}]}

答案 1 :(得分:0)

发现问题。这是我的协议映射中的问题:

原件:

<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>

现在(工作):

<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https" />
  <add binding="webHttpBinding" scheme="http" />
</protocolMapping>

感谢大家的帮助!