• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 715
  • Last Modified:

XML File exchange using WCF

Hi have read a lot of tutorials and threads in EE about WCF but I am still not close to finding a solution to my problem :'-(

My Problem is as follows....

I have a client server application.

The server is a ASP.NET Web application developed using VS 2008 and C#

the client is a winform app deployed at various client locations...

The ASP.NET web app keeps the latest copy of an xml file which is updated from time to time. As soon as this file is Modified / updated I need to notify the clients about it and the clients should fetch the latest copy of the file from the server...

If someone could provide me a working example of this model I would be indeed very grateful

I have been banging my head around this for a while with no luck :-(
0
2ooth
Asked:
2ooth
2 Solutions
 
systanCommented:
Show your code snippet of asp.net server side and the windows form client side.
0
 
2oothAuthor Commented:
Hi,

Thank you for taking time to help me in my problem...


The code snippet for the Server side ASP.NET web pages and the client side windows forms are in the code section

The xml file that needs to be transported from the server to the client whenever this file is updated is attached in the file section


Please don't hesitate to ask, if you need any more information... I'm eager to get this done as soon as possible...


Thanking you in advance
 
////////////ASP.NET Server side code/////////////////////////////

protected void btnSave_Click(object sender, EventArgs e)
        {
            addNewTrial();
        }

        private void addNewTrial()
        {
            saveTrailName(txtTrialName.Text);
            if (trialId != 0)
            {
                saveTrialCriteia();
                generateXMLData(trialId);
            }
        }

        private static void generateXMLData(int ID)
        {
            using (var xmldata = new XMLData())
            {
                xmldata.generateXML(ID);
            }
        }


        internal void generateXML(int trialID)
        {
            using (var da = new DataAccess())
            {

                var dr = da.getTrialCriteria(trialID);
                while (dr.Read())
                {
                    // The index of the dr must match the column returned from the data reader 
                    // as well as the Get datatype must match the returned type
                    Data data = new Data() { TYPE = dr.GetString(0), ID = (int)dr[1], name = dr.GetString(2) };
                    // Add the new Data object to the collection
                    criterias.Add(data);
                }

                var query = (from c in criterias
                             group c by c.TYPE into types
                             select types).ToList();

                // Get the XML document to be modified
                XDocument doc = XDocument.Load(@"C:\ClinicalTrialsList.xml");

                // Create a new Trial node 
                var trial = query.Find(t => t.Key.ToUpper() == "TRIAL").FirstOrDefault();

                XElement xTrial = CreateTrialNode(trial.ID, trial.name);
                // Add the trial node to the end of the doc document
                doc.Root.Add(xTrial);

                // Create the diseases element and all its childrens  
                XElement disease = new XElement("diseases");
                foreach (var dis in query.Find(d => d.Key.ToUpper() == "DISEASES"))
                {
                    XElement d = new XElement("disease",
                        new XAttribute("ID", dis.ID),
                        new XElement("name", dis.name));
                    disease.Add(d);
                }
                // Add the disease to the xTrial
                xTrial.Add(disease);

                // Create the ethnicities element and all its childrens  
                XElement ethnicities = new XElement("ethnicities");
                foreach (var ethnicity in query.Find(d => d.Key.ToUpper() == "ETHNICITY"))
                {
                    XElement eth = new XElement("ethnicity",
                        new XAttribute("ID", ethnicity.ID),
                        new XElement("name", ethnicity.name));
                    ethnicities.Add(eth);
                }
                // Add the ethnicities to the trial
                xTrial.Add(ethnicities);
    
                // Save modified document to file system
                doc.Save(@"C:\ClinicalTrialsList.xml");
            }
        }

///////////////END of Server side code///////////////////////

///////////////Windows forms Client side code////////////////
namespace PatientMatchingLocalAgent
{
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            patientInfoWatcher.Path = @"D:\PROJECTS\ClinicalTrials\PatientMatchingLocalAgent\PatientRecord"
        }


        private void PatientInfoWatcherChanged(object sender, FileSystemEventArgs e)
        {
            XDocument clinicXDoc = XDocument.Load(@"D:\PROJECTS\ClinicalTrials\PatientMatchingLocalAgent\TrialRecord");
            List<Trial> trials = (from trial in clinicXDoc.Descendants("trial")
                                  select new Trial()
                                  {
                                      Id = int.Parse(trial.Attribute("ID").Value),
                                      Name = trial.Element("name").Value,
                                      Diseases =
                                          (from disease in trial.Descendants("disease")
                                           select new Disease()
                                           {
                                               Id = int.Parse(disease.Attribute("ID").Value),
                                               Name = disease.Element("name").Value,
                                           }

                                          ).ToList()
                                  }).ToList();

            XDocument patientXDoc = XDocument.Load(Settings.Default.WatchPath + "\\PatientInfo.xml");
            List<Patient> patients = (from patient in patientXDoc.Descendants("patient")
                                      select new Patient()
                                      {
                                          Name = patient.Element("name").Value,
                                          Diseases = (from disease in patient.Descendants("disease")
                                                      select new Disease()
                                                      {
                                                          Name = disease.Element("name").Value,
                                                      }

                                                  ).ToList()
                                      }).ToList();

            var intersectValues = from trial in trials
                                  from patient in patients
                                  from trialDisease in trial.Diseases
                                  from patientDisease in patient.Diseases
                                  where trialDisease.Name == patientDisease.Name
                                  select new
                                  {
                                      trial.Id,
                                      Disease = trialDisease.Name

                                  };

            foreach (var intersectValue in intersectValues)
            {
                Debug.WriteLine(intersectValue.Id + " " + intersectValue.Disease);
            }
        }

    }
}

class Trial
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Disease> Diseases { get; set; }
    }
public class Disease
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
class Patient
    {
        public string Name { get; set; }
        public List<Disease> Diseases { get; set; }
    }
}

////////////////Windows Client side code ends here/////////////////

Open in new window

ClinicalTrialsList.xml
0
 
faisallCommented:
What you may want to do is write a windows service (that always runs in the background) that communicates using WCF (a complety separate component from the website and client).  in this new service, add a FileSystemWatcher class (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx) and let that class notify the client anytime there is a change to the file.  This will give you a cleaner architecture also as the website is separate and can change separately from the client and vice-versa.  The client can connect to the webservice and as long as its connected it can receive updates as they occur.  God willing, that should solve your rather interesting problem.
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
2oothAuthor Commented:
Hi Faisall,

I'm new to all of this and time is of the essence... and I was hoping to have a working example of one such model so that can tweak it around to my needs

-Regards
0
 
faisallCommented:
I would enjoy writing a prototype but I'm quite behind myself at the moment.  Maybe someone can who sees this post can write one.  Its really quite simple.  Which parts are lest familiar to you?  Is it setting up a windows server or the FileSystemWatcher?  Here are two good links for setting up a windows service.(http://www.c-sharpcorner.com/uploadfile/mahesh/window_service11262005045007am/window_service.aspx and http://msdn.microsoft.com/en-us/library/zt39148a(VS.80).aspx).  Here is one on how to make a WCF service (http://msdn.microsoft.com/en-us/library/ms733069.aspx).  This link is exactly what you need to setup your windows service.  And this link is pretty much how you want to connect from your client (http://msdn.microsoft.com/en-us/library/ms735103.aspx).  It may seem like a lot but its really not.  I would take it one step at a time.  First get comfortable with the file watcher by making a small app that uses the file listener.  Then change a file and see if it works (maybe a windows app that shows a message box when file is changed).  Once you can do that, then try to setup a simple console app that opens a WCF service, and use another console app to connect to it, once the service can send a message when a file changes to the client, you've got all the core functionality.  Hope this helps, God willing.
0
 
Bob LearnedCommented:
I think that with WCF, you have the capability to handle this requirement, without requiring a Windows service.

Here is some reading material, that can give you some background behind an idea.

What You Need To Know About One-Way Calls, Callbacks, And Events
http://msdn.microsoft.com/en-us/magazine/cc163537.aspx

Let me know what you think, and what additional information that you will need.  I don't have any working code for anything like this, but it should be something that we can come up with together.
0
 
Miguel OzSoftware EngineerCommented:
You do not need the windows service.
Your main issue is that your application is not a truly client/server. Your client must not access the xml file at all. The server application must be the only application that can CRUD the xml file.
WCF callbacks
http://idunno.org/archive/2008/05/29/wcf-callbacks-a-beginners-guide.aspx
WCF tutorial
http://www.xvpj.net/2008/03/08/wcf-step-by-step-tutorial/

Implementation ideas:
1) Server application (WCF server)
AddNewTrail(with all details as parameters)
DeleteTrail(string id)
UpdateTrail(string id)
//The above 3 methods call the callback method to inform clients that they need to fetch the xml)
GenerateXML(int trialID)

2) Client application (WCF client)
Call server methods to get/set trial info.
Call server subscribe method (see first link)  

3) Asp.net
Call the server AddNewTrail with all details as parameters

Hope it helps.
0
 
2oothAuthor Commented:
Thank you guys for your help...

I got it to work now :-)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now