Calling WCF RIA Service from Code behind


Assume that there is a RIA Service usually under ProjectName.Web and inside the Service folder with the file name ArtifactService.cs and with the below implementation

 

namespace ProjectName.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using ProjectName.Web.Models;

    [EnableClientAccess()]
    public class ArtifactService : LinqToEntitiesDomainService<PROJEntities>
    {

        public IQueryable<Artifact> GetArtifacts()
        {
            return this.ObjectContext.Artifacts;
        }

        public IQueryable<Artifact> GetArtifactById(string projectId)
        {
            if (!string.IsNullOrEmpty(projectId))
            {
                Guid filterValue = Guid.Parse(projectId);
                return this.ObjectContext.Artifacts.Where(i => i.ProjectId == filterValue);
            }
            else
            {
                return null;
            }
        }
        public void InsertArtifact(Artifact artifact)
        {
            if ((artifact.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(artifact, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Artifacts.AddObject(artifact);
            }
        }

        public void UpdateArtifact(Artifact currentArtifact)
        {
            this.ObjectContext.Artifacts.AttachAsModified(currentArtifact, this.ChangeSet.GetOriginal(currentArtifact));
        }

        public void DeleteArtifact(Artifact artifact)
        {
            if ((artifact.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(artifact, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Artifacts.Attach(artifact);
                this.ObjectContext.Artifacts.DeleteObject(artifact);
            }
        }
    }
}

 

In the MainPage.cs we can execute the service with the below way

 

using ProjectName.Web.Services;
using ProjectName.Web.Models;
using System.ServiceModel.DomainServices.Client;

public partial class MainPage : UserControl
    {
public MainPage()
        {
            InitializeComponent();
ArtifactContext raCtx = new ArtifactContext();
                        raCtx.Load(raCtx.GetArtifactByIdQuery("id parameter"), new Action<LoadOperation<Artifact>>(ArtifactLoaded), false);
                        raCtx = null;
         }
        
private void ArtifactLoaded(LoadOperation<Artifact> args)
         {
             datagridData.AutoGenerateColumns = true;
             datagridData.ItemsSource = args.Entities;
             if (args.Entities.Count() > 0)
             {
                 datagridData.SelectedIndex = 0;
             }
             else
             {
                 datagridData.ItemsSource = null;
             }
        }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s