子曾经曰过

  博客园  :: 首页  ::  ::  ::  :: 管理

IService1.cs

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfWithEvent
{
public enum EventType
{
Event1
= 1,
Event2
= 2,
Event3
= 3,
AllEvents
= Event1 | Event2 | Event3
}
// 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
[ServiceContract(SessionMode=SessionMode.Required,CallbackContract = typeof(IMyEvents))]
public interface IService1
{
[OperationContract(IsOneWay
= true)]
void BroadCast(string str);
[OperationContract]
void Subscribe(EventType mask);
[OperationContract]
void Unsubscribe(EventType mask);
}
public interface IMyEvents //被服务设为回调后自动成为服务契约一部分,不需要加ServiceContract特性
{
[OperationContract(IsOneWay
= true)]
void OnEvent1();
[OperationContract(IsOneWay
= true)]
void OnEvent2(int number);
[OperationContract(IsOneWay
= true)]
void OnEvent3(int number, string text);
}

// 使用下面示例中说明的数据协定将复合类型添加到服务操作

}

Service1.cs

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WcfWithEvent
{
// 注意: 如果更改此处的类名“IService1”,也必须更新 App.config 中对“IService1”的引用。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Service1 : IService1, IMyEvents
{
public Service1()
{

}

public delegate void GenericEventHandler();
public delegate void GenericEventHandler<T>(T t);
public delegate void GenericEventHandler<T, U>(T t, U u);
public delegate void GenericEventHandler<T, U, V>(T t, U u, V v);
public delegate void GenericEventHandler<T, U, V, W>(T t, U u, V v, W w); //可以定义到7个

static GenericEventHandler m_Event1 = delegate { }; //为什么是空的呢?
//static GenericEventHandler m_Event1 = new Service1().Fun;
static GenericEventHandler<int> m_Event2 = delegate { };
static GenericEventHandler<int, string> m_Event3 = delegate { };

public void Subscribe(EventType mask)
{
IMyEvents subscriber
= OperationContext.Current.GetCallbackChannel<IMyEvents>(); //回调接口


if ((mask & EventType.Event1) == EventType.Event1)
{
m_Event1
+= subscriber.OnEvent1; //?m_Event1 这东西是事件?是的话为什么没有event关键字定义?
}
if ((mask & EventType.Event2) == EventType.Event2)
{
m_Event2
+= subscriber.OnEvent2; //?m_Event1 这东西是事件?是的话为什么没有event关键字定义?
}
if ((mask & EventType.Event3) == EventType.Event3)
{
m_Event3
+= subscriber.OnEvent3; //?m_Event1 这东西是事件?是的话为什么没有event关键字定义?
}

}

public void Unsubscribe(EventType mask)
{
IMyEvents Unsubscriber
= OperationContext.Current.GetCallbackChannel<IMyEvents>(); //回调接口
if ((mask & EventType.Event1) == EventType.Event1)
{
m_Event1
-= Unsubscriber.OnEvent1; //?m_Event1 这东西是事件?是的话为什么没有event关键字定义?
}
}

public static void FireEvent(EventType eventType)
{
switch (eventType)
{
case EventType.Event1:
{
m_Event1();
return;
}
case EventType.Event2:
{
m_Event2(
42);
return;
}
case EventType.Event3:
{
m_Event3(
42, "hello");
return;
}
default:
{
throw new InvalidOperationException("unknow event type...");
}

}
}

static List<IMyEvents> m_ClientList = new List<IMyEvents>(); //存储客户端列表
static string tempsay;
public void BroadCast(string str)
{
//MessageBox.Show(OperationContext.Current.SessionId); //每次调用 Sessionid 都不一样,导致重复执行

IMyEvents subscriber
= OperationContext.Current.GetCallbackChannel<IMyEvents>(); //回调接口
//subscriber.OnEvent1();
tempsay = str;
if (!m_ClientList.Contains(subscriber))
{
m_ClientList.Add(subscriber);
}
//MessageBox.Show(m_ClientList.Count.ToString());
foreach (IMyEvents i in m_ClientList)
{
// i.OnEvent1(); //每个客户端都执行一次,就是广播

i.OnEvent3(
3,str);
}


//Type type = typeof(IMyEvents);
//MethodInfo methodInfo = type.GetMethod("DoSomething");
//foreach (IMyEvents subscriberM in m_ClientList)
//{
// try
// {
// methodInfo.Invoke(subscriberM, null);
// }
// catch
// { }
//}
}

public void OnEvent1()
{
//List < IMyEvents > m_ClientList = new List<IMyEvents>();
//Type type = typeof(IMyEvents);
//MethodInfo methodInfo = type.GetMethod("OnEvent1");
//foreach (IMyEvents subscriberM in m_ClientList)
//{
// try
// {
// methodInfo.Invoke(subscriberM, null);
// }
// catch
// { }
//}
MessageBox.Show("aaa");

}

public void OnEvent2(int number)
{ }

public void OnEvent3(int number, string text)
{ }


static void Main()
{ }




}
}

服务端  app.config

View Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout
="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal
="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize
="524288" maxReceivedMessageSize="65536"
messageEncoding
="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead
="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite
="Default" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8731/Design_Time_Addresses/WcfWithEvent/Service1/"
binding
="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IService1"
contract
="ServiceReference1.IService1" name="WSDualHttpBinding_IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

客户端

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EventClient.ServiceReference1;
using System.ServiceModel;

namespace EventClient
{
public partial class Form1 : Form, IService1Callback
{
InstanceContext context;
//这边很容易出错,如果定义放在Button事件里,会导致客户端反复new出新的代理类,导致客户端sessionid总是变化
Service1Client sc;

public Form1()
{
InitializeComponent();
context
= new InstanceContext(this); //参数要求是一个回调实例
sc = new Service1Client(context);
}

private void button1_Click(object sender, EventArgs e)
{
//IService1Callback subscriber = new Form1();

// sc.Subscribe(EventType.Event1); // 订阅事件,如何触发呢?
sc.BroadCast(textBox1.Text);
//sc.BroadCast("第二次");

}

public void OnEvent1()
{
//MessageBox.Show("客户端");
//richTextBox1.Text = textBox1.Text; //还实现不了

}
public void OnEvent2(int i)
{ }
public void OnEvent3(int i, string s)
{
richTextBox1.Text
+= s + "\n";
}
}

//public class MySubsriber : ServiceReference1.IService1Callback
//{
// public void OnEvent1()
// {
// MessageBox.Show("客户端");
// Form f = new Form1();
// f.Text = "aaa";
// }
// public void OnEvent2(int i)
// { }
// public void OnEvent3(int i, string s)
// { }
//}
}
posted on 2011-02-24 00:50  人的本质是什么?  阅读(446)  评论(0编辑  收藏  举报