C# 如何从WCF服务访问自定义类对象并更改其数据?

C# 如何从WCF服务访问自定义类对象并更改其数据?,c#,wcf,C#,Wcf,制作一个小型的WCF测试程序,该程序基于一个有客户的商店,每个客户都有一个平衡的特殊积分。现在我想把重点放在拥有固定数量的客户上,比如说3个。问题是我不知道该在哪里创建这些客户,以及如何从服务功能访问这些客户。我在考虑在服务的main()中创建一个客户阵列,以便以后客户可以使用它们,但这是个好主意吗?非常熟悉WCF,找不到任何示例来说明如何实现类似的功能 using System; using System.Collections.Generic; using System.Linq; usin

制作一个小型的
WCF
测试程序,该程序基于一个有客户的商店,每个客户都有一个平衡的特殊积分。现在我想把重点放在拥有固定数量的客户上,比如说3个。问题是我不知道该在哪里创建这些客户,以及如何从服务功能访问这些客户。我在考虑在服务的
main()
中创建一个客户阵列,以便以后客户可以使用它们,但这是个好主意吗?非常熟悉
WCF
,找不到任何示例来说明如何实现类似的功能

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

namespace StoreService
{

    public class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:6112/StoreService");
            ServiceHost host = new ServiceHost(typeof(Store), baseAddress);
            try
            {
                host.AddServiceEndpoint(typeof(IStore), new WSHttpBinding(), "Store");
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();
                host.Close();

            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occured: {0}", ce.Message);
                host.Abort();
            }
        }
    }

    [ServiceContract(Namespace="http://StoreService")]
    public interface IStore
    {
        [OperationContract]
        void AddPoints(string accNum, double points);

        [OperationContract]
        void SubtractPoints(string accNum, double points);

        [OperationContract]
        int CustomerPoints(string accNum);
    }

    [DataContract]
    public class Customer
    {
        private string accountNumber;
        private string name;
        private double points;

        [DataMember]
        public int AccountNumber
        {
            get { return accountNumber; }
            set { accountNumber = value; }
        }

        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [DataMember]
        public int Points
        {
            get { return points; }
            set { points = value; }
        }
    }


    public class Store : IStore
    {
        public void AddPoints(string accNum, double points)
        {
            //Add points logic
        }

        public void SubtractPoints(string accNum, double points);
        {
            //Substract points logic
        }

        public int CustomerPoints(string accNum)
        {
            //Show points
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.ServiceModel;
使用System.Runtime.Serialization;
使用System.ServiceModel.Description;
名称空间存储服务
{
公共课程
{
静态void Main(字符串[]参数)
{
Uri baseAddress=新Uri(“http://localhost:6112/StoreService");
ServiceHost主机=新的ServiceHost(类型(存储),基地址);
尝试
{
AddServiceEndpoint(typeof(IStore),新的WSHttpBinding(),“Store”);
ServiceMetadataBehavior smb=新ServiceMetadataBehavior();
smb.HttpGetEnabled=true;
host.Description.Behaviors.Add(smb);
WriteLine(“服务准备就绪”);
Console.WriteLine(“按以终止服务”);
Console.WriteLine();
Console.ReadLine();
host.Close();
}
捕获(通信异常ce)
{
WriteLine(“发生异常:{0}”,ce.Message);
host.Abort();
}
}
}
[ServiceContract(命名空间=”http://StoreService")]
公共接口晶体管
{
[经营合同]
void AddPoints(字符串accNum,双点);
[经营合同]
空减去点(字符串accNum,双点);
[经营合同]
int CustomerPoints(字符串accNum);
}
[数据合同]
公共类客户
{
私有字符串accountNumber;
私有字符串名称;
私人双积分;
[数据成员]
公共国际帐户号码
{
获取{return accountNumber;}
设置{accountNumber=value;}
}
[数据成员]
公共字符串名
{
获取{返回名称;}
设置{name=value;}
}
[数据成员]
公共整数点
{
获取{返回点;}
设置{points=value;}
}
}
公共类存储:IStore
{
公共void AddPoints(字符串accNum,双点)
{
//添加点逻辑
}
公共点(字符串accNum,双点);
{
//减分逻辑
}
public int CustomerPoints(字符串accNum)
{
//显示点
}
}
}
首先,从MS获得一系列关于WCF的好示例,让您开始学习。对于这个演示软件,我将考虑创建一种方法,将所有“测试”数据生成到某种集合中,并使用它在您的服务中进行处理。一旦您熟悉了这些概念,请考虑使用数据库作为数据存储。还要寻找初始化WCF服务的方法


同样,您也需要一个服务接口和服务公开的每个方法的代码

这是我脑子里想不出来的,未经测试,微软的例子将向你展示Bryan发布的这些东西

例如,添加2个新类:

// First the Interface, things in here decorated with the Operation contract gets exposed, things with serializable are available to the client

namespace MyService
{
    [ServiceContract]
    public interface ICustomer
    {

        [OperationContract]
        int GetBalance(Guid CustomerId);
    }

    [Serializable]
    public class Customer
    {
        public Guid Id;
        public int Balance;
    }
}




// 2nd class file is the code mapping to the interface
namespace MyService
{
    private List<Customer> Customers = new List<Customer>();
    public class MyService : ICustomer
    {
        public int GetBalance(Guid CustomerId)
        {
            foreach(Customer c in Customers)
            {
                 if(c.Id == CustomerId)
                 {
                     return c.Balance;
                 }
            }
        }
    }
}
//首先是接口,这里用操作契约修饰的东西被暴露出来,客户机可以使用可序列化的东西
命名空间MyService
{
[服务合同]
公共接口ICustomer
{
[经营合同]
int GetBalance(Guid CustomerId);
}
[可序列化]
公共类客户
{
公共Guid Id;
公共利益平衡;
}
}
//第二类文件是映射到接口的代码
命名空间MyService
{
私有列表客户=新列表();
公共类MyService:ICustomer
{
public int GetBalance(Guid CustomerId)
{
foreach(客户中的客户c)
{
如果(c.Id==客户Id)
{
返回c.余额;
}
}
}
}
}

Hmm.看起来很有趣。我必须为客户的id使用Guid吗?我计划按照我的源代码使用字符串。嗨,鲍勃,一点也不,你可以对你的客户id使用你喜欢的任何东西。这只是一个例子,这样你可以更深入地了解这些类。