使用序列化对象调用服务方法

时间:2014-06-06 12:38:21

标签: c# xml wcf

我有一个带有post方法的WCF服务。这需要一个实体。

[OperationContract, FaultContract(typeof(ServiceError))]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped]
Entity SaveEntity(Entity entity);

我有一个带有这些对象的xml序列化表示的文件。我需要能够直接将此xml发布到服务而不在客户端进行反序列化(架构问题,我们没有引用)。

是否可以通过使用HttpClient构建请求来实现此目的?

    var client = new HttpClient(HttpClient:);
    client.PostAsync("http://localhost:55217/MyService.svc/SaveEntity", new HttpContent
    {
      Headers = new System.Net.Http.Headers.HttpContentHeaders
      {
         // can I put my serialised xml here?
      }
    }

这是配置:

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="Default" name="MyService">
        <endpoint address="" binding="webHttpBinding" contract="IMyService" behaviorConfiguration="webBehavior" bindingConfiguration="fullMessageSize" />
        <endpoint address="ws" binding="wsHttpBinding" contract="IMyService" bindingConfiguration="fullMessageSize" />
      </service>      
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="fullMessageSize" maxReceivedMessageSize="104857600" />
      </webHttpBinding>
      <wsHttpBinding>
        <binding name="fullMessageSize" maxReceivedMessageSize="104857600" />
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>

1 个答案:

答案 0 :(得分:3)

为什么不创建中间服务?

[OperationContract, FaultContract(typeof(ServiceError))]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped]
Entity SaveEntity(XElement entitySerialized)
{
    var entity = Deserialize(entitySerialized);
    var realService = new MyServiceClient();
    return realService.SaveEntity(entity);
}

中间服务可以具有序列化所需的引用。

您可以考虑创建服务到服务端点,以便在中间服务和实际服务之间使用。该端点可以使用诸如netTcpBinding之类的速度更快的绑定之一,您可能不希望与您的客户端一起使用。