wcf双工通讯

简介: 首先说一个服务引用不成功的解决办法:需要把服务端配置文件中的Binding换成:或: 下面是一个wcf的简单示例:服务契约代码:using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace WcfService{ // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IUser”。

首先说一个服务引用不成功的解决办法:

需要把服务端配置文件中的Binding换成:

<endpoint address="" binding="BasicHttpBinding" contract="WcfServiceLibrary1.IService1">
或:
 <endpoint address="" binding="wsDualHttpBinding" contract="WcfServiceLibrary1.IService1">

下面是一个wcf的简单示例:

服务契约代码:

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

namespace WcfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IUser”。
    [ServiceContract(Namespace = "localhost:60992/User",
    CallbackContract = typeof(ICallback))]
    public interface IUser
    {
        [OperationContract(IsOneWay=true)]
        void Add(double X, double y);
    }
    public interface ICallback
    {
        [OperationContract(IsOneWay = true)]
        void DisplayResult(double x, double y, double result);
    }
}

实现服务契约代码:

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

namespace WcfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“User”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 User.svc 或 User.svc.cs,然后开始调试。
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class User : IUser
    {
        public void Add(double x, double y)
        {
            double result = x + y;
            ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
            callback.DisplayResult(x, y, result);
        }
    }
}

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime appRequestQueueLimit="100" executionTimeout="900" useFullyQualifiedRedirectUrl="false" maxRequestLength="102400" shutdownTimeout="900" targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfService.User">
        <!--<endpoint address="http://localhost:60992/User" behaviorConfiguration="WcfService.Service1AspNetAjaxBehavior"
          binding="webHttpBinding" contract="WcfService.IUser" />-->
          <endpoint address="" binding="wsDualHttpBinding" contract="WcfService.IUser"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WcfService.Service1AspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false"
      multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
        在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

此配置注意有事后出错请修改红色部分。

客户端代码:

首先在客户端使用服务引用之前要想成功使用回调必须实现回调函数:

    class CallbackHandler : ServiceReference1.IUserCallback
    {
        public void DisplayResult(double x, double y, double result)
        {
            Console.WriteLine("x + y = {2} when x = {0} and y = {1}", x, y, result);
        }
    }

实现回到函数之后使用能够控制到调用服务使用:

     static void Main(string[] args)
        {
            InstanceContext instanceContext = new InstanceContext(new CallbackHandler());
            ServiceReference1.UserClient c = new ServiceReference1.UserClient(instanceContext);
            Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
            Console.WriteLine();
            c.Add(1.1, 2.2);
            Console.ReadLine();
        }

最后附带源码下载地址:https://pan.baidu.com/s/1hr6zPCK

作者:YanBigFeg —— 颜秉锋

出处:http://www.cnblogs.com/yanbigfeg

本文版权归作者和博客园共有,欢迎转载,转载请标明出处。如果您觉得本篇博文对您有所收获,觉得小弟还算用心,请点击右下角的 [推荐],谢谢!

目录
相关文章
|
JavaScript
|
SQL 监控 NoSQL
Wcf通讯基础框架方案(六)解决方案说明以及源代码
源代码见 http://wcfextension.codeplex.com/ 注意: 1) 本来没打算这么早开源,这只是一个比较原始的实现,请勿直接在商业环境使用 2) 请注意本框架的授权条款Apache License 2.
670 0
|
监控 NoSQL
Wcf通讯基础框架方案(一)——基本结构
由于希望使用Wcf作为公司内的通讯框架,因此基于Wcf进行了一些扩展,主要的目的有以下几个方面: 1) 希望减少客户端调用的复杂度,调用方式简化为WcfServiceLocator.Create().Add(1,2)。
770 0
|
XML 监控 数据格式
Wcf通讯基础框架方案(二)——集中配置
从这次开始在几个方面简单阐述一下实现,集中配置是这个框架很大的一个目的,首先在数据库中会有这么一些表: 其实可以看到这些表的结构,应该是和配置节点中的层次有对应的 1) Service表描述的是服务,主要保存服务行为以及服务的配置。
578 0
|
监控
Wcf通讯基础框架方案(三)——客户端
假设定义了一个服务契约: [ServiceContract(Namespace = "WcfExtension.Services.Interface")] public interface ITestService { [OperationContract] ...
689 0
|
监控 负载均衡 缓存
Wcf通讯基础框架方案(四)——横切日志
在第一篇文章中已经列出了几种日志的概览: 所有的日志都有一个最终基类,来看看这个类: [DataContract(Namespace = "WcfExtension")] [KnownType(typeof(WcfExceptionInfo))] [KnownType(t...
609 0
|
缓存 NoSQL Redis
Wcf通讯基础框架方案(五)——更新通知
对于负载均衡环境,多服务器内存中缓存数据的话,需要解决的一个很重要的问题就是一旦数据库中数据有更新,怎么让缓存的数据立即更新? 如果可以容忍延迟或是差异性的话,可以考虑缓存的数据有一个过期时间。但是,最好的方式还是采用通知方式,或者说发布订阅方式。
760 0
|
9月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一