使用WCF双工

来源:互联网 发布:华云数据服务有限公司 编辑:程序博客网 时间:2024/04/30 16:03
说明:在代码中调试运行没问题,但在IIS中没有部署成功


1.新建的是Silveright项目,承载了WEB项目,使用的是Silverlight4.0
2.在web项目中添加了一个WCF服务


IPATransServices.cs  主要代码


    [ServiceContract(CallbackContract = typeof(IPATransServicesCallBack))]
    public interface IPATransServices
    {
        [OperationContract]
        Dictionary<string, string> TransTable(string sTableName,
            string sSourceServer, string sDesServer, string sUserID, string sPass);
    }


    [ServiceContract]
    public interface IPATransServicesCallBack
    {
        [OperationContract(IsOneWay = true)]
        void RecMsg(int intMax, int intValue, string strDesc);
    }
    
    
PATransServices.svc.cs  主要代码


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


namespace PADataTrans.Web
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“PATransServices”。
    public class PATransServices : IPATransServices
    {
        public Dictionary<string, string> TransTable(string sTableName, 
                    string sSourceServer, string sDesServer, string sUserID, string sPass)
        {
            Dictionary<string, string> dicReturn = new Dictionary<string, string>();


            dicReturn["Result"] = "False";


            IPATransServicesCallBack client;
            client = OperationContext.Current.GetCallbackChannel<IPATransServicesCallBack>();


            for (int i = 1; i <= 1000; i++)
            {
                Thread.Sleep(100);
                client.RecMsg(1000, i, "我在回调");
            }


            dicReturn["Result"] = "True";
            return dicReturn;
        }
    }
}






由于要使用PollingDuplexHttpBinding来作为Service的endpoint的binding,所以需要对项目添加引用。右键项目,添加引用,选择浏览,然后关键的部分


Silverlight 版本 4 SDK 附带两个名为 System.ServiceModel.PollingDuplex.dll 的程序集。
WCF 双工服务使用其中一个程序集(位于 %ProgramFiles%\Microsoft SDKs\Silverlight\v4.0\Libraries\Server\ 目录下),
也就是我们要添加引用的程序集。另一个程序集(位于 %ProgramFiles%\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\ 目录下)
在 Silverlight 双工客户端中使用。


楼主的路径是C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Libraries\Server




说明:因为是在web项目中添加的WCF服务,所以在WEB项目中添加Server目录下的System.ServiceModel.PollingDuplex.dll
      在Silverlight客户端项目中添加Client目录下的System.ServiceModel.PollingDuplex.dll
      
      
web.config文件配置很重要,也容易出错


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


<!--
  有关如何配置 ASP.NET 应用程序的详细消息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->


<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>


  <system.serviceModel>
    <extensions>
      <bindingExtensions>
        <add name="pollingDuplexHttpBinding"
            type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement,
              System.ServiceModel.PollingDuplex,Version=4.0.0.0,Culture=neutral"/>
      </bindingExtensions>
    </extensions>
    <bindings>
      <pollingDuplexHttpBinding>
        <binding name="multipleMessagesPerPollPollingDuplexHttpBinding" duplexMode="MultipleMessagesPerPoll" maxOutputDelay="00:00:07">
        </binding>
      </pollingDuplexHttpBinding>
    </bindings>
    <services>
      <service name="PADataTrans.Web.PATransServices">
        <endpoint address="" binding="pollingDuplexHttpBinding"
            bindingConfiguration="multipleMessagesPerPollPollingDuplexHttpBinding"
                   contract="PADataTrans.Web.IPATransServices">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>





原创粉丝点击