WCF中使用nettcp協(xié)議進(jìn)行通訊的方法
快速閱讀
如何在wcf中用net tcp協(xié)議進(jìn)行通訊,一個(gè)打開Wcf的公共類。比較好好,可以記下來。 配置文件中注意配置 Service,binding,behaviors. Service中配置endpoint 指明abc ,binding中配置tcp通訊的要關(guān)參數(shù),behaivor中配置http請求的 地址
1.建立服務(wù)服務(wù)端
還是用上次的代碼,提供一個(gè)user類,實(shí)現(xiàn)一個(gè)方法
[ServiceContract]
public interface IUser
{
[OperationContract]
string GetUserInfo();
}
[ServiceContract]
public interface IUser
{
[OperationContract]
string GetUserInfo();
}
2.ServiceHostManager公有類
通過公有類可以減少代碼編寫量,可以保存下來,以后用的時(shí)候 直接拿來用
public interface IServiceHostmanager : IDisposable
{
void Start();
void Stop();
}
public class ServiceHostManager<TService>:IServiceHostmanager
where TService:class
{
private ServiceHost host;
public void Dispose()
{
Stop();
}
public ServiceHostManager()
{
host=new ServiceHost(typeof(User));
host.Opened+= (sender, e) =>
{
Console.WriteLine("wcf服務(wù)已經(jīng)啟動監(jiān)聽{0}",host.Description.Endpoints[0].Address);
};
host.Closed+= (sender, e) =>
{
Console.WriteLine("wcf服務(wù)已經(jīng)啟動關(guān)閉{0}", host.Description.Endpoints[0].Address);
};
}
public void Start()
{
Console.WriteLine("正在啟動wcf服務(wù){(diào)0}",host.Description.Endpoints[0].Name);
host.Open();
}
public void Stop()
{
if (host != null && host.State == CommunicationState.Opened)
{
Console.WriteLine("正在關(guān)閉wcf服務(wù){(diào)0}", host.Description.Endpoints[0].Name);
host.Close();
}
}
public static Task StartNew(CancellationTokenSource conTokenSource)
{
var task = Task.Factory.StartNew(() =>
{
IServiceHostmanager shm = null;
try
{
shm = new ServiceHostManager<TService>();
shm.Start();
while (true)
{
if (conTokenSource.IsCancellationRequested && shm != null)
{
shm.Stop();
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (shm != null) shm.Stop();
}
},conTokenSource.Token);
return task;
}
}
3.配置的相關(guān)參數(shù)
配置文件中注意配置 Service,binding,behaviors. Service中配置endpoint 指明abc ,binding中配置tcp通訊的要關(guān)參數(shù),behaivor中配置http請求的 地址
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="hcbServiceB.User" behaviorConfiguration="userBehavior"> <endpoint address="net.tcp://localhost:12345/User" binding="netTcpBinding" contract="hcbServiceB.IUser"> <identity> <dns value="localhost"/> </identity> </endpoint> </service> </services> <bindings> <netTcpBinding> <binding name="netTcpBindingConfig" closeTimeout="00:30:00" openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="100" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="100" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647 " maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="false" /> <security mode="Transport"> <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> </security> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="userBehavior"> <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8081/User" /> <serviceDebug includeExceptionDetailInFaults="True" /> <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
4.啟動服務(wù)
控制臺中啟動服務(wù)
static void Main(string[] args)
{
Console.WriteLine("初始化...");
Console.WriteLine("服務(wù)運(yùn)行期間,請不要關(guān)閉窗口。");
Console.Title = "wcf net tcp測試 ";
var cancelTokenSouce = new CancellationTokenSource();
ServiceHostManager<User>.StartNew(cancelTokenSouce);
while (true)
{
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
Console.WriteLine();
cancelTokenSouce.Cancel();
break;
}
}
}
5wcftesttoos軟件測試
軟件路徑位于,可以根據(jù)自己安裝vs的目錄去找。
D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE
測試
參考:
WCF綁定netTcpBinding寄宿到控制臺應(yīng)用程序:https://www.jb51.net/article/165257.htm
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對我們的支持。
上一篇:visual studio 2019正式版安裝簡單教程
欄 目:ASP.NET
下一篇:使用dotnet-dump 查找 .net core 3.0 占用CPU 100%的原因解析
本文標(biāo)題:WCF中使用nettcp協(xié)議進(jìn)行通訊的方法
本文地址:http://www.jygsgssxh.com/a1/ASP_NET/10910.html
您可能感興趣的文章
- 01-11淺析.Net Core中Json配置的自動更新
- 01-11ASP.NET Core靜態(tài)文件的使用方法
- 01-11.NET Core 中的并發(fā)編程
- 01-11VsCode之使用WebView通信詳解
- 01-11ASP.Net Core中使用枚舉類而不是枚舉的方法
- 01-11.NET CORE中比較兩個(gè)文件內(nèi)容是否相同的最快方法
- 01-11WCF如何綁定netTcpBinding寄宿到控制臺應(yīng)用程序詳解
- 01-11ASP.NET Core中間件計(jì)算Http請求時(shí)間示例詳解
- 01-11使用dotnet-dump 查找 .net core 3.0 占用CPU 100%的原因解析
- 01-11.NET Core 3.0中WPF使用IOC的圖文教程


閱讀排行
本欄相關(guān)
- 01-11vscode extension插件開發(fā)詳解
- 01-11VsCode插件開發(fā)之插件初步通信的方法
- 01-11如何給asp.net core寫個(gè)簡單的健康檢查
- 01-11.net core高吞吐遠(yuǎn)程方法如何調(diào)用組件
- 01-11淺析.Net Core中Json配置的自動更新
- 01-11.NET開發(fā)人員關(guān)于ML.NET的入門學(xué)習(xí)
- 01-11.NET Core 遷移躺坑記續(xù)集之Win下莫名其
- 01-11.net core webapi jwt 更為清爽的認(rèn)證詳解
- 01-11docker部署Asp.net core應(yīng)用的完整步驟
- 01-11ASP.NET Core靜態(tài)文件的使用方法
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)


