CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
AppDomainProxy.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="AppDomainProxy.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>This is a test proxy used to test any type of calls that have to</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Reflection;
10using System.Threading;
11using System.Threading.Tasks;
12using Csla.Server;
15
17{
23 public class AppDomainProxy : DataPortalClient.IDataPortalProxy
24 {
25 private const int TIMEOUT = 70000;
26
27 AppDomain _appDomain;
28 Csla.Server.Hosts.RemotingPortal _portal;
29
30 Csla.Server.Hosts.RemotingPortal Portal
31 {
32 get
33 {
34 try
35 {
36 if (_appDomain == null)
37 {
38 var current = AppDomain.CurrentDomain;
39 var setup = current.SetupInformation;
40 setup.ApplicationName = "Csla.DataPortal";
41 _appDomain =
42 AppDomain.CreateDomain("ServerDomain", null, setup);
43 current.DomainUnload += UnloadDomain;
44 SerializationWorkaround(AppDomain.CurrentDomain);
45 SerializationWorkaround(_appDomain);
46 _appDomain.UnhandledException += new UnhandledExceptionEventHandler(_appDomain_UnhandledException);
47 }
48 if (_portal == null)
49 _portal = (Csla.Server.Hosts.RemotingPortal)_appDomain.CreateInstanceAndUnwrap(
50 "Csla", "Csla.Server.Hosts.RemotingPortal");
51 }
52 catch (Exception ex)
53 {
54 UnloadDomain(null, null);
55 Console.WriteLine(ex);
56 throw;
57 }
58 return _portal;
59 }
60 }
61
62 void _appDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
63 {
64 Console.WriteLine(e.ExceptionObject);
65 }
66
67 void UnloadDomain(object sender, EventArgs e)
68 {
69 if (_appDomain != null)
70 {
71 AppDomain.CurrentDomain.DomainUnload -= UnloadDomain;
72 AppDomain.Unload(_appDomain);
73 }
74 }
75
76 #region Create
77
78 private class CreateTask
79 {
80 public Type ObjectType;
81 public object Criteria;
82 public DataPortalContext Context;
83 public DataPortalResult Result;
84 public Exception ResultException;
85 }
86
87 public async Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
88 {
89 var task = new CreateTask{
90 ObjectType = objectType,
91 Criteria = criteria,
92 Context = context
93 };
94 if (isSync)
95 DoCreate(task);
96 else
97 await Task.Factory.StartNew(DoCreate, task);
98 if (task.ResultException != null)
99 throw task.ResultException;
100 return task.Result;
101 }
102
103 void DoCreate(object state)
104 {
105 CreateTask task = null;
106 try
107 {
108 task = state as CreateTask;
109 if (task != null)
110 task.Result = Portal.Create(task.ObjectType, task.Criteria, task.Context);
111 }
112 catch (Exception ex)
113 {
114 if (task != null)
115 task.ResultException = ex;
116 }
117 }
118
119 #endregion
120
121 #region Fetch
122
123 private class FetchTask
124 {
125 public Type ObjectType;
126 public object Criteria;
127 public DataPortalContext Context;
128 public DataPortalResult Result;
129 public Exception ResultException;
130 }
131
132 public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
133 {
134 var task =
135 new FetchTask
136 {
137 ObjectType = objectType,
138 Criteria = criteria,
139 Context = context
140 };
141 if (isSync)
142 DoFetch(task);
143 else
144 await Task.Factory.StartNew(DoFetch, task);
145 if (task.ResultException != null)
146 throw task.ResultException;
147 return task.Result;
148 }
149
150 void DoFetch(object state)
151 {
152 FetchTask task = null;
153 try
154 {
155 task = state as FetchTask;
156 if (task != null)
157 task.Result = Portal.Fetch(task.ObjectType, task.Criteria, task.Context);
158 }
159 catch (Exception ex)
160 {
161 if (task != null)
162 task.ResultException = ex;
163 }
164 }
165
166 #endregion
167
168 #region Update
169
170 private class UpdateTask
171 {
172 public object Obj;
173 public DataPortalContext Context;
174 public DataPortalResult Result;
175 public Exception ResultException;
176 }
177
178 public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
179 {
180 var task =
181 new UpdateTask
182 {
183 Obj = obj,
184 Context = context
185 };
186
187 if (isSync)
188 DoUpdate(task);
189 else
190 await Task.Factory.StartNew(DoUpdate, task);
191 if (task.ResultException != null)
192 throw task.ResultException;
193 return task.Result;
194 }
195
196 void DoUpdate(object state)
197 {
198 UpdateTask task = null;
199 try
200 {
201 task = (UpdateTask)state;
202
203 if (task != null)
204 task.Result = Portal.Update(task.Obj, task.Context);
205 }
206 catch (Exception ex)
207 {
208 if (task != null)
209 task.ResultException = ex;
210 }
211 }
212
213 #endregion
214
215 #region Delete
216
217 public async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
218 {
219 var task =
220 new DeleteTask
221 {
222 Criteria = criteria,
223 Context = context,
224 ObjectType = objectType
225 };
226 if (isSync)
227 DoDelete(task);
228 else
229 await Task.Factory.StartNew(DoDelete, task);
230 if (task.ResultException != null)
231 throw task.ResultException;
232 return task.Result;
233 }
234
235 private class DeleteTask
236 {
237 public object Criteria;
238 public DataPortalContext Context;
239 public DataPortalResult Result;
240 public Type ObjectType;
241 public Exception ResultException;
242 }
243
244 void DoDelete(object state)
245 {
246 DeleteTask task = null;
247 try
248 {
249 task = state as DeleteTask;
250 if (task != null)
251 task.Result = Portal.Delete(task.ObjectType, task.Criteria, task.Context);
252 }
253 catch (Exception ex)
254 {
255 if (task != null)
256 task.ResultException = ex;
257 }
258 }
259
260 #endregion
261
262 #region Serialization bug workaround
263
264 private static void SerializationWorkaround(AppDomain domain)
265 {
266 // hook up the AssemblyResolve
267 // event so deep serialization works properly
268 // this is a workaround for a bug in the .NET runtime
269 AppDomain currentDomain = AppDomain.CurrentDomain;
270
271 domain.AssemblyResolve +=
272 new ResolveEventHandler(ResolveEventHandler);
273 }
274
275 private static Assembly ResolveEventHandler(
276 object sender, ResolveEventArgs args)
277 {
278 var domain = sender as AppDomain;
279
280 // get a list of all the assemblies loaded in our appdomain
281 Assembly[] list = domain.GetAssemblies();
282
283 // search the list to find the assembly that was not found automatically
284 // and return the assembly from the list
285
286 foreach (Assembly asm in list)
287 if (asm.FullName == args.Name)
288 return asm;
289
290 // can't find it...
291 return null;
292 }
293
294 #endregion
295
296 public bool IsServerRemote
297 {
298 get { return true; }
299 }
300 }
301}
DataPortalResult defines the results of DataPortal operation.
Provides consistent context information between the client and server DataPortal objects.
This is a test proxy used to test any type of calls that have to be send accross to a different AppDo...
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
Interface implemented by server-side data portal components.
@ Serializable
Prevents updating or inserting until the transaction is complete.