wcfデュプレクス通信
5562 ワード
ずっと感じて二重は理解していないと思って、本当にとても憂鬱で、ネット上で二重のいくつかの特徴を理解して、直接コードに行って、後でプロジェクトの中で使うことができます:
サービス層:
サービスをコンソールアプリケーションにホストします(コードで実装されています):
Clientレイヤは、コンソールアプリケーションでも使用されています.
まずホストを実行する(hostレイヤリソースフォルダbin->debug:host.exeを見つける)wcfサービスを更新する際にもhostを実行する必要がある.exe、そうしないとサーバに接続できないエラーが発生します.
次に、サービスリファレンスのアドレスバー入力を追加します.http://localhost:991/DuplexHelloアプリケーションサービス
再適用後のクライアントapp.configは、次のコードを自動的に生成します.
カスタマーサービス・エンド・コンソール・アプリケーションのコード:
カスタマーサービス側の出力:
サービス側出力:
サービス層:
IDuplexHello
[ServiceContract(
Name = "DuplexHello",
Namespace = "http://microsoft.wcf.documentation",
CallbackContract = typeof(IHelloCallbackContract), //
SessionMode = SessionMode.Required
)]
public interface IDuplexHello
{
[OperationContract]
void Hello(string greeting);
}
DuplexHello.cs
public class DuplexHello : IDuplexHello
{
public void Hello(string greeting)
{
Console.WriteLine("Caller sent: " + greeting);
Console.WriteLine("Session ID: " + OperationContext.Current.SessionId);
Console.WriteLine("Waiting two seconds before returning call.");
Thread.Sleep(2000);
var callerProxy
= OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>();
var response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting;
Console.WriteLine("Sending back: " + response);
callerProxy.Reply(response);
}
}
( servicecontract, IDuplexHello )
public interface IHelloCallbackContract
{
[OperationContract(IsOneWay = true)] // operationcontract
void Reply(string responseToGreeting);
}
(HelloCallbackContract.cs) service , ,
public class HelloCallbackContract : IHelloCallbackContract
{
public void Reply(string responseToGreeting)
{
Console.WriteLine(responseToGreeting);
}
}
サービスをコンソールアプリケーションにホストします(コードで実装されています):
private static void Main(string[] args)
{
using (var host = new ServiceHost(typeof (DuplexHello),
new Uri("http://localhost:991/DuplexHello"))) // , client ,
{
host.AddServiceEndpoint(typeof (IDuplexHello),
new NetTcpBinding(),
"net.tcp://localhost:999/DuplexHello");
var metadatbehavior =
host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadatbehavior == null)
{
metadatbehavior = new ServiceMetadataBehavior()
{
HttpGetEnabled = true
};
host.Description.Behaviors.Add(metadatbehavior);
}
host.Opened += delegate
{
Console.WriteLine(" ");
};
host.Open();
Console.Read();
}
Clientレイヤは、コンソールアプリケーションでも使用されています.
まずホストを実行する(hostレイヤリソースフォルダbin->debug:host.exeを見つける)wcfサービスを更新する際にもhostを実行する必要がある.exe、そうしないとサーバに接続できないエラーが発生します.
次に、サービスリファレンスのアドレスバー入力を追加します.http://localhost:991/DuplexHelloアプリケーションサービス
再適用後のクライアントapp.configは、次のコードを自動的に生成します.
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_DuplexHello" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:999/DuplexHello"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_DuplexHello"
contract="ServiceReference1.DuplexHello"
name="NetTcpBinding_DuplexHello">
<identity>
<userPrincipalName value="objectboy-PC\objectboy" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
カスタマーサービス・エンド・コンソール・アプリケーションのコード:
private static void Main(string[] args)
{
var hellocallbackcontract =
new HelloCallbackContract();
var instanceContext =
new InstanceContext(hellocallbackcontract); //
var duplexChannelFactory =
new DuplexChannelFactory<IDuplexHello>(instanceContext,
new NetTcpBinding()); // , tcp , ChannelFactory,
var endpointaddress =
new EndpointAddress("net.tcp://localhost:999/DuplexHello");
var proxy =
duplexChannelFactory.CreateChannel(endpointaddress); //
using (proxy as IDisposable)
{
proxy.Hello("cccccccccccccccccc");
}
Console.Read();
}
カスタマーサービス側の出力:
サービス側出力: