WCF\ASP.NET interoperability - asp.net

I have a server application written in WCF using asynchronous callbacks, and a webforms application in ASP.NET.
All of the communication is fine between the 2 applications, I can call the exposed functions in the server via the web application, and the server can send callbacks to the web application, however sometimes the functions within the callback work, and other times, they don't.
For example, I would like a login button on the web app to send a username and password to the server, the server checks this against the database, and if the login information is correct, it should send a callback, which opens a new page in the web app.
Here is the relevant server code:
public void Login(String username, String password)
{
//DoCheckAgainstDatabase(username, password);
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
callback.LoginSuccess();
}
and the web application code:
private InstanceContext _instanceContext;
private ServiceClient _service;
public CallbackHandler MyCallbackHandler = new CallbackHandler();
protected void Page_Load(object sender, EventArgs e)
{
_instanceContext = new InstanceContext(MyCallbackHandler);
_backEnd = new ServiceClient(_instanceContext, "NetTcpBinding_IAU", "net.tcp://localhost/MyService/Service");
_backEnd.Open();
MyCallbackHandler.LoginSucceeded += OnLoginSucceeded;
}
protected void LoginButton_Click(object sender, EventArgs e)
{
_backEnd.Login(UsernameTextBox.Text, PasswordTextBox.Text);
}
private void OnLoginSucceeded(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "OpenClientWindow", "window.open('Client.aspx','_self');", true);
}
I can put in breakpoints, and see that everything is working fine, it's just that the code 'ScriptManager.RegisterStartupScript...' does not execute properly all the time.
Could this be something to do with threading? Could anyone suggest a way to fix this please?
Thanks in advance!
David

It occurs to me that it's possible your page life cycle may be ending - or at least getting to the Render stage, which is where the start up script would be written to the output - before the callback is called.
Is it possible to call your service synchronously, and not proceed out of LoginButton_Click until the service call returns?

I think you are missing script tag - wrap your window.oppen with it, like
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "OpenClientWindow", "<script>window.open('Client.aspx','_self');</script>", true);

Thank you Ann L. for guidance on this. I have added a ManualResetEvent, and then in the button click method, I wait until I have received the callback, then proceed with opening the new page:
private InstanceContext _instanceContext;
private ServiceClient _service;
public CallbackHandler MyCallbackHandler = new CallbackHandler();
private ManualResetEvent _resetEvent = new ManualResetEvent(false);
protected void Page_Load(object sender, EventArgs e)
{
_instanceContext = new InstanceContext(MyCallbackHandler);
_backEnd = new ServiceClient(_instanceContext, "NetTcpBinding_IAU", "net.tcp://localhost/MyService/Service");
_backEnd.Open();
MyCallbackHandler.LoginSucceeded += OnLoginSucceeded;
}
protected void LoginButton_Click(object sender, EventArgs e)
{
_backEnd.Login(UsernameTextBox.Text, PasswordTextBox.Text);
_resetEvent.WaitOne();
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "OpenClientWindow", "window.open('Client.aspx','_self');", true);
}
private void OnLoginSucceeded(object sender, EventArgs e)
{
_resetEvent.Set();
}

Related

Is using OnPropertyChanged(string.Empty); a practice to avoid?

We have OnPropertyChanged(string.Empty); in quite a few places in our Xamarin.Forms application. In general I feel like we shouldn't be saying "Hey, notify everyone that all the properties on this object have changed."
An example of where we do this is in some data syncing event handlers in a view model that inherits
private void OnSyncEnding(object sender, EventArgs e){
SyncItem = syncService.Value.SyncState;
OnPropertyChanged(string.Empty);
}
private void OnSyncStarting(object sender, EventArgs e){
SyncItem = syncService.Value.SyncState;
OnPropertyChanged(string.Empty);
}
private void OnSyncFormSuccessful(object sender, EventArgs e){
SyncItem = syncService.Value.SyncState;
OnPropertyChanged(string.Empty);
}
My question is: How much work does this actually trigger and when is using OnPropertyChanged(string.Empty); the correct thing to do instead of calling OnPropertyChanged("SomeProperty") for all the properties required?

Cookie does not work in asp.net

I have two pages, test1.aspx and test2.aspx
test1.aspx has this
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("test", "test");
cookie.Expires = DateTime.Now.AddDays(1);
Response.SetCookie(cookie);
}
test2.aspx has this
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Response.Cookies["test"].Value);
}
The value of the cookie is null, no matter how many times I tried. I tried to open page1 and then page 2, expecting a cookie to work, but it is not working, I don't know why.
I think you need to read off the Request instead of the response.
As MSDN suggestions
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Cookies["test"].Value);
}
In a web application, the request comes from the client (browser) and the response is sent from the server. When validating cookies or cookie data from the browser you should use the Request.Cookies collection. When you are constructing cookies to be sent to the browser you need to add them to the Response.Cookies collection.
Additional thoughts on the use of SetCookie
Interestingly for HttpResponse.SetCookie as used on your first page; MSDN says this method is not intended for use in your code.
This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Even the example code found on this page uses the Response.Cookies.Add(MyCookie) approach and does not call SetCookie
You need is :
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Cookies["test"].Value);
}
There is a sample here:
Reading and Writing Cookies in ASP.NET and C#
Regards
Save cookie with (response) and read cookie by (request)
//write
response.cookies("abc") = 123;
//read
if ((request.cookies("abc") != null)) {
string abc = request.cookies("abc");
}
Use Response.Cookies.Add(cookie);
Reference: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.cookies
On page test2.aspx
You should try this
protected void Page_Load(object sender, EventArgs e)
{
var httpCookie = Request.Cookies["test"];
if (httpCookie != null)
{
Response.Write(httpCookie.Value);
}
}

how to create scheduler application for mail in asp.net?

my question is, I have to send mail at specific time daily, is there any way to do this in asp.net ?
give me appropriate suggestions.
Note : i don't want to run windows application or windows scheduler.
code which i used in global.asax
private static CacheItemRemovedCallback OnCacheRemove = null;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AddTask("Remoder", 5);
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
private void AddTask(string name, int seconds)
{
OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
HttpRuntime.Cache.Insert(name, seconds, null,
DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, OnCacheRemove);
}
public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
// do stuff here if it matches our taskname, like WebRequest
// re-add our task so it recurs
AddTask(k, Convert.ToInt32(v));
}
public void Remoder()
{
HttpContext.Current.Response.Write("Hello Start");
}
Have a look at http://quartznet.sourceforge.net/
I think this technique from Jeff Himself is what you need:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
I tried the hack ... turning your application global.asax into a virtual scheduler. It was my choice, because my server admin department simply refuses (because they don't understand) to do it with windows, or a database job.
If I had my druthers though, I'd use a database "job", since sql-server can send mail directly. Not sure what db you're using, but if you have the necessary access, I'd recommend looking for a solution like that, instead of trying to fool your asp.net app.

Action after 10 minutes ASP.NET

In ASP.NET C# how to make a action after 10 minutes? It must be without the use of browser... Obviously an server side action...
You could set a timer in Global.asax to fire every 10 minutes:
private static Timer m_MailUpdateTimer;
protected void Application_Start(object sender, EventArgs e)
{
m_MailUpdateTimer = new Timer(MailUpdateTimer_Check, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
}
private static void MailUpdateTimer_Check(object state)
{
// Do something here.
}
protected void Application_End(object sender, EventArgs e)
{
if (m_MailUpdateTimer != null)
m_MailUpdateTimer.Dispose();
}
Of course, this will only fire if the web application is active, so if there is no usage for a while and IIS unloads it from memory, then the timer will not fire.
You may also want to consider using a Windows service or a scheduled job, which might be better suited for your needs.

Fire just once a Thread in Asp.net WebSite Global.asax

I've a legacy application using Asp.Net WebSite (winforms...) and I need run a background thread that collect periodically some files.
But this thread must run just one time!
My problem start when I put a method in Application_Start:
void Application_Start(object sender, EventArgs e) {
SetConnection();
SetNHibernate();
SetNinject();
SetExportThread();
}
So I start my application on Visual Studio and three threads start to run.
I need some singleton? or something?
Try creating a static method and variable:
private static bool _inited = false;
private static object _locker = new object();
private static void Init()
{
if (!_inited)
{
lock(_locker)
{
// Have to check again because the first check wasn't thread safe
if (!_inited)
{
SetConnection();
SetNHibernate();
SetNinject();
SetExportThread();
_inited = true;
}
}
}
}
void Application_Start(object sender, EventArgs e)
{
Init();
}

Resources