2

我有一个正在调用的 WebHttpBinding WCF 服务。我的第一个 POST 方法正确发送对象,但随后对 POST 方法的调用为对象传递了 null。

这是我的服务:

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 服务,并且 insert 方法工作得非常好,我可以在我的数据库中看到持久化的对象。但是在更新时,ObjectDTO 参数为空。如果我加载现有对象并执行更新,它会完美运行。使用 POST 方法对 WCF 服务的后续调用似乎是一个问题。GET 方法没有这个问题。

4

2 回答 2

2

我已经解决了这个问题,但是 WCF SOAP。在客户端站点上,当调试点投入使用时,您可以看到填充了值的对象,它为空。这是由于参数的大小写发生变化而发生的。. 如果您看到您的服务合同有

ObjectDTO 更新(ObjectDTO objectDto);

并且定义有 public void Update(ObjectDTO objectDTO ) ,注意大小写的区别。

于 2013-02-02T06:53:02.400 回答
1

您尚未显示相关代码以 100% 确定问题所在。但看起来它是空的,因为这一行:

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

正在返回 null。所以你需要检查插入方法。

于 2010-02-13T14:02:28.937 回答