WCF响应:省略SOAP结构

时间:2015-03-30 15:03:46

标签: .net web-services wcf

我开发了一个完美的.NET WCF Web服务,可以生成SOAP响应。

我的问题是响应的SOAP格式,导致我的最终客户无法使用我的网络服务。 (他只希望回复的正文内容。)

为了更好地解释这种情况,我重新创建了一个名为“Calculator”的WCF演示;一个简单的Web服务,执行两个数字的总和。 (1 + 1)

当我在SOAP UI上测试Web服务时,我有以下响应:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
   <s:Header>
      <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <u:Timestamp u:Id="_0">
            <u:Created>2015-03-30T14:31:44.759Z</u:Created>
            <u:Expires>2015-03-30T14:36:44.759Z</u:Expires>
         </u:Timestamp>
      </o:Security>
   </s:Header>
   <s:Body>
      <SumResponse xmlns="http://MY-PC">
         <SumResult>2</SumResult>
      </SumResponse>
   </s:Body>
</s:Envelope>

虽然,我的最终客户希望收到以下回复:(全部通过新的wsdl

<?xml version="1.0" encoding="UTF-8"?>
<SumResponse xmlns="http://MY-PC">
    <SumResult>2</SumResult>
</SumResponse>

有没有办法在WCF Web服务的响应中省略SOAP格式? (可能使用.NET标准系统库)

如果没有,有什么方法可以实现.NET中的最终结果吗?

注意
我应该使用.NET framework 4.0,响应应该有:
内容类型'text / xml; charset = utf-8'(与SOAP 1.1格式一样)

1 个答案:

答案 0 :(得分:0)

您可以尝试使用消息调度程序,但是我不确定您是否愿意这样做。如果您创建一个界面,您的客户端可以轻松获得完整的答案,但只返回一个只包含值的对象。 无论如何,使用消息检查器,您可以编辑要发送的消息,这是一个示例:

//First you will need to implement 2 interfaces
//IDispatchMessageInspector or IClientMessageInspector and IServiceBehavior or IEndpointBehavior

public class MustUnderstandMessageInspector : IDispatchMessageInspector
{

    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
    {
        //Here you can use the ref request(Remember it is a ref, careful what you do with it)
        //request is the message you just received
        SaveInDatabase(request);
        return null;
    }

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        //reply is a ref like request and it is the message you are sending
        throw new NotImplementedException();
    }
}

// To be able to implement your MessageInspector in your service you need to create a behavior.
//this behavior will have to be added to your server.

public class UnderstandBehavior : IServiceBehavior
{


    void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
        //throw new NotImplementedException();
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        //This will Add your inspector to every dispatcher inside serviceHostBase
        //you will have to use the one your service is on
        foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
            {
                epDisp.DispatchRuntime.MessageInspectors.Add(new MustUnderstandMessageInspector());
            }
        }
    }

    void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        //throw new NotImplementedException();
    }
}

完成两个界面后,您必须将行为添加到您的服务中。

{
        svcHost = new ServiceHost(typeof(Service), eventendpoint);
        svcHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding, "Endpoint");
        //Add Service Behavior
        UnderstandBehavior behavior = new UnderstandBehavior();
        svcHost.Description.Behaviors.Add(behavior);
        //////
        svcHost.Open();
        textBox1.Text = textBox1.Text + "\n\nService is Running";
        svcHost.Close();
}

如图所示,您还可以编辑您只需使用所需消息检查器的传入消息。