WCF, executing Events?
While continuing to learn WCF, I'm trying to make it work with events.
In this example, upon a button click in a form, I want my wcf service to execute and event that would be trigger something in other form that are connected to this service.
That's the code for the Form1.
public Form1()
{
InitializeComponent();
client.TimeShowEvent += new EventHandler(TimeShowEvent);
///// SAYS THAT IT DOES NOT CONTAIN A DEFINITION FOR TimeShowEvent
}
MyWcfService1.IfaceServiceClient client = new MyWcfService1.IfaceServiceClient();
private void button2_Click(object sender, EventArgs e)
{
try
{
MyWcfService1.IfaceServiceClient client = new MyWcfService1.IfaceServiceClient();
client.passTime();
}
catch
{
MessageBox.Show("Service not availabe!");
}
}
void TimeShowEvent(object sender, EventArgs e)
{
textBox2.Text = client.timestring;
//// SAYS THAT IT DOES NOT CONTAIN A DEFINITION FOR timestring
}
and for the service:
namespace wcfLib
{
[ServiceContract]
public interface IfaceService
{
[Ope开发者_运维技巧rationContract]
int wordLen(string word);
[OperationContract]
string passTime();
///// DO I NEED TO SOMEHOW DECLARE THE VARIABLES ( timestring ) AND EVENTS ( TimeShowEvent ) HERE?
}
}
Service implementation:
public class StockService : IfaceService
{
public event EventHandler TimeShowEvent;
public string timestring = "none";
public string passTime()
{
TimeShowEvent(this, new EventArgs());
timestring = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
return "";
}
public int wordLen(string word)
{
return word.Length;
}
}
Service host app:
public class Service
{
static void Main()
{
ServiceHost serviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/wcfLib"));
serviceHost.AddServiceEndpoint(typeof(IfaceService), new BasicHttpBinding(), "");
serviceHost.Open();
Console.WriteLine("Press return to terminate the service");
Console.ReadLine();
serviceHost.Close();
}
}
Do I need to somehow declare events and variables in [ServiceContract]? Is so, how?...
Thanks! :)
When you create a service reference to your service, your client gets access to the service methods - and only to the service methods.
The event handler TimeShowEvent
in your service implementation class will be present and usable on the server-side only - it will not be available on the client side.
If you want to have something to call, you need to define another service method - those are "mirrored" in the client-side proxy class - and only those.
The only connection your client-side proxy and your server share are the service methods defined in your service contract, and the data that gets passed for those methods - as serialized (XML) messages. There's no "magic link" between client and server - the client can't "reach into" the server class and read stuff from there or call events on that class. There is no "remote object" connection between the two.
Try something like this:
string timeString;
private void button2_Click(object sender, EventArgs e)
{
try
{
//You already declared client, so you don't need to do it again.
//Assign the value from your wcf call to a local variable
timeString = client.passTime();
}
catch
{
MessageBox.Show("Service not availabe!");
}
}
and modify the service to return a string:
public string passTime()
{
TimeShowEvent(this, new EventArgs());
return DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
}
and your event:
void TimeShowEvent(object sender, EventArgs e)
{
textBox2.Text = timeString; //this is your local variable
}
And your event handler needs to be in the client. The wcf service will not know anything about the event that called it or used its result.
精彩评论