C# WCF接收的复杂对象为null

C# WCF接收的复杂对象为null,c#,wcf,object,C#,Wcf,Object,在论坛[StackOverFlow][1]上获得另一个线程的输入后 [1] :我是否得出结论,我的问题在于WCF服务本身。服务似乎无法理解它正在接收的对象 无论我做什么,传递的对象总是空的 我甚至尝试过声明datamember显式,即使在当前版本的WCF中不需要它 以下是我试图通过WCF的课程: namespace ModelLayer { [DataContract] public class User { [Da

在论坛[StackOverFlow][1]上获得另一个线程的输入后

[1] :我是否得出结论,我的问题在于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);


    }

}
[ServiceContract(命名空间=”http://nordahl.org/")]
公共接口IWCFoodandAllergi
{
[经营合同]
int插入器(用户_用户);
[经营合同]
用户GetUser(字符串_mac);
[经营合同]
int InsertRecipe(Recipe\u Recipe);
[经营合同]
列出GetRecipeByAllergi(列出过敏反应);
[经营合同]
列表GetRecipeByType(字符串类型);
[经营合同]
列出GetRecipeByName(字符串_名称);
}
}
最后是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>

当一个复杂对象引用T4生成的类中的另一个复杂对象时,我遇到了与您类似的问题。如果正在使用获取方法中的包含,或与实体或对象相关的其他方法将复杂对象相互链接;您可能会得到一个循环引用错误。我有一个朋友向我展示了如何使用设置属性的自定义类修复此问题: