WCF无法将dataContracts分离/封装到自己的cs文件中 - Stack Overflow Exception

时间:2015-10-15 13:31:55

标签: c# asp.net web-services wcf

我有一个有趣的问题。每当我尝试将数据契约分离到他们自己的cs文件中时...当在服务方法中使用这些数据协定之一时,由于堆栈溢出异常,我终止进程。似乎我需要将数据协定放在接口类中以解决问题,但是,我希望在我的wcf服务应用程序中的DTO目录下为每个数据协定提供单独的cs文件。代码没问题,只有当我尝试将数据合同移动到他们自己的类中时才会发生错误。对于为什么会发生这种情况,一些见解会很好。 wcf测试客户端在使用方法时会生成正确的响应,但是当连接到我的控制台应用程序时。只有接口cs文件(Iservice.cs)中的数据协定才能使用方法,但是,自己的cs文件中的数据协定会抛出异常。

数据合同在其自己的cs文件中。

using developer1.Core.Dto.Account;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace developer1.Core.Dto.Account
{
    [DataContract]
    public class AccountDto
    {
        Guid accountId = Guid.NewGuid();
        Guid userId = Guid.NewGuid();
        string accountName = "default";
        string bankName = "default";


        [DataMember]
        public Guid AccountId
        {
            get { return accountId; }
            set { accountId = value; }
        }


        [DataMember]
        public Guid UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        [DataMember]
        public string AccountName
        {
            get { return accountName; }
            set { accountName = value; }
        }

        [DataMember]
        public string BankName
        {
            get { return bankName; }
            set { bankName = value; }
        }



    }
}

使用对象的方法。

public AccountDto createAccount(string accountName, string bankName)
{
    AccountDto newAccount = new AccountDto() { AccountName = accountName, BankName = bankName };
    return newAccount;
}

Iservice class

using developer1.Core.Dto.Account;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
//using AccountDto = developer1.Core.Dto.Account.AccountDto;

namespace developer1.Core.ServiceContracts
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        CompositeType createComposite(string value);

        [OperationContract]
        List<AccountDto> GenerateMultipleAccounts(int count);
        // TODO: Add your service operations here
        [OperationContract]
        AccountDto createAccount(string accountName, string bankName);

        [OperationContract]
        AccountDto2 createAccount2(string stringValue, bool boolValue);
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    public class AccountDto2
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }


    }


}

接口类中的任何数据协定都将使方法正常工作。但是他们自己的cs文件中的任何契约都会破坏带有堆栈溢出异常的方法

0 个答案:

没有答案
相关问题