Click here to Skip to main content
15,887,376 members
Home / Discussions / C#
   

C#

 
GeneralRe: interact 2 way Pin
Heath Stewart10-Feb-04 11:10
protectorHeath Stewart10-Feb-04 11:10 
GeneralRe: interact 2 way Pin
Blue_Aqua10-Feb-04 18:23
Blue_Aqua10-Feb-04 18:23 
GeneralMultyThreading in C#: Communication between threads Pin
Jasper4C#10-Feb-04 2:46
Jasper4C#10-Feb-04 2:46 
GeneralRe: MultyThreading in C#: Communication between threads Pin
Marc Clifton10-Feb-04 4:17
mvaMarc Clifton10-Feb-04 4:17 
GeneralRe: MultyThreading in C#: Communication between threads Pin
Jasper4C#10-Feb-04 12:08
Jasper4C#10-Feb-04 12:08 
GeneralRe: MultyThreading in C#: Communication between threads Pin
Marc Clifton10-Feb-04 13:38
mvaMarc Clifton10-Feb-04 13:38 
GeneralBackground compile in C# Pin
mufoxe10-Feb-04 2:24
mufoxe10-Feb-04 2:24 
GeneralRemoting, Events, Interfaces and Assemblies Pin
CyberHawke10-Feb-04 1:35
CyberHawke10-Feb-04 1:35 
Hello All,

I have an issue that is driving me crazy, no, not that kind of issue.

I have published an interface that I had intended to use to build objects for both client and server. The problem is, I cannot get my projects to recognize that I have declared an event, even though I can see it right there in the code.

A couple of scenarios:
1. If I don't declare an event, then I can use the published interface without any problems.
2. I can write the interface code directly in my project and again, no problem.

The problem with scenario one is that I need to declare events in my interface in order to be able to use them in both the client and the server.

The problem with scenario two is that now I am not publishing an interface, but rather declaring them in the code for both the client and the server objects. Crazy stuff eh Wink | ;)

This is my published interface which is compiled into a separate assembly:
using System;
using System.Data;

namespace RemoteObjectInterface {
public delegate void TaskCompleted();
public interface IRemoteObject {
event TaskCompleted TaskComplete; // event to be raised or captured

void StartTask();
string GetHostLocation();
DataTable GetProducts();
}
}

In a separate project, I make a reference to this assembly,
and then create a class that implements it:

using System;
using System.Data;
using System.Timers;
using System.Data.SqlClient;
using RemoteObjectInterface; // Reference to compiled assembly

namespace RemoteObject {
public delegate void TaskCompleted(object sender, TaskCompleteEventArgs e);
public class ProductsDB : MarshalByRefObject, IRemoteObject { // declared implementing interface
private static string _connectDef = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;";
public event TaskCompleted TaskComplete;
private Timer tmr = new Timer();

public void StartTask() {
tmr.Interval = 10000;
tmr.Elapsed += new ElapsedEventHandler(tmrCallback);
tmr.Start();
}

private void tmrCallback(object sender, ElapsedEventArgs e) {
tmr.Enabled=false;
if (TaskComplete != null) {
TaskComplete(this,new TaskCompleteEventArgs("Task completed on server"));
}
}

// Get the data from the Products table in the Northwind database and return them to the client.
public DataTable GetProducts() {
string SQL = "SELECT * FROM Products";

// Create ADO.NET objects.
SqlConnection cnProducts = new SqlConnection(_connectDef);
SqlCommand cmdProducts = new SqlCommand(SQL,cnProducts);
SqlDataAdapter daProducts = new SqlDataAdapter(cmdProducts);
DataSet dsProducts = new DataSet();

// Execute the command and retrieve the data.
try {
cnProducts.Open();
daProducts.Fill(dsProducts,"Products");
}
catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
finally {
cnProducts.Close();
}
return dsProducts.Tables[0];
}

public override object InitializeLifetimeService() {
return null;
}

// This method allows you to verify that the object is running remotely.
public string GetHostLocation() {
return AppDomain.CurrentDomain.FriendlyName;
}
}

[Serializable]
public class TaskCompleteEventArgs : EventArgs {
private string _result;

public string Result {
get{return _result;}
}

public TaskCompleteEventArgs(string result) {
this._result = result;
}
}
}

If you attempt to compile this code, you receive this error:
'RemoteObject.ProductsDB' does not implement interface member 'RemoteObjectInterface.IRemoteObject.TaskComplete'. 'RemoteObject.ProductsDB.TaskComplete' is either static, not public, or has the wrong return type.

and yet this code with the interface included in the same assembly compiles perfectly fine.

using System;
using System.Data;
using System.Timers;
using System.Data.SqlClient;

namespace RemoteObject {
public delegate void TaskCompleted(object sender, TaskCompleteEventArgs e);
public interface IRemoteObject {
event TaskCompleted TaskComplete;

void StartTask();
string GetHostLocation();
DataTable GetProducts();
}

public class ProductsDB : MarshalByRefObject, IRemoteObject {
private static string _connectDef = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;";
public event TaskCompleted TaskComplete;
private Timer tmr = new Timer();

public void StartTask() {
tmr.Interval = 10000;
tmr.Elapsed += new ElapsedEventHandler(tmrCallback);
tmr.Start();
}

private void tmrCallback(object sender, ElapsedEventArgs e) {
tmr.Enabled=false;
if (TaskComplete != null) {
TaskComplete(this,new TaskCompleteEventArgs("Task completed on server"));
}
}

// Get the data from the Products table and return them to the client.
public DataTable GetProducts() {
string SQL = "SELECT * FROM Products";

// Create ADO.NET objects.
SqlConnection cnProducts = new SqlConnection(_connectDef);
SqlCommand cmdProducts = new SqlCommand(SQL,cnProducts);
SqlDataAdapter daProducts = new SqlDataAdapter(cmdProducts);
DataSet dsProducts = new DataSet();

// Execute the command and retrieve the data.
try {
cnProducts.Open();
daProducts.Fill(dsProducts,"Products");
}
catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
finally {
cnProducts.Close();
}
return dsProducts.Tables[0];
}

public override object InitializeLifetimeService() {
return null;
}

// This method allows you to verify that the object is running remotely.
public string GetHostLocation() {
return AppDomain.CurrentDomain.FriendlyName;
}
}

[Serializable]
public class TaskCompleteEventArgs : EventArgs {
private string _result;

public string Result {
get{return _result;}
}

public TaskCompleteEventArgs(string result)
{
this._result = result;
}
}
}

This code compiles fine.
Am I missing something here?

John A Vonesh
Senior Developer
Tran-Tech Inc.

BAIT, n. A preparation that renders the hook more palatable. The best kind is beauty
GeneralRe: Remoting, Events, Interfaces and Assemblies Pin
Marc Clifton10-Feb-04 1:45
mvaMarc Clifton10-Feb-04 1:45 
GeneralReport Designer Pin
Itanium9-Feb-04 19:56
Itanium9-Feb-04 19:56 
GeneralRe: Report Designer Pin
Marc Clifton10-Feb-04 1:47
mvaMarc Clifton10-Feb-04 1:47 
GeneralRe: Report Designer Pin
Roger Alsing10-Feb-04 2:44
Roger Alsing10-Feb-04 2:44 
GeneralRe: Report Designer Pin
Marc Clifton10-Feb-04 3:50
mvaMarc Clifton10-Feb-04 3:50 
GeneralRe: Report Designer Pin
Itanium10-Feb-04 20:22
Itanium10-Feb-04 20:22 
GeneralTranslating from Delphi to C# - problem with objects. Pin
Andres Coder9-Feb-04 17:48
Andres Coder9-Feb-04 17:48 
GeneralRe: Translating from Delphi to C# - problem with objects. Pin
John Kuhn9-Feb-04 19:16
John Kuhn9-Feb-04 19:16 
GeneralRe: Translating from Delphi to C# - problem with objects. Pin
Andres Coder10-Feb-04 2:45
Andres Coder10-Feb-04 2:45 
GeneralRe: Translating from Delphi to C# - problem with objects. Pin
John Kuhn10-Feb-04 10:45
John Kuhn10-Feb-04 10:45 
GeneralCharacter array Pin
GetOn&GetGoing9-Feb-04 17:44
GetOn&GetGoing9-Feb-04 17:44 
GeneralRe: Character array Pin
Nick Parker9-Feb-04 18:28
protectorNick Parker9-Feb-04 18:28 
GeneralRe: Character array Pin
Marc Clifton10-Feb-04 1:49
mvaMarc Clifton10-Feb-04 1:49 
GeneralConversion from string to integer Pin
GetOn&GetGoing9-Feb-04 17:00
GetOn&GetGoing9-Feb-04 17:00 
GeneralRe: Conversion from string to integer Pin
John Kuhn9-Feb-04 17:06
John Kuhn9-Feb-04 17:06 
GeneralRe: Conversion from string to integer Pin
HAHAHA_NEXT10-Feb-04 4:09
HAHAHA_NEXT10-Feb-04 4:09 
GeneralConvert int to byte[4] Pin
Anonymous9-Feb-04 14:04
Anonymous9-Feb-04 14:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.