Solved

XML File exchange using WCF

Posted on 2010-08-13
8
702 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 14

Expert Comment

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

Author Comment

by:2ooth
ID: 33437805
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
ID: 33438502
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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:2ooth
ID: 33438545
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
 
LVL 2

Expert Comment

by:faisall
ID: 33440107
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
ID: 33442046
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 36

Accepted Solution

by:
Miguel Oz earned 400 total points
ID: 33443026
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
ID: 33450235
Thank you guys for your help...

I got it to work now :-)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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