WCF服务

WCF服务

1.创建一个接口,并给予ServiceContract协议

1
2
3
4
5
6
[SerciveContract]
public interface IWCFService
{
[OperationContract]
bool DoLogin(string username, string psswd);
}

2.创建一个继承IWCFService的类WCFService

1
2
3
4
5
6
7
8
9
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
public class WCFService :IWCFService
{

public bool Dologin(string username, string psswd)
{
//实现登录逻辑
}
}

3.定义宿主寄宿WCF服务

寄宿方式分为4类:
1.”Self-Hosting” in a Managed Application(自托管宿主)
2.Managed Windows Services(Windows Services宿主)
3.Internet Information Services(IIS宿主)
4.Windows Process Activation Service(WAS宿主)

此例以第一种介绍:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string uri = "http://localhost:39005/WCFService";
\\定义ServiceHost
ServiceHost host = new ServiceHost(typeof(WCFService),new Uri(uri));

\\绑定参数
BasicHttpBinding _basic = new BasicHttpBinding();
_basic.MaxBufferPoolSize = int.MaxValue;
_basic.MaxBufferSize = int.MaxValue;
_basic.MaxReceivedMessageSize = int.MaxValue;

\\行为参数
System.Xml.XmlDictionaryReaderQuotas _readerQuotas = new XmlDictionaryReaderQuotas();
_readerQuotas.MaxArrayLength = int.MaxValue;
_readerQuotas.MaxBytesPerRead = int.MaxValue;
_readerQuotas.MaxDepth = int.MaxValue;
_readerQuotas.MaxNameTableCharCount = int.MaxValue;
_readerQuotas.MaxStringContentLength = int.MaxValue;
_basic.ReaderQuotas = _readerQuotas;

\\开启服务
host.AddServiceEndpoint(typeof(WCFService), _basic, uri);

通常的Windows App都是采用第二种寄主方式,可以随时开启和停止WCF服务

Donate comment here
-------------本文结束感谢您的阅读-------------
Fork me on GitHub