2014-06-06 71 views
1

我有一個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> 
+0

您的WCF服務是否公開RESTful端點? –

+0

@YuvalItzchakov擴展到包含端點配置 – Liath

回答

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,您可能不想使用您的客戶端。