• 消息(6)——WCF,构建简单的WCF服务,MTOM编码


    构建一个简单的WCF服务

    Web服务类似的步骤由IIS进行宿主服务。建立的步骤:

    1 新建3.5网站

    2 添加WCF服务,自动生成契约接口与实现,这里改动一下,添加个字串参数:

    [ServiceContract]

    public interface IFirstService

    {

        [OperationContract]

        void DoWork(string strContent);

    }

     

    服务中的方法什么都不用做。

    public class FirstService : IFirstService

    {

        public void DoWork(string strContent)

        {

        }

    }

    在添加WCF服务时,会自动在配置文件中添加必要的章节,例如绑定和元数据发布。

     

    <system.serviceModel>

      <behaviors>

        <serviceBehaviors>

          <behavior name="FirstServiceBehavior">

            <serviceMetadata httpGetEnabled="true" />

            <serviceDebug includeExceptionDetailInFaults="false" />

          </behavior>

        </serviceBehaviors>

      </behaviors>

      <services>

        <service behaviorConfiguration="FirstServiceBehavior"

                 name="FirstService">

          <endpoint address=""

                    binding="basicHttpBinding"

                    contract="IFirstService">

            <identity>

              <dns value="localhost" />

            </identity>

          </endpoint>

          <endpoint address="mex"

                    binding="mexHttpBinding"

                    contract="IMetadataExchange" />

        </service>

      </services>

    </system.serviceModel>

     

    这里把绑定改一下,改为basicHttpBinding

     

    然后在测试端:新建立类库项目,由发布的元数据生成代理,然后进行服务请求:

    [Test]

    public void Test()

    {

    FirstInstance.FirstServiceClient client =

    New FirstInstance.FirstServiceClient();

        client.DoWork("this is a test!");

    }

     

    现在看一下消息包的情况:

    这是客户端请求的信息:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

      <s:Body>

        <DoWork xmlns="http://tempuri.org/">

          <strContent>this is a test!</strContent>

        </DoWork>

      </s:Body>

    </s:Envelope>

    这是服务端回应的信息:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

      <s:Body>

        <DoWorkResponse xmlns="http://tempuri.org/"/>

      </s:Body>

    </s:Envelope>

     

    对于BasicHttpBinding来说,它通过http来发送soap1.1的消息。这个绑定用于配置和公开能够与基于asmxweb service和客户端进行通信的终结点,以及符合ws-i basic profile 1.1标准的其它服务。

     

    通过设置WCF绑定的消息编码格式来设置传输过程中所使用的编码:

    <basicHttpBinding>

    <binding name="firstBinding" messageEncoding="Text">

    </binding>

    </basicHttpBinding>

     

    现设置BasicHttp绑定的消息编码为文本,当传输二进制附件时,会怎么用base64编码:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

      <s:Body>

        <SaveImage xmlns="www.self001.com">

          <bb>GMDggAOw==</bb>

        </SaveImage>

      </s:Body>

    </s:Envelope>

     

    其中附件部分我省略了大部分,只留一小段。

    当使用MTOM编码格式时:

    --uuid:deef670a-dfd7-4a71-8d89-face6ac975dd+id=1

    Content-ID: <http://tempuri.org/0>

    Content-Transfer-Encoding: 8bit

    Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

     

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

      <s:Body>

        <SaveImage xmlns="www.self001.com">

          <bb>

            <xop:Include

              href="cid:http%3A%2F%2Ftempuri.org%2F1%2F634057273450156250"

              xmlns:xop="http://www.w3.org/2004/08/xop/include"/>

          </bb>

        </SaveImage>

      </s:Body>

    </s:Envelope>

    --uuid:deef670a-dfd7-4a71-8d89-face6ac975dd+id=1

    Content-ID: <http://tempuri.org/1/634057273450156250>

    Content-Transfer-Encoding: binary

    Content-Type: application/octet-stream

    GIF89ad……省略

    --uuid:deef670a-dfd7-4a71-8d89-face6ac975dd+id=1--

     

    这与WSE3中使用的MTOM是相同的。

    博客园大道至简

    http://www.cnblogs.com/jams742003/

    转载请注明:博客园

  • 相关阅读:

    泛型
    特性
    2.Linux基础入门
    1.Linux介绍及系统安装
    php十七种设计模式适用场合总结
    转载zendframework 插件方式
    常用的Jquery代码片段,没事的时候研究一下
    Zend Framework1.9 模块化搭建
    PHP的设计模式总结命令链模式
  • 原文地址:https://www.cnblogs.com/jams742003/p/1702277.html
Copyright © 2020-2023  润新知