Solved

XML File exchange using WCF

Posted on 2010-08-13
8
685 Views
Last Modified: 2013-12-17
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
Comment
Question by:2ooth
8 Comments
 
LVL 14

Expert Comment

by:systan
Comment Utility
Show your code snippet of asp.net server side and the windows form client side.
0
 

Author Comment

by:2ooth
Comment Utility
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
 
LVL 2

Expert Comment

by:faisall
Comment Utility
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
 

Author Comment

by:2ooth
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:faisall
Comment Utility
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
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 100 total points
Comment Utility
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
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 400 total points
Comment Utility
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
 

Author Comment

by:2ooth
Comment Utility
Thank you guys for your help...

I got it to work now :-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now