`
feiliboos
  • 浏览: 679843 次
文章分类
社区版块
存档分类
最新评论

wcf/web service 编码

 
阅读更多
using System;
using System.CodeDom;
using System.IO;
using System.Text;
using System.Web.Services.Configuration;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
 
// The YMLAttribute allows a developer to specify that the YML SOAP
// extension run on a per-method basis.  The disabled property
// turns reversing the XML on and off. 

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
public class YMLAttribute : SoapExtensionAttribute 
{
  int priority = 0;
  bool disabled = false;
       
  public YMLAttribute() : this(false) {}
  public YMLAttribute(bool disabled) 
  {
     this.disabled = disabled;
  }
      
  public override Type ExtensionType 
  {
    get { return typeof(YMLExtension); }
  }
  public override int Priority 
  {
    get { return priority; }
    set { priority = value; }
  }

  public bool Disabled 
  { 
    get { return disabled; }
    set { disabled = value; }
  }
}

public class YMLExtension : SoapExtension 
{
  bool disabled = false;
  Stream oldStream;
  Stream newStream;

  public override object GetInitializer(LogicalMethodInfo methodInfo,
                                        SoapExtensionAttribute attribute)
  {
    YMLAttribute attr = attribute as YMLAttribute;
    if (attr != null) return attr.Disabled;
       return false;
  }

  public override object GetInitializer(Type serviceType) 
  {
        return false;
  }

  public override void Initialize(object initializer) 
  {
     if (initializer is Boolean) disabled = (bool)initializer;
  }

  public override Stream ChainStream(Stream stream) 
  {
     if (disabled) return base.ChainStream(stream);
     oldStream = stream;
     newStream = new MemoryStream();
     return newStream;
  }

  public override void ProcessMessage(SoapMessage message) 
  {
    if (disabled) return;
    switch (message.Stage) 
    {
      case SoapMessageStage.BeforeSerialize:
        Encode(message);
        break;
      case SoapMessageStage.AfterSerialize:
        newStream.Position = 0;
        Reverse(newStream, oldStream);
        break;
      case SoapMessageStage.BeforeDeserialize:
        Decode(message);
        break;
      case SoapMessageStage.AfterDeserialize:
        break;
    }
  }        
  void Encode(SoapMessage message) 
  {
     message.ContentType = "text/yml";
  }

  void Decode(SoapMessage message) 
  {
   if (message.ContentType != "text/yml") 
     throw new Exception("invalid content type:" + message.ContentType);
   Reverse(oldStream, newStream);
   newStream.Position = 0;
   message.ContentType = "text/xml";
  }

  void Reverse(Stream from, Stream to) 
  {
    TextReader reader = new StreamReader(from);
    TextWriter writer = new StreamWriter(to);
    string line;
    while ((line = reader.ReadLine()) != null) 
    {
      StringBuilder builder = new StringBuilder();
      for (int i = line.Length - 1; i >= 0; i--) 
      {
        builder.Append(line[i]);
      }
      writer.WriteLine(builder.ToString());
    }
    writer.Flush();
  }
}

// The YMLReflector class is part of the YML SDFE; it is
// called during the service description generation process.
public class YMLReflector : SoapExtensionReflector 
{
  public override void ReflectMethod() 
  {
    ProtocolReflector reflector = ReflectionContext;
    YMLAttribute attr = (YMLAttribute)reflector.Method.GetCustomAttribute(
                        typeof(YMLAttribute));
    // If the YMLAttribute has been applied to this Web service
    // method, adds the XML defined in the YMLOperationBinding class.
    if (attr != null) 
    {
      YMLOperationBinding yml = new YMLOperationBinding();
      yml.Reverse = !(attr.Disabled);
      reflector.OperationBinding.Extensions.Add(yml);
    }
  }
}
  
// The YMLImporter class is part of the YML SDFE; it is called when
// a proxy class is generated for each Web service method the proxy
// class communicates with.  The class checks whether the service
// description contains the XML that this SDFE adds to a service
// description.  If it exists, then the YMLExtension is applied to the
// method in the proxy class.
public class YMLImporter : SoapExtensionImporter 
{
  public override void ImportMethod(CodeAttributeDeclarationCollection
                                    metadata)
 {
    SoapProtocolImporter importer = ImportContext;
   // Checks whether the XML specified in the YMLOperationBinding is in
   // the service description.
   YMLOperationBinding yml = (YMLOperationBinding)
       importer.OperationBinding.Extensions.Find(
       typeof(YMLOperationBinding));
   if (yml != null)
   {
     // Only applies the YMLAttribute to the method when the XML should
     // be reversed.
     if (yml.Reverse)
     {
       CodeAttributeDeclaration attr = new CodeAttributeDeclaration(
            typeof(YMLAttribute).FullName);
       attr.Arguments.Add(new CodeAttributeArgument(new
         CodePrimitiveExpression(true)));
       metadata.Add(attr);
     }
   }
 }
}

// The YMLOperationBinding class is part of the YML SDFE; it is the
// class that is serialized into XML and placed in the service
// description.
[XmlFormatExtension("action", YMLOperationBinding.YMLNamespace,
                    typeof(OperationBinding))]
[XmlFormatExtensionPrefix("yml", YMLOperationBinding.YMLNamespace)]
public class YMLOperationBinding : ServiceDescriptionFormatExtension 
{
   private Boolean reverse;
   public const string YMLNamespace = "http://www.contoso.com/yml";

   [XmlElement("Reverse")]
   public Boolean Reverse 
   {
     get { return reverse; }
     set { reverse = value; }
   }
}
分享到:
评论

相关推荐

    wcfservice示例

    "wcfservice示例"通常是指一个用于演示如何创建、配置和使用WCF服务的实例。在这个示例中,我们将会探讨WCF服务的基本概念、结构以及如何通过`WcfService1`这个文件来理解和学习WCF服务。 1. **WCF服务基础** WCF...

    WCF 关于TCP/IP的通信

    绑定定义了服务如何与外部世界通信的细节,包括使用的传输协议、编码格式以及安全性设置。在WCF中,预定义了多种绑定,其中NetTcpBinding就是专为利用TCP/IP设计的。NetTcpBinding提供了高性能、低开销的通信,同时...

    WCF1.2简单的控制台服务端/客户端

    WCF1.2是WCF的一个版本,它提供了一种统一的方式来进行进程间通信(IPC)、网络通信、Web服务等不同形式的服务交互。在这个实例中,我们将探讨如何创建一个简单的WCF控制台服务端和客户端。** ### 1. WCF服务端创建...

    WCFService.rar_c++ webservice_it_wcf WebService_webservice

    在本压缩包"WCFService.rar"中,包含了一个C++实现的WCF Web Service示例,这将帮助我们了解如何在C++中利用WCF技术来开发Web服务。** **WCF Web Service** 是一种基于HTTP协议的,允许不同系统间进行数据交换的...

    第五篇 Web Service.rar_service_web .net_web service

    在.NET框架中,Web服务的实现主要依赖于ASMX(ASP.NET Web Service)和WCF(Windows Communication Foundation)。本篇文章将深入探讨.NET框架下创建和使用Web服务的相关知识点。 一、ASMX Web服务 1. ASMX简介:...

    XML Web Service开发教程

    例如,ASMX允许开发者使用.NET Framework中的Web方法创建Web Service,而WCF提供了更高级的功能,如安全、事务和消息队列。 开发过程中,还将涉及SOAP消息的编码和解码,以及错误处理。理解HTTP状态代码和SOAP错误...

    web service文档的学习

    在实际工作中,你可能还需要学习一些相关的工具和技术,如WS-*标准(如WS-Security、WS-ReliableMessaging等),以及现代Web服务框架,如JAX-WS(Java API for XML Web Services)和.NET的WCF(Windows ...

    delphi 7调用wcf

    这可以通过查看服务的WSDL(Web Service Description Language)文档获取,WSDL描述了服务的结构和交互方式。 2. **创建客户端代理**:在Delphi中,你需要一个客户端代理来与WCF服务通信。由于Delphi 7不直接支持...

    WCF系列课程 webcast

    - 随着WebAPI和ASP.NET Core的出现,WCF在现代Web开发中的地位有所下降。WebAPI更专注于RESTful服务,而ASP.NET Core提供了更现代、轻量级的跨平台解决方案。 在本系列课程的webcast中,您将逐步学习这些概念,并...

    WEB服务、Remoting实例、WCF实例

    2. **实现服务**:在.NET中,可以通过ASP.NET Web Service或WCF来实现服务端的业务逻辑。 3. **发布服务**:将服务部署到IIS服务器,并通过URL公开。 4. **消费服务**:客户端通过SOAP请求与服务交互,获取或发送...

    一个简单的WCF例子

    <service name="WT.Study.WCF.Service1"> <endpoint address="" binding="wsHttpBinding" contract="WT.Study.WCF.IService1" /> <add baseAddress="http://localhost:8732/Design_Time_Addresses/WT.Study....

    WCF

    WCF集成了多种通信技术,如Web服务、面向消息的中间件、TCP/IP等,为开发人员提供了丰富的功能和灵活性。 在WCF中,服务是主要的工作单元,它们可以通过多种协议和传输方式暴露接口。服务契约定义了服务的行为,即...

    WCF自定义绑定与Webservice传输性能比较

    当我们谈论“WCF自定义绑定与Web Service传输性能比较”时,实际上是在探讨这两种技术在数据传输效率、灵活性和可配置性等方面的差异。 首先,让我们深入了解WCF的自定义绑定。自定义绑定是WCF的一项重要特性,它...

    WCF 入门教程二demo

    1. 在Visual Studio中,右键点击项目,选择“启动调试”以运行WCF测试客户端(如果是Web应用,可能需要IIS Express或IIS)。 2. 测试客户端将显示可用的服务和操作,可以用来调用服务并查看响应。 五、创建WCF服务...

    基于Web Service的3G手机应用开发入门讲座

    - 开发语言:Java、C#、Python等,通常使用支持Web Service调用的库或框架,如Java的JAX-WS,.NET的ASMX或WCF。 - 开发环境:Eclipse、Visual Studio等,配备相应的插件支持Web Service开发。 - 测试工具:SoapUI...

    .net 调用Web Service 发送短信

    综上所述,.NET调用Web Service发送短信涉及多个层面的技术,从理解Web Service原理到实际编码实现,再到安全性、性能优化等,都是开发者需要掌握的关键技能。确保遵循良好的编程实践和文档,可以使这项任务变得更加...

    一个WCF小实例

    <service name="WcfService1.Service1"> <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1"/> <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfService...

    wcf c# 服务器 客户端 示例

    每种绑定定义了不同的传输协议、消息编码和安全性设置。例如,`wsHttpBinding`适合跨域通信,提供强类型的安全性,而`NetTcpBinding`则提供了更高的性能和更低的开销,但仅限于同一网络内的通信。 四、WCF的服务...

    Wcf 简单运行Demo程序

    <service name="WcfServices.CalculatorService"> <endpoint address="" binding="basicHttpBinding" contract="WcfServices.ICalculatorService" /> <add baseAddress="http://localhost:8731/Design_Time_...

    WCF实现的计算器

    1. **服务宿主(Service Host)**:WCF服务必须运行在一个宿主进程中,这个宿主可以是IIS、Windows Service或自定义的应用程序。 2. **终结点(Endpoint)**:服务通过终结点与外界通信,包含地址(Address)、绑定...

Global site tag (gtag.js) - Google Analytics