[英](.Net 4.5) Problems Converting my WCF-Service to Rest-Service
I have a working WCF-Service with one Method GetCountries
. 我有一个带有一个方法GetCountries
WCF服务。 This function returns an array of custom object DTOCountry
. 此函数返回custom object DTOCountry
的数组。 Yet when i convert my method to rest, i can't seem to get it working. 但是,当我将方法转换为休息状态时,我似乎无法正常工作。 Moreover it keeps on pushing me towards my old BasicHttpBinding
(in my WCF-Testclient). 而且,它继续将我推向旧的BasicHttpBinding
(在WCF-Testclient中)。
Could someone point out what i'm doing wrong? 有人可以指出我在做什么错吗?
My Config 我的配置
Endpoints (note the different contract that is linked): 端点(请注意链接的不同合同):
<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: 行为:
<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>
My bindings: 我的绑定:
<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>
My Interface 我的介面
[ServiceContract]
public interface IPW_GeoService
{
[OperationContract]
DTOCountry[] GetAllCountries();
}
My SVC 我的SVC
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetAllCountries")]
public DTOCountry[] GetAllCountries()
{
try
{
return _myCountryBl.GetAllCountries().ToArray();
}
catch (Exception ex)
{
throw;
}
}
Question: Can someone spot the configuration error? 问题:有人可以发现配置错误吗? I address my method as follows Http://localhost/PW_GeoService.svc/GeoRest/GetAllCountries
我按以下方式处理我的方法Http://localhost/PW_GeoService.svc/GeoRest/GetAllCountries
Note: : Tried using WebInvoke() as WebGet() 注意::尝试使用WebInvoke()作为WebGet()
Note2: : Tried decorating the interface aswell as the implementation of my service 注意2 ::尝试装饰界面以及我的服务的实现
Update: When addressing Client-Side i get the error : 400 Bad-Request. 更新:在解决客户端问题时,出现错误:400 Bad-Request。
Try to look at this documentation of WebGet attribute . 尝试查看此WebGet属性文档 。 In the example this attribute decorates the interface. 在示例中,此属性装饰接口。 In your code you are decorating the implementation. 在代码中,您正在修饰实现。 I think this could be the source of your problem. 我认为这可能是您问题的根源。
I made a simple WCF application and tried to reuse as much code and configuration as possible. 我制作了一个简单的WCF应用程序,并尝试重用尽可能多的代码和配置。 I have created basic WCF application that is available in menu of visual studio. 我创建了Visual Studio菜单中可用的基本WCF应用程序。 And it worked. 而且有效。
Here is my code: Service contracts: 这是我的代码:服务合同:
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; }
}
}
Service implementation: 服务实施:
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" } };
}
}
}
And web.config 和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>
I made following request in IE11: 我在IE11中提出了以下要求:
http://localhost:57925/Service1.svc/rest/GetAllCountries
and the response was 回应是
{"GetAllCountriesResult":[{"Name":"a"},{"Name":"b"}]}
Found the problem. 找到了问题。 It was problem in my protocolmapping: 这是我的协议映射中的问题:
Original: 原版的:
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
Now (Working): 工作中):
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
Thanks for everyone for the help! 感谢大家的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.