Got 404 when consume WCF in REST mode - wcf

I used this url in browser, and I expect to see an XML, I only got 404 not found error. Can you help please?
--http://SomeIP:65001/WCFPilot/Service1/REST/GetData
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Windows" />
</system.web>
<system.serviceModel>
<services>
<service name="WCFPilot.Service1">
<endpoint address="REST" binding="webHttpBinding" contract="WCFPilot.IService1" behaviorConfiguration="REST">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp faultExceptionEnabled="true" helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
Service Contract
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetData")]
string GetData();
}
Thanks

The URL for the service should be
http://SomeIP:65001/Service1.svc/REST/GetData

Related

Can't surf to WCF REST service from browser

Consider this:
[ServiceContract]
public interface ICreditCardExtractor
{
[OperationContract]
[WebGet]
string CoolAction();
}
I've directed a new app in my IIS to the artifacts, and able to surf to files.
However, when I try to invoke the CoolAction (by browsing to http://localhost/MySvc/CreditCardExtractor.svc/CoolAction)
I get
The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
This is my web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
<!--pages controlRenderingCompatibilityVersion="4.0"/-->
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<!--added service behaviour configuration-->
<services>
<service name="WcfService1.CreditCardExtractor">
<endpoint
address=""
binding="webHttpBinding"
contract="WcfService1.ICreditCardExtractor" />
</service>
</services>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
....
</configuration>
You've defined your endpoint behavior - but you haven't applied it to the service endpoint - try to update your configuration to:
<services>
<service name="WcfService1.CreditCardExtractor">
<endpoint
address=""
behaviorConfiguration="web" -- add this line here!!
binding="webHttpBinding"
contract="WcfService1.ICreditCardExtractor" />
</service>
</services>

Protobuf-net return type List<t> is null

I'm trying to config my service to use protobuf-net. The methods that return T[] and T works but List is returning null. Is it supported?
Also, how can i tell that it is really using protobuf for during serialization. I tried to use fiddler but i can't really tell the difference.
public interface IOrdersService
{
[OperationContract(IsOneWay = false)]
OrderDTO[] GetAllOrders();
[OperationContract(IsOneWay = false)]
List<OrderDTO> GetAllOrders2();
[OperationContract(IsOneWay = false)]
OrderDTO GetOrder();
}
public class OrdersClient : IOrdersServiceCallback
{
private OrdersServiceClient client;
public OrdersClient()
{
}
public void Init()
{
InstanceContext site = new InstanceContext(null, new OrdersClient());
client = new OrdersServiceClient(site);
}
public void Start()
{
OrderDTO[] orders = client.GetAllOrders();
OrderDTO[] orders2 = client.GetAllOrders2();
OrderDTO dto = client.GetOrder();
Console.WriteLine(dto);
}
}
web config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="OrdersServer.OrdersService">
<endpoint address=""
binding="wsDualHttpBinding"
bindingConfiguration="Binding1"
contract="OrdersServer.IOrdersService"
behaviorConfiguration="ProtoBufBehavior" />
</service>
</services>
<bindings>
<wsDualHttpBinding>
<binding name="Binding1">
<!-- Binding property values can be modified here. -->
<security mode="None"/>
</binding>
</wsDualHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ProtoBufBehavior">
<protobuf />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<!--add binding="basicHttpsBinding" scheme="https" />-->
<add scheme="http" binding="wsDualHttpBinding"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<extensions>
<behaviorExtensions>
<add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.640, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
client config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IOrdersService" clientBaseAddress="http://localhost:8000/whatisthis/">
<security mode="None" />
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost.fiddler:49522/OrdersService.svc"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IOrdersService"
contract="OrdersServiceReference.IOrdersService" name="WSDualHttpBinding_IOrdersService"
behaviorConfiguration="proto"/>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="proto">
<protobuf />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.640, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>

Configuring wcf rest services in web.config

Where in the web.config should the following blocks of code go for a WCF RESTful service?
<endpoint address="" binding="webHttpBinding"contract="Wcf_Test.IMyService"
behaviorConfiguration="httpEndpointBehavour">
<identity>
<dns value="localhost"/>
<Identity>
</endpoint>
and
<behaviors>
<serviceBehaviors>
<behavior name="httpBehaviour"> <serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
and
<endpointBehaviors>
<behavior name="httpEndpointBehavour">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
In order to configure a WCF REST service, you need a few things in your web.config file
1) Declare your service and its endpoint
<services>
<service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService"
behaviorConfiguration="webHttp"/>
</service>
</services>
Service name will be [project name].[service name]
Behavior configuration will be same name as the behavior you declare in the next step
Binding must be webHttpBinding because you want it as REST. If you want SOAP, you declare as basicHttpBinding
Contract is the [project name].[interface name]
Behavior configuration in the endpoint will be the name you declare in next step
2) Declare the service behavior (usually default)
<behavior name="ServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
Behavior name can be anything, but it will be used to match BehaviorConfiguration you declared in step 1
Leave the rest alone
3) Declare your endpoint behavior
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
Behavior name can be anything, but it will be used to match the behaviorConfiguration in endpoint.
In the end, this is what the web.config should look like for a simple REST service:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService"
behaviorConfiguration="webHttp"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
for the rest type using WCFservice
<configuration>
<system.serviceModel>
<services>
<service>
<--
"place the first code snippet here "
it will contain the endpoint details
for WCFrestfulServices it will have 'A' ,'B' and 'C'
that is address, binding and contract
-->
</service>
</services>
<behaviors>
<servicebehaviours>
<--
"place the second code snippet"
the name of the behavior should be the same to that of the
behavior configuration attribute value of service tag
-->
</servicebehaviours>
<endpointBehaviors>
<--
"place your third code snippet"
the name of the behavior should be the same to that of the
behavior configuration attribute value of endpoint tag
-->
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Web config changes.
<system.serviceModel>
<services>
<service name="WcfService1.Service1">
<endpoint address="" behaviorConfiguration="restbehavior" binding="webHttpBinding" bindingConfiguration=""
contract ="WcfService1.IBookService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost/bookservice"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Interface: we have to use WebGet for httpGet / WebInvoke for HttpPost & Put & Delete.
[ServiceContract]
public interface IBookService
{
[OperationContract]
[WebGet]
List<BOOK> GetBooksList();
[OperationContract]
[WebGet(UriTemplate = "Book/{id}")]
BOOK GetBookById(string id);
[OperationContract]
[WebInvoke(UriTemplate = "AddBook/{name}")]
void AddBook(string name);
}
Ref : https://www.codeproject.com/Articles/571813/A-Beginners-Tutorial-on-Creating-WCF-REST-Services

WCF Service request and parameters sent via URL

I'm creating a WCF service (.net 4.5 - Visual Studio 2012) and I need to be abble to call my service operations and pass the right parameters via URL.
What do I need to change to allow this?
When I use the following URL: ".../MyService.svc/GetData?value=2", I get HTTP/1.1 400 Bad Request.
Here is my 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 behaviorConfiguration="ofertaServiceBehavior" name="Oferta" >
<endpoint binding="webHttpBinding" bindingConfiguration="" contract="GeoOfertas.IOfertaService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ofertaServiceBehavior">
<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"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Problem solved with the following 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 behaviorConfiguration="ofertaServiceBehavior" name="GeoOfertas.OfertaService">
<endpoint behaviorConfiguration="webBehavior" binding="webHttpBinding"
bindingConfiguration="" contract="GeoOfertas.IOfertaService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ofertaServiceBehavior">
<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"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>

WCF REST service results in 404

I have read most of the WCF REST 404 posts but none that helped me...
I have built a WCF REST service successfully. However, now it is causing issues. I tried just creating a sample WCF REST service and I cannot get this to work without using the .SVC.
This is what I have in my code
[ServiceContract]
public interface IService1
{
[WebGet(UriTemplate="data")]
[OperationContract]
string GetData();
}
public class Service1 : IService1
{
public string GetData()
{
return "1";
}
}
and this is what I have in my web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="Default" name="RESTService.Service1">
<endpoint address="http://mydomain:8888/Service1.svc"
binding="webHttpBinding"
contract="RESTService.IService1"
behaviorConfiguration="Web" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
When I go to http://mydomain:8888/data is responds in 404. Any ideas why it is not hitting the GetData() function? The following URL works if I remove the Endpoint address
http://mydomain:8888/Service1.svc/data
However, I want the address to be http://mydomain:8888/data
You can try leaving your web config in the version you have working correctly (with the svc in the path) and add route table entries during application start in the global asax file
check this out for more info
http://msdn.microsoft.com/en-us/library/cc668177.aspx