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:
- 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 intoLogin
object. - 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
intoAPIOrder
. I know that I could assign field values one by one but is there a better method? Also, please notice that eachOrder
object can have one or more Item objects inside which also should be deserialized into appropriate objects inAPIOrder
object. Probably I would have to: (1) serializeOrder
into XML, (2) transform XML, (3) deserialize XML intoAPIOrder
. Could someone show me code which realizes this task? - 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
haveLogIn
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