开发者

WCF Service Library

开发者 https://www.devze.com 2023-04-06 18:41 出处:网络
I am new to WCF services. I was asked to manually create a WCF service. I did the following: Created a new project Console App.

I am new to WCF services. I was asked to manually create a WCF service. I did the following:

  1. Created a new project Console App.
  2. Created a class called Evaluation
  3. Created an interface called IEvaluatorService
  4. Created a class EvaluationService implementing the interface IEvaluato开发者_运维技巧rService

I need to use the following address: http://localhost:8000/Evaluations then test my service via WcfTestClient. I am not sure what to do next. Code below.

Thanks in advance for any help!

namespace Evaluations
{
    [ServiceContract]
    interface IEvaluatorService
    {
        [OperationContract(Name="AddEvaluation")]
        int Add(string user, string content);

        [OperationContract(Name="RemoveEvaluation")]
        void Remove([MessageParameter(Name="existingID")] int id);

        [OperationContract(Name="GetAllEvaluations")]
        Evaluation[] GetAll();

        [OperationContract(Name="GetEvaluation")]
        Evaluation Get(int id);

        [OperationContract(Name="GetAllEvaluationsFrom")]
        Evaluation[] GetAll([MessageParameter(Name = "username")] string submitter);
    }
}

namespace Evaluations
{
    class EvaluationService : IEvaluatorService
    {
        List<Evaluation> myList = new List<Evaluation>();
        static int count = 0;

        public int Add(string user, string content)
        {
            Evaluation eval = new Evaluation()
            {
                UniqueID = count++, 
                Submitter = user,
                SubmissionTime = DateTime.Now,
                Text = content
            };
            myList.Add(eval);
            return eval.UniqueID;
        }

        public void Remove(int id)
        {
            myList.RemoveAt(id);
        }

        public Evaluation[] GetAll()
        {
            return myList.ToArray<Evaluation>();
        }

        public Evaluation Get(int id)
        {
            throw new NotImplementedException();
        }

        public Evaluation[] GetAll(string submitter)
        {
            throw new NotImplementedException();
        }
    }
}

namespace Evaluations
{
    [DataContract]
    class Evaluation
    {
        [DataMember]
        public string Submitter { get; set; }
        [DataMember]
        public int UniqueID { get; set; }
        [DataMember]
        public DateTime SubmissionTime { get; set; }
        [DataMember]
        public string Text { get; set; }
    }
}


The easiest thing to do is...

  • go into Visual Studio
  • right click on your project
  • select Add New
  • choose WCF Service

See what code Visual Studio added and follow that pattern for your service.

0

精彩评论

暂无评论...
验证码 换一张
取 消