本文整理匯總了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();
}
}
}