Program.cs
class Program
{
static void Main(string[] args)
{
try
{
Console.Title = "同時(shí)支持http和Webservice交互的服務(wù)";
//可以突出重點(diǎn)輸出內(nèi)容
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("WCF服務(wù)正在初始化.....");
Console.ForegroundColor = ConsoleColor.Gray;
#region 支持Post和Get
Console.WriteLine("======================================");
String HttpUrl = "http://127.0.0.1:8880";
Uri address = new Uri(HttpUrl);
WebServiceHost http_host = new WebServiceHost(typeof(HttpInterface), address); //綁定處理的類
http_host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
http_host.Open();
Console.WriteLine("Http請(qǐng)求URL--->" + HttpUrl);
#endregion
#region 支持Webservice
String WsUrl = "http://127.0.0.1:8881/wstest";
address = new Uri(WsUrl);
ServiceHost webservice_host = new ServiceHost(typeof(WSInterface), address); //綁定處理的類
webservice_host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
//=======設(shè)置最大連接數(shù)
ServiceThrottlingBehavior behavior = new ServiceThrottlingBehavior();
behavior.MaxConcurrentCalls = 2147483647;
behavior.MaxConcurrentInstances = 2147483647;
behavior.MaxConcurrentSessions = 2147483647;
webservice_host.Description.Behaviors.Add(behavior);
Console.WriteLine("Webservice請(qǐng)求URL--->" + WsUrl);
webservice_host.Open();
#endregion
Console.WriteLine("WCF服務(wù)啟動(dòng)成功!");
while (true)
{
Console.ReadKey(false);
}
}
catch (Exception qq)
{
Console.WriteLine(qq.Message);
Console.WriteLine("WCF服務(wù)啟動(dòng)失敗!");
Console.Read();
}
}
}
HttpInterface.cs
/// <summary>
/// http://127.0.0.1:8880
/// </summary>
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract]
public class HttpInterface
{
/// <summary>
/// 測(cè)試接口
/// 調(diào)用示例 http://127.0.0.1:8880/wcfapp/TestHttp?test1=測(cè)試
/// </summary>
/// <param name="idCardNo"></param>
/// <returns></returns>
[OperationContract]
[WebInvoke(Method = "*",
UriTemplate = "wcfapp/testhttp?test1={test1}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
public string TestHttp (string test1)
{
return string.Format("HTTP返回測(cè)試內(nèi)容 = {0}",test1);
}
}
WSInterface.cs
/// <summary>
/// http://127.0.0.1:8881/wstest?wsdl
/// </summary>
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract]
public class WSInterface
{
[OperationContract]
public string TestWS(string test1)
{
return string.Format("Webservice返回測(cè)試內(nèi)容 = {0}", test1);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者