2010-02-12 143 views
2

我有我打電話的WebHttpBinding WCF服務。我的第一個POST方法正確地發送對象,但後續對POST方法的調用則爲該對象傳遞null。WCF呼叫傳遞一個空參數

這裏是我的服務:

public void Update(ObjectDTO objectDTO) 
{ 
    string token = WebOperationContext.Current != null ? WebOperationContext.Current.IncomingRequest.Headers["token"] : string.Empty; 

    //Authentication 
    bool isUserAuthenticatedResult = IsUserAuthenticated(ref token); 
    if (!isUserAuthenticatedResult) 
     return null; 

    //Perform service action 
    MyDtoManager = new MyDtoManager(); 
    objectDTO = MyDtoManager.Update(objectDTO); 
    return objectDTO; 
} 

這裏是我的服務合同:

[ServiceContract] 
public interface IMyDtoService 
{ 
    [OperationContract] 
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] 
    List<ObjectDTO> LoadById(string value); 

    [OperationContract] 
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] 
    List<ObjectDTO> Load(string field, string value); 

    [OperationContract] 
    [WebInvoke(Method="GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] 
    List<ObjectDTO> LoadAll(); 

    [OperationContract(Name = "InsertSingle")] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] 
    ObjectDTO Insert(ObjectDTO objectDto); 

    [OperationContract(Name = "UpdateSingle")] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] 
    ObjectDTO Update(ObjectDTO objectDto); 

    [OperationContract(Name = "DeleteSingle")] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] 
    ObjectDTO Delete(ObjectDTO objectDto); 
} 

這裏是我的服務器配置:

<system.serviceModel> 
    <bindings> 
    <webHttpBinding> 
     <binding name="WebHttpBindingConfig" 
      openTimeout="00:05:00" 
      sendTimeout="00:05:00" 
      maxBufferSize="65536000" 
      maxBufferPoolSize="52428800" 
      maxReceivedMessageSize="65536000" 
      transferMode="Buffered"> 
     <readerQuotas maxDepth="32" 
        maxStringContentLength="65536000" 
        maxArrayLength="16384" 
        maxBytesPerRead="4096" 
        maxNameTableCharCount="16384" /> 
     <security> 
     <transport /> 
     </security> 
    </binding> 
    </webHttpBinding> 
</bindings> 
<services> 
    <service behaviorConfiguration="Services.ServiceBehavior" 
     name="Services.MyDtoService"> 
    <endpoint address="" 
      behaviorConfiguration="HttpBehavior" 
      binding="webHttpBinding" 
      name="Services.MyDtoService" 
      contract="ServiceInterfaces.IMyDtoService"> 
    <identity> 
     <dns value="localhost" /> 
    </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
</service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="Services.ServiceBehavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="HttpBehavior"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
</system.serviceModel> 

最後我的客戶端代碼中進行調用:

IMyDtoService myDtoService = new WebChannelFactory<IMyDtoService>(BindingConfig, new Uri("http://localhost:8080/MyDtoService.svc")).CreateChannel(); 
using (new OperationContextScope((IClientChannel)myDtoService)) 
{ 
    if (WebOperationContext.Current != null) 
     WebOperationContext.Current.OutgoingRequest.Headers.Add("token", tokenResult.Result); 

    ObjectDTO insertResult = ipAddressService.Insert(new ObjectDTO 
               { ObjectGuid = Guid.NewGuid(), 
               IsAllow = true, 
               Identifier = 1, 
               IdentifierType = 0, 
               StartIpAddress = "192.168.0.1" 
               }); 
    List<ObjectDTO> loadByIdResult1 = myDtoService.LoadById(insertResult.ObjectGuid.ToString()); 
    Console.WriteLine("Insert Found: " + loadByIdResult1.Count); 

    insertResult.IsAllow = false; 
    ObjectDTO updateResult = ipAddressService.Update(insertResult); 
} 

正如你可以看到我的客戶端代碼調用我的WCF服務和插入方法工作得很好,我可以看到我的數據庫中的持久對象。但是,在更新中,ObjectDTO參數爲空。如果我加載一個現有的對象並執行更新,它可以很好地工作。這似乎是隨後使用POST方法調用WCF服務的問題。我沒有GET方法的這個問題。

+0

做一些測試,如果我將我的更新調用它自己: 使用(新OperationalContextScope((IClientChannel)myDtoService)) 然後我的兩個插入和更新方法的工作。如果我的Update方法仍然是我發佈的示例中的方式,那麼Update方法將爲ObjectDTO參數獲取一個空值。誰能解釋爲什麼? – Brandon 2010-02-12 20:06:20

回答

1

您沒有顯示相關的代碼是100%確定問題所在。但它看起來是空的,因爲這條線:

ObjectDTO insertResult = ipAddressService.Insert(new ObjectDTO 
               { ObjectGuid = Guid.NewGuid(), 
               IsAllow = true, 
               Identifier = 1, 
               IdentifierType = 0, 
               StartIpAddress = "192.168.0.1" 
               }); 

返回null。所以你需要檢查插入方法。

+0

我的insertResult對象在調用後被填充。我相信這與OperationalContextScope有關。如果我將Insert和Update方法調用分離到它們自己的OperationalContextScopes中,則兩種方法都會得到正確的結果並傳遞正確的參數。 – Brandon 2010-02-14 13:55:20

1

我已經經歷了這個問題,但WCF的SOAP。在客戶端站點上,當調試點服務時,它可以看到填充了值的對象,它爲空。它已經發生,由於param的變化。。如果你看到你的服務合同有

ObjectDTO Update(ObjectDTO objectDto);

and defination has public void Update(ObjectDTO objectDTO),請注意套管的區別。