本文整理汇总了C#中System.ServiceModel.Web.WebServiceHost类的典型用法代码示例。如果您正苦于以下问题:C# WebServiceHost类的具体用法?C# WebServiceHost怎么用?C# WebServiceHost使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebServiceHost类属于System.ServiceModel.Web命名空间,在下文中一共展示了WebServiceHost类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Add
[ServiceContract]
public interface ICalculator
{
[OperationContract]
[WebInvoke(UriTemplate = "add?x={x}&y={y}")]
long Add(long x, long y);
[OperationContract]
[WebInvoke(UriTemplate = "sub?x={x}&y={y}")]
long Subtract(long x, long y);
[OperationContract]
[WebInvoke(UriTemplate = "mult?x={x}&y={y}")]
long Multiply(long x, long y);
[OperationContract]
[WebInvoke(UriTemplate = "div?x={x}&y={y}")]
long Divide(long x, long y);
[OperationContract]
[WebGet(UriTemplate = "hello?name={name}")]
string SayHello(string name);
}
public class CalcService : ICalculator
{
public long Add(long x, long y)
{
return x + y;
}
public long Subtract(long x, long y)
{
return x - y;
}
public long Multiply(long x, long y)
{
return x * y;
}
public long Divide(long x, long y)
{
return x / y;
}
public string SayHello(string name)
{
return "Hello " + name;
}
}
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/");
WebServiceHost svcHost = new WebServiceHost(typeof(CalcService), baseAddress);
try
{
svcHost.Open();
Console.WriteLine("Service is running");
Console.WriteLine("Press enter to quit...");
Console.ReadLine();
svcHost.Close();
}
catch (CommunicationException cex)
{
Console.WriteLine("An exception occurred: {0}", cex.Message);
svcHost.Abort();
}
}
}