none
Unable to invoke WCF REST methods RRS feed

  • Question

  • Hi Everyone,

    This is my first attempt at creating a REST based Web Service using WCF .NET 3.5. So far i've managed to implement a couple of simple methods and host the Web Service in IIS 7.  I've been using a tool called SOAP UI in order to be able to view the Web Services resources (it has the capacity for both SOAP and REST)  But i'm unable to commuincate with the Web Service.

    Here is the code for my Web Service...

    the Interface

    namespace CMDBservice
    {
        // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
        [ServiceContract]
        
        public interface IService1
        {        
            [WebGet(
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml)]
            DataSet getPersonDetails();       

            [OperationContract]
            [WebGet(
                BodyStyle = WebMessageBodyStyle.Bare,
                ResponseFormat = WebMessageFormat.Xml,
                RequestFormat = WebMessageFormat.Xml,
                UriTemplate = "/hasPerson/{personCheck}")]
            bool hasPerson(string personCheck);        

            [OperationContract]
            [WebGet(
                BodyStyle = WebMessageBodyStyle.Bare,
                ResponseFormat = WebMessageFormat.Xml,
                RequestFormat = WebMessageFormat.Xml,
                UriTemplate = "/addNumber/{a}, {b}")]
            int addNumber(int a, int b);

            [OperationContract]
            [WebInvoke(
                Method = "GET",
                BodyStyle = WebMessageBodyStyle.Bare,
                ResponseFormat = WebMessageFormat.Xml,
                RequestFormat = WebMessageFormat.Xml,
                UriTemplate = "/testGet/")]
            string testGet();

            // TODO: Add your service operations here
        }
        
    }

    Method Definitions

    namespace CMDBservice
    {
        // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
        public class Service1 : IService1
        {        
                    
            public bool hasPerson(string value)
            {
                if (value == null)
                {
                    return false;
                }
                return true;
            }                 

            public DataSet getPersonDetails()
            {

                string dbConCN3041_PersonDetails = "server=Kevin-PC;database=CN3041; User Id=sa; Password=zepplin123";
                String query = "SELECT * FROM PersonDetails";

                SqlConnection conn = new SqlConnection(dbConCN3041_PersonDetails);

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = query;

                conn.Open();

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet dsPersonDetails = new DataSet();
                adapter.Fill(dsPersonDetails);

                dsPersonDetails.DataSetName = "dsPersonDetails";
                dsPersonDetails.Tables[0].TableName = "tblPersonDetails";

                conn.Close();
                conn.Dispose();
                conn = null;

                return dsPersonDetails;

            }

            public int addNumber(int numA, int numB)
            {
                return numA + numB;
            }

            public string testGet()
            {
                return "Hello it worked";
            }
        }
    }

    Web Config:

    <system.serviceModel>
    <services>
    <service name="CMDBservice.Service1" behaviorConfiguration="CMDBservice.Service1Behavior">        
    <!-- Service Endpoints -->
    <endpoint address="http://kevin-pc/CMDBService/Service1.svc" binding="webHttpBinding" contract="CMDBservice.IService1">
              
    <!-- 
                  Upon deployment, the following identity element should be removed or replaced to reflect the 
                  identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
                  automatically.
              -->
    <!--<identity>
    <dns value="localhost"/>
    </identity>-->
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
    </services>
    <behaviors>
          <endpointBehaviors>
            <behavior name="webHttp"><webHttp/>
            </behavior>
          </endpointBehaviors>
    <serviceBehaviors>
    <behavior name="CMDBservice.Service1Behavior">
    <!-- 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>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/></system.serviceModel>
    </configuration>

    The final goal is to be able to consume this RESTful Web Service from a Mobile Application.

    Any help would be appreciated!

    Thank you in advance

    Kevin


    Thursday, June 11, 2009 6:16 PM

Answers

  • Hi Kevin,

    first of all, you should ditch the absolute endpoint address from the config file, always use relative addresses when hosting under IIS. One more thing, the addNumber method will fail - you should change the UriTemplate to something like this: "addNumber?a={a}&b={b}".

    Ok, let's imagine that the service is running and that the endpoint's address is represented with an empty string. You can trigger the addNumber method like this (using the browser):

    http://localhost/TheNameOfTheIISAppFolder/TheNameOfTheSvcFile.svc/addNumber?a={1}&b={2}

    One more thing, you forgot to assign the behavior configuration to the endpoint (behaviorConfiguration="webHttp").

    Did this help?

    Regards,
    John
    Friday, June 12, 2009 1:30 AM

All replies

  • What do you mean you are unable to communicate with the service? What error do you get?

    What URI are you using to hit it?


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    Thursday, June 11, 2009 7:01 PM
    Moderator
  • Hi thanks for replying,

    When the SOAPUI tool asks for the URL i enter:  http://localhost/CMDBService/Service1.svc

    I'm not sure if this is correct.

    other variations have been:

    Http://localhost/CMDBService/Service1.svc/getPersonDetails
    Http://localhost.CMDBService/Service1.svc?wsdl

    I've had no joy in each case.

    I'm asking, how would I make the call to the Web Service? in this case...how would I make the call to the getPersonsDetails method?

    Thank you

    Kevin
    Thursday, June 11, 2009 8:32 PM
  • Hi Kevin,

    first of all, you should ditch the absolute endpoint address from the config file, always use relative addresses when hosting under IIS. One more thing, the addNumber method will fail - you should change the UriTemplate to something like this: "addNumber?a={a}&b={b}".

    Ok, let's imagine that the service is running and that the endpoint's address is represented with an empty string. You can trigger the addNumber method like this (using the browser):

    http://localhost/TheNameOfTheIISAppFolder/TheNameOfTheSvcFile.svc/addNumber?a={1}&b={2}

    One more thing, you forgot to assign the behavior configuration to the endpoint (behaviorConfiguration="webHttp").

    Did this help?

    Regards,
    John
    Friday, June 12, 2009 1:30 AM
  • Hi slatkovi,

    Thank you, this was a great help!!!

    I am now able to call all methods and view the results in a browser.

    I'm actually shocked I didnt notice them!...

    Thanks a bunch

    Regards

    Kevin
    Friday, June 12, 2009 9:48 AM