Ask a questionAsk a question
 

AnswerWCF and Json 404 Error

  • Tuesday, November 03, 2009 9:22 PMdev14 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm trying to call the WCF service(Different machine) from my local machine but no luck, returns 404
    there may be simple things missing but as I'm new to this couldn't figure out.

    1. WCFService.svc

    namespace WCFTest
    {
       
        public class WCFService : IWCFService
        {
            public string HelloWorld(string strName)
            {
                return "Your Entered:"+strName;
            }
        }
    }

    2. WCFService.svc.cs

    namespace WCFTest
    {
       
        [ScriptService]
        public class WCFService : IWCFService
        {
            public string HelloWorld(string strName)
            {
                return "Your Entered:"+strName;
            }
        }
    }

    3. webconfig

        <system.serviceModel>
            <bindings>
                <webHttpBinding>
                    <binding name="jsonBinding" maxReceivedMessageSize="2147483647">
                    </binding>
                </webHttpBinding>
            </bindings>
            <services>
                <service behaviorConfiguration="WCFTest.WCFServiceBehavior" name="WCFTest.WCFService">
                    <endpoint address="http://server:portnumber/WCFService.svc" binding="webHttpBinding" bindingConfiguration="jsonBinding" behaviorConfiguration="jsonBehavior" name="myservice" contract="WCFTest.IWCFService">
                    </endpoint>
                </service>
            </services>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="WCFTest.WCFServiceBehavior">
                        <serviceMetadata httpGetEnabled="true"/>
                        <serviceDebug includeExceptionDetailInFaults="false"/>
                    </behavior>
                </serviceBehaviors>
                <endpointBehaviors>
                    <behavior name="jsonBehavior">
                        <enableWebScript/>
                    </behavior>
                </endpointBehaviors>
            </behaviors>
        </system.serviceModel>


    ** The service is deployed on the server.

    4. Created Defalut.aspx on my local machine

    <body onload="Initialize();">
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            <Services>
                <asp:ServiceReference Path="http://server:portnumber/WCFService.svc"/>
            </Services>
            <Scripts>
                <asp:ScriptReference Path="~/test.js"/>
            </Scripts>
        </asp:ScriptManager>
        <div>
        <label>Greetings:</label><asp:Label ID="lblShow" runat="server"></asp:Label>
        </div>
        </form>
    </body>

    5. Created test.js on my local machine

    function Initialize() {
        var proxy = new WCFTest.IWCFService();
        proxy.HelloWorld("YOurName",OnSuccess, OnFailure);
    }

    function OnSuccess(result) {
        document.getElementById('lblShow').innerHTML = result;
    }

    function OnFailure(result) {
        alert('Failed');
    }

    6. Result: "The server method 'HelloWorld' failed."

    Using Firebug:

    **Response Headers

    Server    ASP.NET Development Server/9.0.0.0
    Date    Tue, 03 Nov 2009 20:48:12 GMT
    Content-Length    1210
    Connection    Close


    **Request Headers

    Host    localhost:4007
    User-Agent    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 (.NET CLR 3.5.30729)
    Accept    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language    en-us,en;q=0.5
    Accept-Encoding    gzip,deflate
    Accept-Charset    ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive    300
    Connection    keep-alive
    Content-Type    application/json; charset=utf-8
    Referer    http://localhost:4007/MyTest/Default.aspx
    Content-Length    20

    **POST
    {"strName":"YOurName"}

    **Response

    <html>
        <head>
            <title>Not Found</title>
            <style>
                body {font-family:"Verdana";font-weight:normal;font-size: 8pt;color:black;}
                p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
                b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
                h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
                h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
                pre {font-family:"Lucida Console";font-size: 8pt}
                .marker {font-weight: bold; color: black;text-decoration: none;}
                .version {color: gray;}
                .error {margin-bottom: 10px;}
                .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
            </style>
        </head>
        <body bgcolor="white">

                <span><h1>Server Error in '/MyTest' Application.<hr width=100% size=1 color=silver></h1>

                <h2> <i>HTTP Error 404 - Not Found.</i> </h2></span>

                <hr width=100% size=1 color=silver>

                <b>Version Information:</b>&nbsp;ASP.NET Development Server 9.0.0.0

                </font>

        </body>
    </html>


    The URL it is trying to hit is

    POST http://localhost:4007/WCFService.svc/HelloWorld

    thanks.
















Answers

  • Monday, November 09, 2009 8:16 AMRiquel_DongModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi ,

    The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server but the server could not find what was requested.

    Please have a look at this code sample about one REST Model WCF service for your reference. ScriptService should be used in previous Web Service technology.
    http://msdn.microsoft.com/en-us/library/bb412178.aspx

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Monday, November 09, 2009 8:16 AMRiquel_DongModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi ,

    The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server but the server could not find what was requested.

    Please have a look at this code sample about one REST Model WCF service for your reference. ScriptService should be used in previous Web Service technology.
    http://msdn.microsoft.com/en-us/library/bb412178.aspx

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.