开发者

WCF Call Passing a Null Parameter

开发者 https://www.devze.com 2022-12-19 20:49 出处:网络
I have a WebHttpBinding WCF service that I am calling. My first POST method send the object correctly, but subsequent calls to POST methods are passing null for the object.

I have a WebHttpBinding WCF service that I am calling. My first POST method send the object correctly, but subsequent calls to POST methods are passing null for the object.

Here is my service:

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

Here is my Service Contract:

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

Here is my server configuration:

<system.serviceModel>
  <bindings>
    <webHttpBinding>
      <binding name="WebHttpBindingConfig" 
           openTimeout="00:05:00" 
           sendTimeout="00:05:00"
           maxBufferSize="65536000" 
           maxBufferPoolSize="52428800" 
           maxReceivedMessag开发者_JAVA百科eSize="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>

And finally my client code making the call:

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

As you can see my client code calls my WCF service and the insert method works perfectly fine and I can see the persisted object in my database. However on the update, the ObjectDTO parameter is null. If I load an existing object and perform an update it works perfectly. It appears to be an issue with subsequent calls to the WCF service using POST methods. I do not have this problem with GET methods.


I have gone through this problem but WCF SOAP. On client site you can see object filled with values when debug point comes in service it is null. It had happened due to change in casing of param. . If you see your service contract has

ObjectDTO Update(ObjectDTO objectDto);

and defination has public void Update(ObjectDTO objectDTO) , note the difference in casing.


You have not shown the relevant code to be 100% sure what the problem is. But it looks like it is null because this line:

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

is returning null. So you need to check the insert method.

0

精彩评论

暂无评论...
验证码 换一张
取 消