2010-02-12 4 views
2

호출 할 WebHttpBinding WCF 서비스가 있습니다. 내 첫 번째 POST 메서드는 개체를 올바르게 보내지 만 POST 메서드에 대한 후속 호출은 개체에 대해 null을 전달합니다.WCF 호출 Null 매개 변수 전달

<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> 

그리고 마지막으로 내 클라이언트 코드가 전화를 걸 : 여기

[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); 
} 

내 서버 구성입니다 : 여기
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; 
} 

내 서비스 계약입니다 : 여기

내 서비스 :

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 매개 변수는 null입니다. 기존 객체를로드하고 업데이트를 수행하면 완벽하게 작동합니다. POST 메서드를 사용하여 WCF 서비스를 이후에 호출 할 때 문제가되는 것으로 보입니다. GET 메서드에이 문제가 없습니다.

+0

, 몇 가지 테스트를 수행. 내 Update 메서드가 게시 된 예제에 그대로있는 경우 Update 메서드는 ObjectDTO 매개 변수에 null 값을 가져옵니다. 왜 그 이유를 설명 할 수 있습니까? – Brandon

답변

1

관련 코드를 100 % 확실하게 표시하지 않았습니다.

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

가 null 반환 :이 줄 때문에이 null처럼하지만 보인다. 삽입 방법을 확인해야합니다.

+0

내 insertResult 개체는 호출 후에 채워집니다. OperationalContextScope와 관련이 있다고 생각합니다. 삽입 및 업데이트 메서드 호출을 고유 한 OperationalContextScopes로 분리하면 두 메서드 모두 올바른 결과를 얻고 올바른 매개 변수를 전달합니다. – Brandon

1

이 문제는 해결했지만 WCF SOAP를 사용했습니다. 클라이언트 사이트에서 디버그 지점 서비스가 null 일 때 값으로 채워지는 객체를 볼 수 있습니다. 그것은 parham의 케이스에 변경으로 인해 발생했습니다.. 서비스 계약이있는 경우

ObjectDTO 업데이트 (ObjectDTO objectDto);

defination에 공개 무효화가 있습니다. 업데이트 (ObjectDTO objectDTO), 대소 문자 차이점에 유의하십시오. 다음 (새 OperationalContextScope ((IClientChannel) myDtoService))를 사용하여 내 삽입 및 업데이트 방법이 모두 작동 : 나는 그것을 자신의 내 업데이트 호출을 이동하는 경우