Reputation: 2520
I have a working WCF-Service with one Method GetCountries
. This function returns an array of 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).
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
[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
Note:: Tried using WebInvoke() as WebGet()
Note2:: Tried decorating the interface aswell as the implementation of my service
Update: When addressing Client-Side i get the error : 400 Bad-Request.
Upvotes: 1
Views: 3173
Reputation: 2520
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!
Upvotes: 0
Reputation: 8877
Try to look at this documentation of WebGet attribute. 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. I have created basic WCF application that is available in menu of visual studio. 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
<?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:
http://localhost:57925/Service1.svc/rest/GetAllCountries
and the response was
{"GetAllCountriesResult":[{"Name":"a"},{"Name":"b"}]}
Upvotes: 1