【问题标题】:(.Net 4.5) Problems Converting my WCF-Service to Rest-Service(.Net 4.5) 将我的 WCF-Service 转换为 Rest-Service 的问题
【发布时间】:2014-02-24 20:02:27
【问题描述】:

我有一个使用方法 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。

【问题讨论】:

  • 什么版本的.net? 4.5?
  • 是的,它是 4.5。我会把它添加到我的问题中
  • 它给出了什么错误?是否抛出任何异常?
  • 没有抛出异常/没有给出输出(Chrome)。在 Firefox/IE 中,我收到 400 异常“无效请求”
  • 您可以在浏览器中浏览您的服务吗?

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


【解决方案1】:

试着看看这个documentation of WebGet attribute。在示例中,此属性装饰界面。在您的代码中,您正在装饰实现。我认为这可能是您问题的根源。

我制作了一个简单的 WCF 应用程序,并尝试尽可能多地重用代码和配置。我创建了 Visual Studio 菜单中可用的基本 WCF 应用程序。它奏效了。

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

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"}]}

【讨论】:

  • 尝试了装饰界面和实现。同样的问题。也试过 WebInvoke() 。我更多地考虑配置错误
  • 感谢您的努力 (+1)。我会在午休时间尝试测试它。我注意到的唯一区别是您的配置中没有 basichttp-endpoint。 Basichttp 和 Rest 之间是否存在我不知道的已知问题?
  • 我不知道。我没有使用绑定,因为它背后的端点使用了不同的合约。我尝试添加另一个界面并更新了我的答案。它仍然有效:)
  • 还是不行。它真的开始让我生气了....(对不起我的语言):'(
  • 您收到什么异常?还尝试启用 includeExceptionDetailInFaults 并浏览到您的服务的基本 url,看看是否至少有 soap 工作。当我一直在扩展我的示例并添加另一个接口时,我不小心将这些方法命名为相同。当我访问服务的基本 url 时,它编译但在运行时出错。
【解决方案2】:

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

原文:

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

现在(工作):

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

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 2013-05-01
    • 2021-02-12
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2020-04-03
    相关资源
    最近更新 更多