繁体   English   中英

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

[英](.Net 4.5) Problems Converting my WCF-Service to Rest-Service

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

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

我制作了一个简单的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"}]}

找到了问题。 这是我的协议映射中的问题:

原版的:

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

工作中):

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

感谢大家的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM