in
Forums
Blogs
Files
Devexpress.Com
Client Center
Support Center
DevExpress Channel

Using XPO inside a web method

Last post 9/25/2007 4:54 AM by Dan (Developer Express). 6 replies.
Page 1 of 1 (7 items)
Sort Posts:
Previous Next
  • 9/6/2007 6:31 AM

    Using XPO inside a web method

    Hi,

    I want to use XPO inside a web method of a web service. I think I have to create the DataLayer inside the web method because the web method is stateless. Is this correct? Also, do I have to be careful how I create the DataLayer or is it the same as in a WinForm application?

     
    Thanks,

    Petros
     

  • 9/7/2007 3:13 AM In reply to

    Re: Using XPO inside a web method

    This is some dirty hack code we using internally:

        [WebMethod]
        public void SubmitMonitorMessage(AgentMessageType messageType, string client, string service, string message, string info) {
            session = new UnitOfWork();
            session.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringForDevelopment"].ConnectionString;
    
            AgentMessage msg = new AgentMessage(session);
            msg.MessageType = messageType;
            msg.Client = client;
            msg.Service = service;
            msg.When = DateTime.Now;
            msg.ExpiresOn = msg.When.AddMinutes(AgentMessage.MonitorIntervalMin);
            msg.Message = message;
            msg.Info = info;
            session.CommitChanges();
        }
    
    
    This example assumes that XAF security is not used and web service is in separate web application. If you add your webservice as a part of XAF application and want to use its security features, code would be different.

    Filed under: ,
  • 9/7/2007 4:09 AM In reply to

    Re: Using XPO inside a web method

    Hello,
     
    There are several articles about the Xpo and Web services:
     
     
    Hope this explains all the things you need.

    --
    Thanks, Dan
    R&D, .NET Team Developer Express Inc.
     
    PS. If you wish to receive direct assistance from our Support Team, use
    Support Center at http://www.devexpress.com/Support/Center
  • 9/19/2007 8:36 AM In reply to

    Re: Using XPO inside a web method

    I am sorry I marked this post as answered and now I mark it unanswered again, but I just realized I was not looking exactly what article "How to use XPO with a Web Service" describes. Let me explain. I am not looking to use a web service as a gate that will enable me to directly connect a CF client using XPO. I want to call a regular web method from a CF client that returns a simple type. But inside that web method I want to connect to a local database using XPO. I have managed to do this, but it seems the way I am creating the DataLayer inside the web method causes problems after a while with pool connections. Now I know this might be an entirely "How to connect to the database inside a web service method" subject, but I would appreciate it if anyone with web services experience sheds some light.

    My Web Service solution doesn't have a Global.asax file. The code for my simple web method is this:

    [WebMethod]

    public string GetDescription(int oid)

    {
      string database = Settings.Default.Database;
      XpoDefault.DataLayer = XpoDefault.GetDataLayer(MSSqlConnectionProvider.GetConnectionString(database, "Scrapyard"),
      AutoCreateOption.SchemaAlreadyExists);

      using (Session session = new Session())

      {

        Car car = session.GetObjectByKey<Car>(oid);

        if (car != null)

        {

          return car.Description;
        }

        return string.Empty;
      }
    }

    This code means that each time the web method is called, a new datalayer is created as well as a connection? The problem is that after several calls, I get an exception that no more connections can be created because the pool is full. What should I do? I have tried adding a Global.asax file and create the DataLayer in the Application_Start event, but it stopped working completely and my web methods tried to create the default connection. Where should I create the connection and in what way to comply with web services logic?

    Thank you
    Petros

  • 9/19/2007 9:45 AM In reply to

    Re: Using XPO inside a web method

    It turns out that I should close the connections made by XPO. Do you know how to explicitly close a connection using XPO?
  • 9/21/2007 6:54 AM In reply to

    Re: Using XPO inside a web method

    Ok, here is the answer. I should create a Global.asax and enter the following code:

    protected void Application_Start(object sender, EventArgs e)
    {
      string database = Settings.Default.Database;
      string schema = Settings.Default.Schema;
      XpoDefault.DataLayer = XpoDefault.GetDataLayer(MSSqlConnectionProvider.GetConnectionString(database, schema),
        AutoCreateOption.SchemaAlreadyExists);
    }

    This only creates one connection, not several connections per web method call. 

  • 9/25/2007 4:54 AM In reply to

    Re: Using XPO inside a web method

    Hello Petros,
     
      It seems there is some mess with the Session, DataLayer, DBConnection and other related classes. It's hard to understand how these objects are related to each other, and what is their life cycle at a glance.
      To clarify this issue, I will enumerate and comment upon classes used by XPO starting from reading the information from a database and ending with objects creation:
      ....
     
      I have continued in the "XPO objects' life cycle" thread.
     
    --
    Thanks, Dan
    R&D, .NET Team Developer Express Inc.
     
    PS. If you wish to receive direct assistance from our Support Team, use
    Support Center at http://www.devexpress.com/Support/Center
Page 1 of 1 (7 items)
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED