views:

413

answers:

1

Hello folks.

I am trying to create a simple web service in .NET but my knowledge is limited on that subject.

Here is the code of this web service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    /// 
    class Login
    {
        public string User;
        public string Password;
        public string Database;
    }

    public class Item
    {
        public string Name;
        public float Price;
        public float Quanity;
    }

    public class WebOrder
    {
        public int Number;
        public int ClientID;
        public Item[] Items;
    }

    public class ExternalAPIItem
    {
        public string APIItemName;
        public float APIItemPrice;
        public float APIItemQuanity;
    }

    public class ExternalAPIOrder
    {
        public int APIOrderNumber;
        public int APIOrderClientID;
        public ExternalAPIItem[] APIOrderItems;
    }

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string MakeOrder(WebOrder Order)
        {
            Login DBLogin;
            ExternalAPIOrder APIOrder;
            DBLogin = new Login();
            DBLogin.User = "SomeUser";
            DBLogin.Password = "SomePassword";
            DBLogin.Database = "SomeDatabase";

            //try to log via external API

            //ExternalAPI.LogIn(DBLogin)   <- is this possible to log call to this method?

            APIOrder = new ExternalAPIOrder();

            //how to deserialize Order object to APIOrder object which is instance of different class?

            //ExternalAPI.MakeOrder(APIOrder)   <- is this possible to log call to this method?

            return "Order complete";
        }
    }
}

The purpose of this web service is to receive some order from Internet and realize that order via external API on the server. It looks like this

Some website(s) - > WebService1 - > ExternalAPI - > Underying database

I have few questions:

  1. Firstly I need to keep some configuration data which will be loaded at the start of web service instance. My first idea was to keep it in the XML file and deserialize that file into Login object. However, there is this Web.config file already bundled to the service. Is this a good place to keep such things? If so than could you show me how to (a) store my data in that fie (b) retrieve stored data from the file into Login object.
  2. I've read few articles about serialization and I do know how to serialize some basic object to XML. But what if I would like to transfer data from one object into another? On the above example I have shown that I need to transfer data from Order into APIOrder. I know that I could assign field values one by one but is there a better method? Also, please notice that each Order object can have one or more Item objects inside which also should be deserialized into appropriate objects in APIOrder object. Probably I would have to: (1) serialize Order into XML, (2) transform XML, (3) deserialize XML into APIOrder. Could someone show me code which realizes this task?
  3. Is there a possibility to log into some file an execution of given methods in the way that I could convert executed method name into string? For example: ExternalAPI have LogIn method. Can I create some method that can take as a parameter any method from ExternalAPI class (with method parameters) and firstly convert that method name into string and then execute that method?

I don't know if this have a meaning but I am forced to use assembly where ExternalAPI class is defined. So in my real WebService there won't be ExternalAPIItem and ExternalAPIOrder defined by me. They are already defined in the assembly namespace.

Thanks for your time

+1  A: 

To answer your second question, I would avoid getting fancy with converting Order to XML, running XSLT on that, and deserializing. Keep it simple. Have a constructor on APIOrder that accepts an Order parameter and copy over the public properties. Delegate the LineItem (or whatever) child objects to an APILineItem constructor. Easy to read, easy to maintain, easy to debug, easy to test. If you want to avoid coupling APIOrder to Order, write a converter class that does this.

TrueWill