WCF 双工模式

扫码查看

WCF之消息模式分为:
1、请求/答复模式
2、单向模式
3、双工模式

其中,请求/答复模式,在博文:

WCF 入门教程一(动手新建第一个WCF程序并部署)

WCF 入门教程二

中进行了详细介绍,此处将主要介绍:单向模式与双工模式。

1、首先,先创建一个WCF应用程序:

WCF 双工模式-LMLPHP

创建完成后,目录如下:

WCF 双工模式-LMLPHP

2、删除IService1.cs和Serivce1.svc,或者修改名称为:CalculateService.svc与ICalculateService.cs后,显示如下:

WCF 双工模式-LMLPHP

3、ICalculateService.cs文件内容如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. namespace WcfDuplexTest
  9. {
  10. // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”。
  11. [ServiceContract(Namespace = "http://blog.csdn.net/jiankunking",
  12. SessionMode = SessionMode.Required, CallbackContract = typeof(ICalculatorDuplexCallback))]
  13. public interface ICalculateService
  14. {
  15. [OperationContract(IsOneWay = true)]
  16. void GetData(string value);
  17. [OperationContract]
  18. CompositeType Clear();
  19. // TODO: 在此添加您的服务操作
  20. }
  21. /*我们可以看到它有一个ICalculatorDuplexCallback的接口,由于它在ServiceContract中被标记为CallbackContract = typeof(ICalculatorDuplexCallback),所以它用于客户端回调。
  22. * 意即,服务端可以通过此接口中的方法将数据发送给客户端,客户端只需要实现此接口,即可接收到服务端发送过来的消息。*/
  23. public interface ICalculatorDuplexCallback
  24. {
  25. [OperationContract(IsOneWay = true)]
  26. void ComplexCalculate(string result);
  27. [OperationContract]
  28. string GetComplexCalculateResult(string value);
  29. }
  30. // 使用下面示例中说明的数据协定将复合类型添加到服务操作
  31. [DataContract]
  32. public class CompositeType
  33. {
  34. bool boolValue = true;
  35. string stringValue = "Hello ";
  36. [DataMember]
  37. public bool BoolValue
  38. {
  39. get { return boolValue; }
  40. set { boolValue = value; }
  41. }
  42. [DataMember]
  43. public string StringValue
  44. {
  45. get { return stringValue; }
  46. set { stringValue = value; }
  47. }
  48. }
  49. }

4、CalculateService.svc文件中的内容:

  1. <pre name="code" class="csharp">using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. namespace WcfDuplexTest
  9. {
  10. /*ServiceContract的SessionMode
  11. 用于Contract上的枚举, 3种:
  12. Allowed: 指定协定永支持会话
  13. Required:指定协定必须会话绑定,否则将引发异常。BasicHttpBinding不支持会话,所以当使用BasicHttpBinding的时候毕会异常;
  14. NotAllowed:指定协定永不支持启动会话的绑定。*/
  15. // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service”。
  16. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
  17. public class CalculateService : ICalculateService
  18. {
  19. //声明一个ICalculatorDuplexCallback接口的对象
  20. ICalculatorDuplexCallback callback = null;
  21. //CalculateService类的构造方法
  22. public CalculateService()
  23. {
  24. //实例化一个ICalculatorDuplexCallback
  25. callback = OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
  26. }
  27. public void GetData(string value)
  28. {
  29. //服务端调用客户端的ComplexCalculate方法
  30. callback.ComplexCalculate(value);
  31. }
  32. public CompositeType Clear()
  33. {
  34. CompositeType composite = new CompositeType();
  35. composite.BoolValue = false;
  36. //服务端调用客户端的GetComplexCalculateResult方法
  37. composite.StringValue = "测试回调客户端带有返回值的方法\r\n " + callback.GetComplexCalculateResult("客户端方法:GetComplexCalculateResult");
  38. return composite;
  39. }
  40. }
  41. }</pre>

5、修改Web.config的配置文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3. <system.web>
  4. <compilation debug="true" targetFramework="4.0" />
  5. </system.web>
  6. <system.serviceModel>
  7. <!--WCF应用程序 一下services节点需要自己手动添加-->
  8. <services>
  9. <service name="WcfDuplexTest.CalculateService">
  10. <endpoint address="" binding="wsDualHttpBinding" contract="WcfDuplexTest.ICalculateService">
  11. <identity>
  12. <dns value="localhost" />
  13. </identity>
  14. </endpoint>
  15. <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  16. <host>
  17. <baseAddresses>
  18. <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfDuplexTest/CalculateService/" />
  19. </baseAddresses>
  20. </host>
  21. </service>
  22. </services>
  23. <behaviors>
  24. <serviceBehaviors>
  25. <behavior>
  26. <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
  27. <serviceMetadata httpGetEnabled="true"/>
  28. <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
  29. <serviceDebug includeExceptionDetailInFaults="true"/>
  30. </behavior>
  31. </serviceBehaviors>
  32. </behaviors>
  33. <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  34. </system.serviceModel>
  35. <system.webServer>
  36. <modules runAllManagedModulesForAllRequests="true"/>
  37. </system.webServer>
  38. </configuration>

6、新建winform客户端进行测试

7、添加服务端引用:

WCF 双工模式-LMLPHP
小注:

今天在vs2015中新建WCF类库,又能找到服务了WCF 双工模式-LMLPHP

WCF 双工模式-LMLPHP

8、客户端代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.ServiceModel;
  10. using FormTest.CalculateService;
  11. namespace FormTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. // Construct InstanceContext to handle messages on callback interface
  22. InstanceContext instanceContext = new InstanceContext(new CallbackHandler());
  23. // Create a client
  24. CalculateService.CalculateServiceClient client = new CalculateService.CalculateServiceClient(instanceContext);
  25. client.GetData("客户端 传入 参数 测试 GetData");
  26. MessageBox.Show("GetData 调用完成!");
  27. //WCF 数据契约的用途
  28. CompositeType composite = client.Clear();
  29. MessageBox.Show("Clear 调用成功 \r\n" + composite.StringValue);
  30. }
  31. }
  32. /// <summary>
  33. /// 以为能找到服务端里的ICalculatorDuplexCallback接口,谁知道服务端的接口ICalculatorDuplexCallback
  34. /// 是ICalculateServiceCallback的形式出现在客户端的
  35. /// </summary>
  36. //修改回调回调函数的通知线程,将其改为在非UI线程中执行。
  37. //WCF中可以通过在客户端回调函数类中的CallbackBehaviorAttribute中控制这一行为
  38. //从而解决UI死锁问题
  39. [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false)]
  40. public class CallbackHandler : CalculateService.ICalculateServiceCallback
  41. {
  42. public void ComplexCalculate(string result)
  43. {
  44. MessageBox.Show(result.ToString());
  45. }
  46. public string GetComplexCalculateResult(string result)
  47. {
  48. return result;
  49. }
  50. }
  51. }

小注:
在WCF回调中需要注意死锁问题
1、如果WCF中暴露出来的操作,没有返回值,则可以通过就是设置回调操作
IsOneWay=true,这样回调以后立即释放服务实例,不需要等待客户端响应消息,也可以避免死锁。
2、如果WCF中暴露出来的操作,有返回值,则需要通过,修改服务的ServiceBehavior的ConcurrencyMode为Reentrant或Multiple即可。
此时,服务端的死锁问题搞定了。
下面就需要考虑客户端的死锁问题了
客户端的死锁问题,通过在客户端回调函数类中的CallbackBehaviorAttribute中控制这一行为

死锁具体分析可以参考:点击打开链接

demo代码:点击打开链接

服务端死锁时的提示信息:

  1. 未处理 System.ServiceModel.FaultException`1
  2. HResult=-2146233087
  3. Message=此操作将死锁,因为在当前邮件完成处理以前无法收到答复。如果要允许无序的邮件处理,则在 ServiceBehaviorAttribute 上指定可重输入的或多个 ConcurrencyMode。
  4. Source=mscorlib
  5. Action=http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault
  6. StackTrace:
  7. Server stack trace:
  8. 在 System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
  9. 在 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
  10. 在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
  11. 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
  12. 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
  13. Exception rethrown at [0]:
  14. 在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
  15. 在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
  16. 在 FormTest.CalculateService.ICalculateService.GetData(Int32 value)
  17. 在 FormTest.CalculateService.CalculateServiceClient.GetData(Int32 value) 位置 E:\WorkSpace\WorkSpaceTest\WcfDuplexTest\FormTest\Service References\CalculateService\Reference.cs:行号 124
  18. 在 FormTest.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\WorkSpace\WorkSpaceTest\WcfDuplexTest\FormTest\Form1.cs:行号 27
  19. 在 System.Windows.Forms.Control.OnClick(EventArgs e)
  20. 在 System.Windows.Forms.Button.OnClick(EventArgs e)
  21. 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  22. 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  23. 在 System.Windows.Forms.Control.WndProc(Message& m)
  24. 在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
  25. 在 System.Windows.Forms.Button.WndProc(Message& m)
  26. 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  27. 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  28. 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  29. 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  30. 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
  31. 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  32. 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  33. 在 System.Windows.Forms.Application.Run(Form mainForm)
  34. 在 FormTest.Program.Main() 位置 E:\WorkSpace\WorkSpaceTest\WcfDuplexTest\FormTest\Program.cs:行号 18
  35. 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
  36. 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  37. 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  38. 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  39. 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  40. 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  41. 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  42. 在 System.Threading.ThreadHelper.ThreadStart()
  43. InnerException:

小注:

WCF 双工模式-LMLPHP

04-26 15:51
查看更多