现在的位置: 首页 > 综合 > 正文

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

2013年05月11日 ⁄ 综合 ⁄ 共 2472字 ⁄ 字号 评论关闭

构建一个简单的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"

         

抱歉!评论已关闭.