WCF将复杂对象作为null接收

时间:2014-05-16 16:23:29

标签: c# wcf object

在论坛[StackOverFlow] [1]

上输入另一个帖子后

[1]:Getting a nullreference when passing a complex object from KSOAP2 to WCF我得出的结论是我的问题在于WCF服务本身。似乎该服务无法理解它正在接收的对象。

无论我做什么,传递的对象总是为空。

我甚至尝试将datamember声明为显式,即使它在当前版本的WCF中不是必需的。

这是我试图通过WCF的课程:

namespace ModelLayer
{

        [DataContract]
        public class User   
        {
            [DataMember]
            public int Id { get; set; }
            [DataMember]
            public string UserName { get; set; }
            [DataMember]
            public string Mac { get; set; }

            public User()
            {

            }
        }
    }

我的服务界面:

[ServiceContract (Namespace = "http://nordahl.org/")]
    public interface IWcfFoodAndAllergi
    {

        [OperationContract]
        int InsertUser(User _user);

        [OperationContract]
        User GetUser(string _mac);

        [OperationContract]
        int InsertRecipe(Recipe _recipe);

        [OperationContract]
        List<Recipe> GetRecipeByAllergi(List<Allergies> _allergies);

        [OperationContract]
        List<Recipe> GetRecipeByType(string _type);

        [OperationContract]
        List<Recipe> GetRecipeByName(string _name);


    }

}

最后是webconfig文件本身:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="1048576"/>
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
        <fileExtensions allowUnlisted="true">
          <remove fileExtension=".cs"/>
          <add fileExtension=".cs" allowed="true"/>

        </fileExtensions>
      </requestFiltering>
    </security>
  </system.webServer>
  <system.serviceModel>

          <services>
      <service name="WcfFoodAndAllergi.WcfFoodAndAllergi">
        <!-- Use a bindingNamespace to eliminate tempuri.org -->
        <endpoint address=""
                  binding ="basicHttpBinding" 
                  bindingNamespace="http://nordahl.org/"
                  contract="WcfFoodAndAllergi.IWcfFoodAndAllergi"
        />
      </service>
    </services>


    <behaviors>
      <serviceBehaviors>
        <behavior name ="">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

1 个答案:

答案 0 :(得分:0)

当复杂对象引用T4生成的类中的另一个复杂对象时,我遇到了类似的问题。如果您在获取方法中使用包含,或者在实体或与复杂对象链接的对象之间使用其他方法;您可能会收到循环引用错误。我有一个朋友告诉我如何通过设置属性的自定义类来解决这个问题:

Complex Object won't return when using 'include' syntax in WCF with Entity Version 6