CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
WcfProxy.cs
Go to the documentation of this file.
1#if !NETSTANDARD2_0 && !NET5_0
2//-----------------------------------------------------------------------
3// <copyright file="WcfProxy.cs" company="Marimer LLC">
4// Copyright (c) Marimer LLC. All rights reserved.
5// Website: https://cslanet.com
6// </copyright>
7// <summary>Implements a data portal proxy to relay data portal</summary>
8//-----------------------------------------------------------------------
9using System;
10using System.ServiceModel;
11using System.Threading.Tasks;
12using Csla.Server;
14
16{
22 {
23 private static System.ServiceModel.Channels.Binding _defaultBinding;
24 private const int TimeoutInMinutes = 10;
25 private static string _defaultEndPoint = "WcfDataPortal";
26
31 public static System.ServiceModel.Channels.Binding DefaultBinding
32 {
33 get
34 {
35 if (_defaultBinding == null)
36 {
37 _defaultBinding = new WSHttpBinding();
38 WSHttpBinding binding = (WSHttpBinding)_defaultBinding;
39 binding.MaxReceivedMessageSize = int.MaxValue;
40 binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
41 {
42 MaxBytesPerRead = int.MaxValue,
43 MaxDepth = int.MaxValue,
44 MaxArrayLength = int.MaxValue,
45 MaxStringContentLength = int.MaxValue,
46 MaxNameTableCharCount = int.MaxValue
47 };
48
49 binding.ReceiveTimeout = TimeSpan.FromMinutes(TimeoutInMinutes);
50 binding.SendTimeout = TimeSpan.FromMinutes(TimeoutInMinutes);
51 binding.OpenTimeout = TimeSpan.FromMinutes(TimeoutInMinutes);
52 }
53 return _defaultBinding;
54 }
55 set { _defaultBinding = value; }
56 }
57
65 public static string DefaultUrl
66 {
67 get { return ApplicationContext.DataPortalUrlString; }
68 set { ApplicationContext.DataPortalUrlString = value; }
69 }
70
75 public static string DefaultEndPoint
76 {
77 get { return _defaultEndPoint; }
78 set { _defaultEndPoint = value; }
79 }
80
86 public WcfProxy()
87 {
91 }
92
99 public WcfProxy(string dataPortalUrl)
100 {
101 this.DataPortalUrl = dataPortalUrl;
104 }
105
110 public bool IsServerRemote
111 {
112 get { return true; }
113 }
114
118 public System.ServiceModel.Channels.Binding Binding { get; protected set; }
119
124 public string DataPortalUrl { get; protected set; }
125
130 public string EndPoint { get; protected set; }
131
142 protected virtual ChannelFactory<IWcfPortal> GetChannelFactory()
143 {
144 if (!string.IsNullOrEmpty(ApplicationContext.DataPortalUrlString))
145 return new ChannelFactory<IWcfPortal>(Binding, new EndpointAddress(ApplicationContext.DataPortalUrl));
146 else
147 return new ChannelFactory<IWcfPortal>(EndPoint);
148 }
149
158 protected virtual IWcfPortal GetProxy(ChannelFactory<IWcfPortal> cf)
159 {
160 return cf.CreateChannel();
161 }
162
173 public async Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
174 {
175 ChannelFactory<IWcfPortal> cf = GetChannelFactory();
176 var proxy = GetProxy(cf);
177 WcfResponse response = null;
178 try
179 {
180 var request = new CreateRequest(objectType, criteria, context);
181 if (isSync)
182 response = proxy.Create(request);
183 else
184 response = await proxy.CreateAsync(request);
185 if (cf != null)
186 cf.Close();
187 }
188 catch
189 {
190 cf.Abort();
191 throw;
192 }
193 object result = response.Result;
194 if (result is Exception)
195 throw (Exception)result;
196 return (DataPortalResult)result;
197 }
198
209#pragma warning disable 1998
210 public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
211#pragma warning restore 1998
212 {
213 ChannelFactory<IWcfPortal> cf = GetChannelFactory();
214 var proxy = GetProxy(cf);
215 WcfResponse response = null;
216 try
217 {
218 var request = new FetchRequest(objectType, criteria, context);
219 if (isSync)
220 response = proxy.Fetch(request);
221 else
222 response = await proxy.FetchAsync(request);
223 if (cf != null)
224 cf.Close();
225 }
226 catch
227 {
228 cf.Abort();
229 throw;
230 }
231 object result = response.Result;
232 if (result is Exception)
233 throw (Exception)result;
234 return (DataPortalResult)result;
235 }
236
246#pragma warning disable 1998
247 public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
248#pragma warning restore 1998
249 {
250 ChannelFactory<IWcfPortal> cf = GetChannelFactory();
251 var proxy = GetProxy(cf);
252 WcfResponse response = null;
253 try
254 {
255 var request = new UpdateRequest(obj, context);
256 if (isSync)
257 response = proxy.Update(request);
258 else
259 response = await proxy.UpdateAsync(request);
260 if (cf != null)
261 cf.Close();
262 }
263 catch
264 {
265 cf.Abort();
266 throw;
267 }
268 object result = response.Result;
269 if (result is Exception)
270 throw (Exception)result;
271 return (DataPortalResult)result;
272 }
273
284#pragma warning disable 1998
285 public async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
286#pragma warning restore 1998
287 {
288 ChannelFactory<IWcfPortal> cf = GetChannelFactory();
289 var proxy = GetProxy(cf);
290 WcfResponse response = null;
291 try
292 {
293 var request = new DeleteRequest(objectType, criteria, context);
294 if (isSync)
295 response = proxy.Delete(request);
296 else
297 response = await proxy.DeleteAsync(request);
298 if (cf != null)
299 cf.Close();
300 }
301 catch
302 {
303 cf.Abort();
304 throw;
305 }
306 object result = response.Result;
307 if (result is Exception)
308 throw (Exception)result;
309 return (DataPortalResult)result;
310 }
311 }
312}
313#endif
Implements a data portal proxy to relay data portal calls to a remote application server by using WCF...
Definition: WcfProxy.cs:22
WcfProxy(string dataPortalUrl)
Creates an instance of the object, initializing it to use the supplied URL and DefaultBinding values.
Definition: WcfProxy.cs:99
static string DefaultUrl
Gets or sets the default URL address for the data portal server.
Definition: WcfProxy.cs:66
bool IsServerRemote
Gets a value indicating whether the data portal is hosted on a remote server.
Definition: WcfProxy.cs:111
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to create a new business object.
Definition: WcfProxy.cs:173
WcfProxy()
Creates an instance of the object, initializing it to use the DefaultUrl and DefaultBinding values.
Definition: WcfProxy.cs:86
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to load an existing business object.
Definition: WcfProxy.cs:210
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Called by DataPortal to update a business object.
Definition: WcfProxy.cs:247
string EndPoint
Gets the WCF endpoint name for the data portal server used by this proxy instance.
Definition: WcfProxy.cs:130
System.ServiceModel.Channels.Binding Binding
Gets the binding object used by this proxy instance.
Definition: WcfProxy.cs:118
string DataPortalUrl
Gets the URL address for the data portal server used by this proxy instance.
Definition: WcfProxy.cs:124
virtual ChannelFactory< IWcfPortal > GetChannelFactory()
Returns an instance of the channel factory used by GetProxy() to create the WCF proxy object.
Definition: WcfProxy.cs:142
static System.ServiceModel.Channels.Binding DefaultBinding
Gets or sets the default binding used to initialize future instances of WcfProxy.
Definition: WcfProxy.cs:32
virtual IWcfPortal GetProxy(ChannelFactory< IWcfPortal > cf)
Returns the WCF proxy object used for communication with the data portal server.
Definition: WcfProxy.cs:158
static string DefaultEndPoint
Gets or sets the default WCF endpoint name for the data portal server.
Definition: WcfProxy.cs:76
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to delete a business object.
Definition: WcfProxy.cs:285
DataPortalResult defines the results of DataPortal operation.
Provides consistent context information between the client and server DataPortal objects.
Request message for creating a new business object.
Request message for deleting a business object.
Request message for retrieving an existing business object.
Definition: FetchRequest.cs:19
Request message for updating a business object.
Response message for returning the results of a data portal call.
object Result
Busines object or exception return as result of service call.
Interface implemented by client-side data portal proxy objects.