【问题标题】:WCF Relocation of DataContractsDataContracts 的 WCF 重定位
【发布时间】:2009-09-16 03:40:57
【问题描述】:

这是一个功能齐全的 WCF Hello World 程序。 IE。我可以毫无异常地运行这个程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace DataContractsNamespace
{
    [DataContract]
    public class AccountInfo
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Clients
{
    public class BankProxy : ServiceContractsNamespace.IBank
    {
        ServiceContractsNamespace.IBank channel;

        public BankProxy()
        {
            channel = ChannelFactory<ServiceContractsNamespace.IBank>.CreateChannel(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/Services/BankService"));
        }

        public decimal GetAcccountBalance(string AcctNo)
        {
           return channel.GetAcccountBalance(AcctNo);
        }

        public DataContractsNamespace.AccountInfo GetAccountInfo(string AcctNo)
        {
             return channel.GetAccountInfo(AcctNo);
        }
    }
}

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;

namespace ServiceContractsNamespace
{
    [ServiceContract]
    public interface IBank
    {
        [OperationContract]
        decimal GetAcccountBalance(string AcctNo);

        [OperationContract]
        DataContractsNamespace.AccountInfo GetAccountInfo(string AcctNo);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clients
{
    class Program
    {
        static void Main(string[] args)
        {
            BankProxy prox = new BankProxy();
            Console.WriteLine("Hit enter to invoke the service call. Type exit then enter to close");

            while (Console.ReadLine() != "exit")
            {
                string balance = prox.GetAcccountBalance("1234").ToString("c");
                DataContractsNamespace.AccountInfo ai = prox.GetAccountInfo("1234");
                Console.WriteLine("{0} {1} your account balance is {2}.", ai.FirstName, ai.LastName, balance);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hosts
{
    public class BankService : ServiceContractsNamespace.IBank
    {
        public decimal GetAcccountBalance(string AcctNo)
        {
            return 1.37m;
        }

        public DataContractsNamespace.AccountInfo GetAccountInfo(string AcctNo)
        {
            DataContractsNamespace.AccountInfo ai = new DataContractsNamespace.AccountInfo();
            ai.FirstName = "Paul";
            ai.LastName = "Johansen";
            return ai;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Hosts
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost servHo = new ServiceHost(typeof(BankService), new Uri("http://localhost:8000/Services"));
            servHo.AddServiceEndpoint(typeof(ServiceContractsNamespace.IBank), new BasicHttpBinding(), "BankService");

            servHo.Open();
            Console.WriteLine("This service is open for business. Hit Enter to close.");
            Console.ReadLine();
            servHo.Close();
        }
    }
}

如您所见,AccountInfo - 数据合约由客户端和主机共享。

我只需要与主机/服务端保持数据合同。

客户端应该只能看到 DataContracts 的接口(例如 IAccountInfo)。

我应该如何修改我的程序以引入IAccountInfo

【问题讨论】:

  • 您似乎试图将 .net 远程类型设计硬塞到 WCF 中,WCF 喜欢 DTO。

标签: wcf datacontract


【解决方案1】:

听起来你想返回一个接口而不是一个类。我不完全确定您为什么不满足于返回 AccountInfo。但是,您应该能够执行此操作,但您需要使用 KnownType 或 ServiceKnownType 才能使其工作。

或者,如果您在完全 .NET 环境中工作,您可以使用 NetDataContractSerializer 而不是 DataContractSerializer。

您可以查看参考和示例:

【讨论】:

    【解决方案2】:

    如果您不想共享 AccountInfo,IBank.GetAccountInfo 应该返回给客户什么?创建 2 个类,使第一个数据合同成为第二个,而不是在您要共享的地方使用第一个,如果不是,则使用第二个

    【讨论】:

    • IBank.GetAccountInfo 将返回 IAccountInfo。
    • 还有什么?应该是客户端中实现 IAccountInfo 的类?你为什么不想创建 2 个类?
    猜你喜欢
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    相关资源
    最近更新 更多