none
WCF, Client, Hosting and XML Creation confusion!!! RRS feed

  • Question

  • Hello to all

    I'm pretty new to this services thing. I was asked at work to build a web service in order to have a web page that a user could access, enter a couple of data (Agent Number, phone number and coordinates)  and the web service should return an XML which will allow some coworkers in other group to fill a new DB. I'm just confused in how all of this work. 

    I did my homework though. I get the coordinates of the user using an HTML 5 implementation. I could implement HTML easily on a ASP web app. I also created a WCF Service following this tutorial:

    WCF Service creation tutorial

    (Only the corresponding endpoint was used of course)

    Now, here's come my confusion... in what part of the work is where actually I have the corresponding XML needed for my guys?? I mean... I created a WCF service and it works hosted on a console app.

    Here's a link to my SkyDrive where you can download my simple WCF solution and projects: 

    Skydrive link

    As you see, I have a simple method that returns the latitude. Very simple... I just want to know how this works. 

    Again, my doubts comes in this part...

    I need to create a client?... maybe an ASP web application and through it, calling my client.method thing, I would pass it the variable which contains the latitude no? (When I get it from the user of course) It would work, considering that the service method it's just returning it's own parameter. 

    Then, when it actually returns it... that's when corresponding XML is created?? So I actually create the corresponding XML file filled with information when actually the user consume the service via the ASP web app? 

    I'm in the correct path? Or I'm terribly misguided? Please! A little bit of help with these concepts will be REALLY appreciated.


    - Samy
    • Edited by SamyCode Friday, January 24, 2014 3:40 AM
    Friday, January 24, 2014 3:38 AM

Answers

  • I don't have much experience with WCF specifically, but client and server are two separate things. If your task is to build a service, then do that. Create a service that receives the parameters/data it needs, then return the result of its processing to the client.

    You don't need to know what the client is or how it is implemented. It could be a web form, or someone building a Request in Fiddler or a Java app - whatever. You just have to assume whatever has sent your service a request knows what to do with the response.


    Bob - www.ContrivedExample.com

    Friday, January 24, 2014 5:32 PM
  • If this form you need to create to collect the data is the only way your webservice will be accessed, then you could simply do this whole project as a simple aspnet web applicaton.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Data.aspx.cs" Inherits="Data" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Label runat="server" Text="Agent Number"></asp:Label>
            <asp:TextBox runat="server" ID="AgentNumberTextBox"></asp:TextBox>
            <br />
            <asp:Label runat="server" Text="Phone"></asp:Label>
            <asp:TextBox runat="server" ID="PhoneTextBox"></asp:TextBox>
            <br />
            <asp:Label runat="server" Text="Coords"></asp:Label>
            <asp:TextBox runat="server" ID="CoordsTextBox"></asp:TextBox>
            <br />
            <asp:Button runat="server" ID="submitButton" OnClick="submitButton_Click" Text="Submit" />
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Xml.Linq;
    
    public partial class Data : System.Web.UI.Page
    {
        protected void submitButton_Click(object sender, EventArgs e)
        {
            XDocument doc = new XDocument(
                new XElement("root",
               new XElement("AgentNumber", AgentNumberTextBox.Text),
               new XElement("Phone", PhoneTextBox.Text),
               new XElement("Coords", CoordsTextBox.Text)
               ));
    
            Response.Clear();
            Response.ContentType = "text/xml";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            doc.Save(Response.Output);
            Response.End();
        }
    }

    Output becomes:

    <?xml version="1.0" encoding="UTF-8"?>
    -<root>
    <AgentNumber>123213-54</AgentNumber>
    <Phone>123-456-7890</Phone>
    <Coords>152,123</Coords>
    </root>
    You will of course want to validate the data and do some error handling.



    Bob - www.ContrivedExample.com

    Sunday, January 26, 2014 7:36 PM

All replies

  • So no answer to this one? I will really appreciate if somebody clarify me some of these points :/
    • Edited by SamyCode Friday, January 24, 2014 5:15 PM
    Friday, January 24, 2014 5:14 PM
  • I don't have much experience with WCF specifically, but client and server are two separate things. If your task is to build a service, then do that. Create a service that receives the parameters/data it needs, then return the result of its processing to the client.

    You don't need to know what the client is or how it is implemented. It could be a web form, or someone building a Request in Fiddler or a Java app - whatever. You just have to assume whatever has sent your service a request knows what to do with the response.


    Bob - www.ContrivedExample.com

    Friday, January 24, 2014 5:32 PM
  • Hi Bob. First of all, thank you so much for always reply to the posts.

    When I mention that the service is hosted using a console application, is because I have a console app project in order for the service to be hosted in ny own machine (localhost) so I can test it.

    I almost get you on everything but here is where I get confused:

    You mention that I should not care how the web service will be consumed. The problem is that my VC told me to create like a web form where the user can enter some info and the web service should get that info and send an XML of it. That's what get me into confusion

    Should that web form be an ASP web application? I get the info needed by the user, the webservice get it with it's methods and.... damn! hehe well, you mention that, " then return the result of its processing to the client". but.... to the client? The client need that info for what? I need to get info from the user in order to create the XML, but then, I'm maybe wrong and I should get that info in a ASP Web App, but that will not be exactly the tool used to consume the service right?

    Argh! I wish I can explain my confusion better. I just don't understand where to exactly get the info that I need for the service to fill it's XML, like the agent number or his coordinates for example.

    Any other help will be greatly appreciated!!!

    - Sam




    • Edited by SamyCode Friday, January 24, 2014 8:28 PM
    Friday, January 24, 2014 7:35 PM
  • If this form you need to create to collect the data is the only way your webservice will be accessed, then you could simply do this whole project as a simple aspnet web applicaton.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Data.aspx.cs" Inherits="Data" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Label runat="server" Text="Agent Number"></asp:Label>
            <asp:TextBox runat="server" ID="AgentNumberTextBox"></asp:TextBox>
            <br />
            <asp:Label runat="server" Text="Phone"></asp:Label>
            <asp:TextBox runat="server" ID="PhoneTextBox"></asp:TextBox>
            <br />
            <asp:Label runat="server" Text="Coords"></asp:Label>
            <asp:TextBox runat="server" ID="CoordsTextBox"></asp:TextBox>
            <br />
            <asp:Button runat="server" ID="submitButton" OnClick="submitButton_Click" Text="Submit" />
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Xml.Linq;
    
    public partial class Data : System.Web.UI.Page
    {
        protected void submitButton_Click(object sender, EventArgs e)
        {
            XDocument doc = new XDocument(
                new XElement("root",
               new XElement("AgentNumber", AgentNumberTextBox.Text),
               new XElement("Phone", PhoneTextBox.Text),
               new XElement("Coords", CoordsTextBox.Text)
               ));
    
            Response.Clear();
            Response.ContentType = "text/xml";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            doc.Save(Response.Output);
            Response.End();
        }
    }

    Output becomes:

    <?xml version="1.0" encoding="UTF-8"?>
    -<root>
    <AgentNumber>123213-54</AgentNumber>
    <Phone>123-456-7890</Phone>
    <Coords>152,123</Coords>
    </root>
    You will of course want to validate the data and do some error handling.



    Bob - www.ContrivedExample.com

    Sunday, January 26, 2014 7:36 PM
  • Molku, thank you so much for your response.

    Now I'm a little more clear of the concept. I read your message an I see that I could very well do this as a whole ASP.net app. Thank you very much for you advice.

    Now I have two questions... hopefully the last ones.

    I got the thing to work using webservice. I kept reading and I have a clearer mind now of the concept. My WS just return what I pass to it and, as I enabled the logs files via WCF Configuration, I can see the response of the service... and seems to be what I need.

    My question is... considering that this data will be passed to a SQL database, the approach that you gave it's ok? I mean, it would be easier to consume my Web Service or to pass that file.xml to a DB?

    Thank you!!


    • Edited by SamyCode Monday, January 27, 2014 2:06 PM
    Monday, January 27, 2014 2:05 PM
  • I'm not really sure exactly what your goals or requirements are. If that xml is just going to end up in a database you may as well just do that from the service instead of returning it to a client that will then have to insert it into the database.

    But maybe I misunderstand what you are asking.


    Bob - www.ContrivedExample.com

    Wednesday, January 29, 2014 1:27 PM