Reputation: 6192
Im trying to create my first WCF restful service. I was reading this page: http://www.colossaltechnologies.com/vd/index.php?option=com_content&view=article&id=128%3Awcf-rest-example&Itemid=101 And tried to copy the code.
However, I dont have my WCF service in a separate application, but added it to my existing web application project. The service will not be needed outside the scope of my current project. What I want to achieve is that this rest service can be called from mobile apps (iOS/Android/Windows Phone) that need to be developed.
I noticed this step in the above tutorial: "Create a Host for the Service" Now, what I wonder is: do really need to run that code to start the host, and if so: when and how do I run it on a production server?
It seems that I indeed need to do something with that host start, because now when I go to: www.test.com/api/job/bob (I also tried www.test.com/job/bob). I get a 404.
Below my code, I hope anyone is able to see what Im doing wrong. Am I not using the correct URI? What did I miss?
Iweddingservice.vb
Imports System.ServiceModel
Imports System.Web
Imports System.IO
Imports System.Runtime.Remoting.Activation
Imports System.Collections.Generic
Namespace RestService
<ServiceContract()>
Public Interface Iweddingservice
<OperationContract()> _
<Web.WebGet(UriTemplate:="job/{name}")> _
Function DoJob(name As String) As String
End Interface
End Namespace
** weddingservice.svc.vb **
Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.IO
Imports System.ServiceModel.Activation
Imports System.Web.Script.Serialization
Imports System.Collections.Generic
Imports System.Xml
Imports System.Net
Namespace RestService
Public Class weddingservice
Implements Iweddingservice
Public Function DoJob(name As String) As String Implements Iweddingservice.DoJob
Return String.Format("Hello, {0}", name)
End Function
End Class
End Namespace
** web.config **
<rewrite>
<rules>
<rule name="api access 2">
<match url="^api/job/$" />
<action type="Rewrite" url="weddingservice.svc/api/job/" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<system.serviceModel>
<services>
<service name="RestService.weddingservice">
<host>
<baseAddresses>
<add baseAddress="http://www.test.com/api"/>
</baseAddresses>
</host>
<endpoint binding="webHttpBinding" contract="RestService.Iweddingservice" />
</service>
</services>
</system.serviceModel>
Upvotes: 0
Views: 680
Reputation: 49245
Are you trying hosting the service in IIS? If yes, then you cannot provide the address part - it will be implicit by the service file location. For example if your service file weddingservice.svc
is in the root location then use configuration such as
<system.serviceModel>
<services>
<service name="WeddingService">
<endpoint binding="webHttpBinding" contract="RestService.Iweddingservice"
behaviorConfiguration="webHttp"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Now, you service will be browsable at say - http://localhost/weddingservice.svc
and your method at http://localhost/weddingservice.svc/job
. Instead of localhost, you may have whatever host address your web site supports (e.g. www.xyz.com). For eliminating .svc
extension, you need to use re-write rule or ASp.NET routing.
Finally, if you are not hosting the service in IIS then check your base address - is DNS etc set up correctly for the same. Try the URL w/o any re-writing e.g. www.test.com/weddingservice.svc/job
.
EDIT: It appears that you are still stuck on this. I would suggest that you follow below step-by-step tutorials:
This one should get you started with: http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
This is a bit detailed and cover quite a few things: http://blogs.msdn.com/b/endpoint/archive/2010/01/07/getting-started-with-wcf-webhttp-services-in-net-4.aspx
Here are other useful links:
http://msdn.microsoft.com/en-us/library/bb412172.aspx
http://msdn.microsoft.com/en-us/library/dd203052.aspx
http://msdn.microsoft.com/en-us/library/dd699772.aspx (more samples)
Upvotes: 1
Reputation: 11
Just keep following the ABC of WCF. How are you planning to host your service? If you are planning to host the service using IIS the base address tag will be ignored
Upvotes: 0