I am developing an application that runs on a FEZ Spider board that communicate with a WCF hosted on my PC. I have followed this example, but instead of using a WCFServiceHost, I've hosted the WCF service in IIS.
The problem is that when I'm contacting the WCF service, the application always raises an IO Exception.
Here is the code of my simple Gadgeteer application that tests the WCF connection.
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using tempuri.org;
using schemas.datacontract.org.WcfServiceTest;
using Ws.Services.Binding;
using Ws.Services;
namespace GadgeteerClient
{
public partial class Program
{
private bool networkUp = false;
private IService1ClientProxy proxy;
//*****
// TODO: change this to the IP address of your machine that is running the WCF service
//*****
string hostRunningWCFService = "169.254.65.183";
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
/*******************************************************************************************
Modules added in the Program.gadgeteer designer view are used by typing
their name followed by a period, e.g. button. or camera.
Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
button.ButtonPressed +=<tab><tab>
If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
timer.Tick +=<tab><tab>
timer.Start();
*******************************************************************************************/
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
// USE UpdateClientProxy.cmd to create the proxy classes
ethernetJ11D.NetworkUp += ethernetJ11D_NetworkUp;
ethernetJ11D.UseThisNetworkInterface();
ethernetJ11D.UseStaticIP("169.254.141.168", "255.255.0.0", hostRunningWCFService);
button.ButtonPressed += button_ButtonPressed;
}
void ethernetJ11D_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
{
Debug.Print("NETWORK IS UP!");
networkUp = true;
proxy = new IService1ClientProxy(new WS2007HttpBinding(), new ProtocolVersion11());
// NOTE: the endpoint needs to match the endpoint of the servicehost
proxy.EndpointAddress = "http://" + hostRunningWCFService + "/Service1";
}
void button_ButtonPressed(Button sender, Button.ButtonState state)
{
if (!networkUp)
return;
button.TurnLedOn();
try
{
GetData data = new GetData();
data.value = 123;
// call test
string result = proxy.GetData(data).GetDataResult;
// should print 123, IO Exception is instead generated
Debug.Print("Received: " + result);
displayTE35.SimpleGraphics.DisplayText(result, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Gray, 30, 30);
}
catch (System.IO.IOException ex)
{
Debug.Print("Error making WCF call");
Debug.Print(ex.ToString());
}
finally
{
button.TurnLedOff();
}
}
}
}
How could I solve this issue?
EDIT:
Details about the exception are:
Exception was thrown: System.IO.IOException
There is no inner exception and the stack trace is:
Ws.Services.Binding.HttpTransportBindingElement::OnProcessInputMessage
Ws.Services.Binding.BindingElement::ProcessInputMessage
Ws.Services.Binding.BindingElement::ProcessInputMessage
Ws.Services.Binding.RequestChannel::ReceiveMessage
Ws.Services.Binding.RequestChannel::Request
tempuri.org.IService1ClientProxy::GetData
GadgeteerClient.Program::button_ButtonPressed
Gadgeteer.Modules.GHIElectronics.Button::OnButtonEvent
System.Reflection.MethodBase::Invoke
Gadgeteer.Program::DoOperation
Microsoft.SPOT.Dispatcher::PushFrameImpl
Microsoft.SPOT.Dispatcher::PushFrame
Microsoft.SPOT.Dispatcher::Run
Gadgeteer.Program::Run
Related
This is my first project using the NetMQ (ZMQ) framework, so, maybe I didn't understand how to use it exactly.
I create a Windows Forms project with two applications, one send a "ping" to the other and receives a "pong" as an answer. The protocol is not so complex and uses the Request-Reply pattern, all the commands have a first part that identifies the objective, like "query" or "inform", and a second part that contains the command or the message itself. In this case one app send a "query-ping" and the other answer with "inform-pong".
I create a class to encapsulate all the dirty job, so the main form can use the protocol in a very simple way. Everything is working fine, but when I try to close the app, it gets stuck in the poller and the app never closes. In Visual Studio I can see the pause and stop button but I don't get any exception or errors:
and when I click in pause button I get this message (The application is in break mode):
If I click in 'Continue execution' the app back to same state and never closes.
If I remove the poller the app closes normally, but of course, the poller doesn't work and the app doesn't answer anymore.
This is the code from the Form1:
using CommomLib;
using System;
using System.Windows.Forms;
namespace Test1
{
public partial class Form1 : Form
{
ZmqCommunication zmqComm = new ZmqCommunication();
int portNumber;
string status;
public Form1()
{
InitializeComponent();
InitializeZmqComm();
}
public void InitializeZmqComm()
{
// Calls the ZMQ initialization.
portNumber = zmqComm.InitializeComm();
if (portNumber == 0)
status = "Ini error";
else
status = "Ini ok";
}
// Executes a ping command.
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
richTextBox1.AppendText(zmqComm.RequestPing(55001) + "\n");
}
}
}
And this is the code from my NetMQ class. It is in a separated library project. In my Dispose method I tried all combinations of Remove, Dispose and StopAsync and nothing works:
using NetMQ;
using NetMQ.Sockets;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace CommomLib
{
public class ZmqCommunication
{
ResponseSocket serverComm = new ResponseSocket();
NetMQPoller serverPoller;
int _portNumber;
public ZmqCommunication()
{
_portNumber = 55000;
}
// Problem here! The serverPoller gets stuck.
public void Dispose()
{
//serverPoller.RemoveAndDispose(serverComm);
//serverComm.Dispose();
if (serverPoller.IsDisposed)
Debug.WriteLine("A");
//serverPoller.RemoveAndDispose(serverComm);
serverPoller.Remove(serverComm);
//serverPoller.StopAsync();
serverPoller.Dispose();
serverComm.Dispose();
if (serverPoller.IsDisposed)
Debug.WriteLine("B");
Thread.Sleep(500);
if (serverPoller.IsDisposed)
Debug.WriteLine("C");
Thread.Sleep(500);
if (serverPoller.IsDisposed)
Debug.WriteLine("D");
}
// ZMQ initialization.
public int InitializeComm()
{
bool ok = true;
bool tryAgain = true;
// Looks for a port.
while (tryAgain && ok)
{
try
{
serverComm.Bind("tcp://127.0.0.1:" + _portNumber);
tryAgain = false;
}
catch (NetMQ.AddressAlreadyInUseException)
{
_portNumber++;
tryAgain = true;
}
catch
{
ok = false;
}
}
if (!ok)
return 0; // Error.
// Set up the pooler.
serverPoller = new NetMQPoller { serverComm };
serverComm.ReceiveReady += (s, a) =>
{
RequestInterpreter();
};
// start polling (on this thread)
serverPoller.RunAsync();
return _portNumber;
}
// Message interpreter.
private void RequestInterpreter()
{
List<string> message = new List<string>();
if (serverComm.TryReceiveMultipartStrings(ref message, 2))
{
if (message[0].Contains("query"))
{
// Received the command "ping" and answers with a "pong".
if (message[1].Contains("ping"))
{
serverComm.SendMoreFrame("inform").SendFrame("pong");
}
}
}
}
// Send the command "ping".
public string RequestPing(int port)
{
using (var requester = new RequestSocket())
{
Debug.WriteLine("Running request port {0}", port);
requester.Connect("tcp://127.0.0.1:" + port);
List<string> msgResp = new List<string>();
requester.SendMoreFrame("query").SendFrame("ping");
if (requester.TryReceiveMultipartStrings(new TimeSpan(0, 0, 10), ref msgResp, 2))
{
if (msgResp[0].Contains("inform"))
{
return msgResp[1];
}
}
}
return "error";
}
}
}
Can you try to call NetMQConfig.Cleanup(); in window close event?
Place a breakpoint and see if you even get to ZmqCommunication.Dispose - that might be the issue - the form class not disposing the ZmqCommunication class.
Thanks guys for the answers, they were not the solution but pointed me in the right direction.
After a lot of debugging, I figured out it was a silly problem. I have two programs almost identical (I had both opened at the same time), one of them had the method Dispose() and the other no. During the debugging, in the breaking point, I thought I was in one program but I was in the other one. Really silly.
I have a windows service that communicates with a DB residing on a different server.The service is installed and always running.The service will look out if there any new records in the DB Table and get 1000 records at a time via stored proc and process records (updates/creates in the CRM system).
Logic is working perfectly alright but the problem is with service going to idle state(service is in running state but won't execute the method 'ProcessNewOtranRecords' that calls stored proc) after few hours. When the service is restarted it again works as expected for few more hours.
Please suggest me if there is any good approach to keep service active all the time.
Here is the code :
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Security.Permissions;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using CRM.Objects.BusinessLogic;
namespace CRM
{
partial class CrmProcessOtran : ServiceBase
{
OtranBL _otranBL = new OtranBL();
private System.Timers.Timer mainTimer;
int eventID = 0;
public CrmProcessOtran()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("Service Start");
mainTimer = new System.Timers.Timer(30000); // 30 seconds
mainTimer.Elapsed += PerformOtranOperations;
mainTimer.AutoReset = false;
mainTimer.Start();
}
protected override void OnStop()
{
eventLog1.WriteEntry("Service Stopped");
mainTimer.Stop();
mainTimer.Dispose();
mainTimer = null;
}
public void PerformOtranOperations(object sender, EventArgs e)
{
eventID++;
eventLog1.WriteEntry(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss") + " - checking for new otran records", EventLogEntryType.Information, eventID);
// Check for new otran records
int otranRecords = _otranBL.GetOtranRecordCount(eventLog1);
if (otranRecords == 0)
{
eventLog1.WriteEntry("0 new otran records", EventLogEntryType.Information, eventID);
return;
}
eventLog1.WriteEntry(otranRecords.ToString("N0") + " new otran records found with proc_status = 0", EventLogEntryType.Information, eventID);
// Process new records
eventLog1.WriteEntry(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss") + " - Begin processing new otran records", EventLogEntryType.Information, eventID);
int processedrecords = _otranBL.ProcessNewOtranRecords(eventLog1);
eventLog1.WriteEntry(processedrecords.ToString() + " processed records", EventLogEntryType.Information, eventID);
mainTimer.Start();
}
}
}
You should add some Try/Catch/Finally logic into PerformOtranOperations. You're propably experiencing some exception after some iterations and your mainTimer.Start(); is never called from that moment.
So the service is running but the trigger is not active any more.
The Timer component catches and suppresses all exceptions thrown by event handlers
for the Elapsed event; see MSDN.
Because of this you probably don't see any errors in the Windows event viewer.
Can you rework your code to use a System.Threading.Timer instance instead, which doesn't swallow the exception if something goes wrong.
As Pavlinll suggest, you should implement some error handling in order to keep your service running.
Here is my code, a simplified version
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using RDSCOMMUNICATORLib;
using System.Timers;
using System.Threading;
namespace RDSConsoleApplication
{
class Program
{
static public RDSComClass oObj = new RDSComClass();
static void Main(string[] args)
{
try
{
oObj.Host = "127.0.0.1";
oObj.Port = 2902;
oObj.LoadPiece(); // OK HERE
IConnectionEvents_OnPieceEventHandler PieceArraved = new IConnectionEvents_OnPieceEventHandler(oObj_OnPiece);
oObj.OnPiece += PieceArraved;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
} // end main
static public void oObj_OnPiece(int lLSCRef, string strLSCName, int lPieceNumber, int bWithScans)
{
try
{
// HERE WE START GETTING EXCEPTION "Unable to cast COM object of type.....
// The application called an interface that was marshalled for a different thread"
oObj.LoadPiece();
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
} // end class Program
} // end namespace
I am referencing a COM object inside C# console application that serves as a gateway to connect to the back end and periodically receive some "piece" objects.
As a test, when I try from within the main method all works fine: I can connect, receive "piece" object and access its properties. The problem is that I need to receive and process that same "piece" object from within oObj_OnPiece callback method, and it throws the above mentioned exception. I browsed other similar posts, I understand it's a threading issue, but not sure how to resolve it. Any help is appreciated.
You try to query an interface, which is already in use in a different thread in your application. In your case you have queried by your call the interface in your main-thread first. I guess this is the first thread.
Is it possible that the eventhandler is opening a different thread to deal with the event? If this is the case ( just check this by adding one breakpoint in your event-handler before trying to access the interface , start your program and check if there a 2 threads running ).
What you have to do is: ensure that you query your interface only in one thread by removing the first call to your COM-object.
I'm trying to create a web app which does many things but the one that I'm currently focused in is the inbox count. I want to use EWS StreamSubscription so that I can get notification for each event and returns the total count of items in the inbox. How can I use this in terms of MVC? I did find some code from Microsoft tutorial that I was gonna test, but I just couldn't figure how I could use it in MVC world i.e. What's the model going to be, if model is the count then how does it get notified every time an event occurs in Exchange Server, etc.
Here's the code I downloaded from Microsoft, but just couldn't understand how I can convert the count to json and push it to client as soon as a new change event occurs. NOTE: This code is unchanged, so it doesn't return count, yet.
using System;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.Exchange.WebServices.Data;
namespace StreamingNotificationsSample
{
internal class Program
{
private static AutoResetEvent _Signal;
private static ExchangeService _ExchangeService;
private static string _SynchronizationState;
private static Thread _BackroundSyncThread;
private static StreamingSubscriptionConnection CreateStreamingSubscription(ExchangeService service,
StreamingSubscription subscription)
{
var connection = new StreamingSubscriptionConnection(service, 30);
connection.AddSubscription(subscription);
connection.OnNotificationEvent += OnNotificationEvent;
connection.OnSubscriptionError += OnSubscriptionError;
connection.OnDisconnect += OnDisconnect;
connection.Open();
return connection;
}
private static void SynchronizeChangesPeriodically()
{
while (true)
{
try
{
// Get all changes from the server and process them according to the business
// rules.
SynchronizeChanges(new FolderId(WellKnownFolderName.Inbox));
}
catch (Exception ex)
{
Console.WriteLine("Failed to synchronize items. Error: {0}", ex);
}
// Since the SyncFolderItems operation is a
// rather expensive operation, only do this every 10 minutes
Thread.Sleep(TimeSpan.FromMinutes(10));
}
}
public static void SynchronizeChanges(FolderId folderId)
{
bool moreChangesAvailable;
do
{
Console.WriteLine("Synchronizing changes...");
// Get all changes since the last call. The synchronization cookie is stored in the _SynchronizationState field.
// Only the the ids are requested. Additional properties should be fetched via GetItem calls.
var changes = _ExchangeService.SyncFolderItems(folderId, PropertySet.IdOnly, null, 512,
SyncFolderItemsScope.NormalItems, _SynchronizationState);
// Update the synchronization cookie
_SynchronizationState = changes.SyncState;
// Process all changes
foreach (var itemChange in changes)
{
// This example just prints the ChangeType and ItemId to the console
// LOB application would apply business rules to each item.
Console.Out.WriteLine("ChangeType = {0}", itemChange.ChangeType);
Console.Out.WriteLine("ChangeType = {0}", itemChange.ItemId);
}
// If more changes are available, issue additional SyncFolderItems requests.
moreChangesAvailable = changes.MoreChangesAvailable;
} while (moreChangesAvailable);
}
public static void Main(string[] args)
{
// Create new exchange service binding
// Important point: Specify Exchange 2010 with SP1 as the requested version.
_ExchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
{
Credentials = new NetworkCredential("user", "password"),
Url = new Uri("URL to the Exchange Web Services")
};
// Process all items in the folder on a background-thread.
// A real-world LOB application would retrieve the last synchronization state first
// and write it to the _SynchronizationState field.
_BackroundSyncThread = new Thread(SynchronizeChangesPeriodically);
_BackroundSyncThread.Start();
// Create a new subscription
var subscription = _ExchangeService.SubscribeToStreamingNotifications(new FolderId[] {WellKnownFolderName.Inbox},
EventType.NewMail);
// Create new streaming notification conection
var connection = CreateStreamingSubscription(_ExchangeService, subscription);
Console.Out.WriteLine("Subscription created.");
_Signal = new AutoResetEvent(false);
// Wait for the application to exit
_Signal.WaitOne();
// Finally, unsubscribe from the Exchange server
subscription.Unsubscribe();
// Close the connection
connection.Close();
}
private static void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
// Cast the sender as a StreamingSubscriptionConnection object.
var connection = (StreamingSubscriptionConnection) sender;
// Ask the user if they want to reconnect or close the subscription.
Console.WriteLine("The connection has been aborted; probably because it timed out.");
Console.WriteLine("Do you want to reconnect to the subscription? Y/N");
while (true)
{
var keyInfo = Console.ReadKey(true);
{
switch (keyInfo.Key)
{
case ConsoleKey.Y:
// Reconnect the connection
connection.Open();
Console.WriteLine("Connection has been reopened.");
break;
case ConsoleKey.N:
// Signal the main thread to exit.
Console.WriteLine("Terminating.");
_Signal.Set();
break;
}
}
}
}
private static void OnNotificationEvent(object sender, NotificationEventArgs args)
{
// Extract the item ids for all NewMail Events in the list.
var newMails = from e in args.Events.OfType<ItemEvent>()
where e.EventType == EventType.NewMail
select e.ItemId;
// Note: For the sake of simplicity, error handling is ommited here.
// Just assume everything went fine
var response = _ExchangeService.BindToItems(newMails,
new PropertySet(BasePropertySet.IdOnly, ItemSchema.DateTimeReceived,
ItemSchema.Subject));
var items = response.Select(itemResponse => itemResponse.Item);
foreach (var item in items)
{
Console.Out.WriteLine("A new mail has been created. Received on {0}", item.DateTimeReceived);
Console.Out.WriteLine("Subject: {0}", item.Subject);
}
}
private static void OnSubscriptionError(object sender, SubscriptionErrorEventArgs args)
{
// Handle error conditions.
var e = args.Exception;
Console.Out.WriteLine("The following error occured:");
Console.Out.WriteLine(e.ToString());
Console.Out.WriteLine();
}
}
}
I just want to understand the basic concept as in what can be model, and where can I use other functions.
Your problem is that you are confusing a service (EWS) with your applications model. They are two different things. Your model is entirely in your control, and you can do whatever you want with it. EWS is outside of your control, and is merely a service you call to get data.
In your controller, you call the EWS service and get the count. Then you populate your model with that count, then in your view, you render that model property. It's really that simple.
A web page has no state. It doesn't get notified when things change. You just reload the page and get whatever the current state is (ie, whatever the current count is).
In more advanced applications, like Single Page Apps, with Ajax, you might periodically query the service in the background. Or, you might have a special notification service that uses something like SignalR to notify your SPA of a change, but these concepts are far more advanced than you currently are. You should probably develop your app as a simple stateless app first, then improve it to add ajax functionality or what not once you have a better grasp of things.
That's a very broad question without a clear-cut answer. Your model could certainly have a "Count" property that you could update. The sample code you found would likely be used by your controller.
I am trying to get a test application to work using Oracle Change Notification with C#, but I am not receiving the callback notification in my application. Oracle DB version is 11.2.0.1.0. Oracle.DataAccess v.2.112.30. I can confirm the query gets registered in Oracle by viewing SYS.USER_CHANGE_NOTIFICATION_REGS and SYS.USER_CQ_NOTIFICATION_QUERIES. However, nothing ever appears in SYS.DBA_CHANGE_NOTIFICATION_REGS.
The registration persists until I commit a transaction on the table. The registration disappears after a few seconds after the commit and my application does not received the notification.
I have made sure my computer is listening on the correct port and have even tried turning off any firewall that could be blocking the port.
I do have GRANT CHANGE NOTIFICATION to MYSCHEMA, GRANT EXECUTE ON DBMS_CHANGE_NOTIFICATION TO MYSCHEMA, and the JOB_QUEUE_PROCESSES is set to 1.
Questions:
1) Should the registration be visible in SYS.DBA_CHANGE_NOTIFICATION_REGS and, if so, what could be causing it not to be when it is visible in SYS.USER_CHANGE_NOTIFICATION_REGS and SYS.USER_CQ_NOTIFICATION_QUERIES?
2) What could be causing the registration to disappear after a commit?
3) What could be causing the failure of the notification to my application?
Here is the C# code I am using and it is basically the same as from the Oracle website:
using System;
using System.Threading;
using System.Data;
using Oracle.DataAccess.Client;
namespace NotifyTest
{
public class Program
{
public static bool IsNotified = false;
public static void Main(string[] args)
{
string constr = "User Id=mySchema;Password=myPassword;Data Source=myOracleInstance";
OracleDependency dep = null;
try
{
using (var con = new OracleConnection(constr))
{
Console.WriteLine("Registering query...");
var cmd = new OracleCommand("select * from mySchema.NOTIFY_TEST", con);
con.Open();
OracleDependency.Port = 1005;
dep = new OracleDependency(cmd);
dep.OnChange += OnMyNotificaton;
int queryRegistered = cmd.ExecuteNonQuery();
// If oracle returns -1, then the query is successfully registered
if (queryRegistered == -1)
{
Console.WriteLine("Query Registered...");
Console.WriteLine("Listening for Callback...");
}
else
{
Console.WriteLine("There was an error...");
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
// Loop while waiting for notification
while (IsNotified == false)
{
Thread.Sleep(100);
}
}
public static void OnMyNotificaton(object sender, OracleNotificationEventArgs arg)
{
Console.WriteLine("Table change notification event is raised");
Console.WriteLine(arg.Source.ToString());
Console.WriteLine(arg.Info.ToString());
Console.WriteLine(arg.Source.ToString());
Console.WriteLine(arg.Type.ToString());
IsNotified = true;
}
}
}
Just wanted to provide an update as to how I resolved this issue. I changed my Oracle.DataAccess.dll from v.2.112.3.0 to v.2.112.1.2 and it works fine.
In SYS.CHNF$_REG_INFO attribute QOSFLAGS there is QOS_DEREG_NFY, which specifies that the database should unregister the registration on the first notification.
Not sure but the value on job_queue_processes (1) is a bit low. Oracle performs all kinds of maintenance and event handling tasks internally. For this they also use Job slaves. Raise job_queue_processes (default 1000) and check Troubleshooting CQN Registrations