日期:2014-05-17 浏览次数:20458 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace MagicEightBallServiceLib
{
[ServiceContract]
public interface IEightBall
{
[OperationContract]
string ObtainAnswer(string userQuestion);
}
public class MagicEightBallService:IEightBall
{
#region IEightBall 成员
public MagicEightBallService()
{
Console.WriteLine("The magic 8 ball awaits for your answer");
}
public string ObtainAnswer(string userQuestion)
{
string[] answer = { "Future uncertain", "yes", "no", "Hazy" };
Random r = new Random();
return String.Format("{0}?{1}", userQuestion, answer[r.Next(answer.Length)]);
}
#endregion
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MagicEightBallServiceLib.MagicEightBallService">
<endpoint address="http://localhost:8080/MagicEightBallService"
binding="basicHttpBinding" contract="MagicEightBallServiceLib.IEightBall"/>
</service>
</services>
</system.serviceModel>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using MagicEightBallServiceLib;
namespace MagicEightBallServiceHost
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Console basic WCF host");
using (ServiceHost serviceHost=new ServiceHost("MagicEightBallService"))
{
&nbs