2011-09-08 11 views
86
私はWCF名前付きパイプの最小限の例を探しています

(私は名前付きパイプを介して通信することができ、2つの最小限のアプリケーション、サーバとクライアントを期待しています。)WCF名前付きパイプ最小限の例

Microsoftはブリリアントの記事がありますGetting Started TutorialはHTTP経由のWCFを記述しています.WCFと名前付きパイプについても同様のものを探しています。

私はインターネットでいくつかの投稿を見つけましたが、少し "高度な"ものです。私は何か最小限のものを必要とし、義務的な機能しか必要としないので、自分のコードを追加してアプリケーションを動かすことができます。

名前付きパイプを使用するにはどうすればよいですか?

<endpoint address="http://localhost:8000/ServiceModelSamples/Service/CalculatorService" 
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator" 
    contract="ICalculator" name="WSHttpBinding_ICalculator"> 
    <identity> 
     <userPrincipalName value="OlegPc\Oleg" /> 
    </identity> 
</endpoint> 

名前付きパイプを使用するにはどうすればよいですか?

// Step 1 of the address configuration procedure: Create a URI to serve as the base address. 
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service"); 

// Step 2 of the hosting procedure: Create ServiceHost 
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); 

try 
{ 
    // Step 3 of the hosting procedure: Add a service endpoint. 
    selfHost.AddServiceEndpoint(
     typeof(ICalculator), 
     new WSHttpBinding(), 
     "CalculatorService"); 

    // Step 4 of the hosting procedure: Enable metadata exchange. 
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
    smb.HttpGetEnabled = true; 
    selfHost.Description.Behaviors.Add(smb); 

    // Step 5 of the hosting procedure: Start (and then stop) the service. 
    selfHost.Open(); 
    Console.WriteLine("The service is ready."); 
    Console.WriteLine("Press <ENTER> to terminate service."); 
    Console.WriteLine(); 
    Console.ReadLine(); 

    // Close the ServiceHostBase to shutdown the service. 
    selfHost.Close(); 
} 
catch (CommunicationException ce) 
{ 
    Console.WriteLine("An exception occurred: {0}", ce.Message); 
    selfHost.Abort(); 
} 

名前付きパイプを使用するクライアントを生成するにはどうすればよいですか?

+1

あなたはhttp://stackoverflow.com/questions/184878/expose-a-wcf-service-through-a-named-pipes-bindingを見ましたか? –

+1

まだそれを取得していません... – javapowered

答えて

78

ただちにthis excellent little tutorialが見つかりました。壊れたリンクCached version

私もいいですが、私はマイクロソフトのチュートリアルに従っていましたが、パイプも必要でした。

ご覧のとおり、設定ファイルとそのすべての面倒なものは必要ありません。

ところで、彼はHTTPとパイプの両方を使用します。 HTTPに関連するすべてのコード行を削除するだけで、純粋なパイプの例が得られます。

+2

ありがとう!また、ハードコードされた設定ではなく設定用にweb.configを使用するサービスを構築しようとするときは、次のマイクロソフトの例を参照してください。http://msdn.microsoft.com/en-us/library/ms752253.aspx – Nullius

+3

リンクはありません他の場所でチュートリアルですか? – user1069816

+0

そのリンクはまさに​​私が必要としたものでした。ありがとうございました。 – Patrick

52

これを試してください。

ここにサービスパーツがあります。ここで

[ServiceContract] 
public interface IService 
{ 
    [OperationContract] 
    void HelloWorld(); 
} 

public class Service : IService 
{ 
    public void HelloWorld() 
    { 
     //Hello World 
    } 
} 

はプロキシ

public class ServiceProxy : ClientBase<IService> 
{ 
    public ServiceProxy() 
     : base(new ServiceEndpoint(ContractDescription.GetContract(typeof(IService)), 
      new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/MyAppNameThatNobodyElseWillUse/helloservice"))) 
    { 

    } 
    public void InvokeHelloWorld() 
    { 
     Channel.HelloWorld(); 
    } 
} 

そして、ここでは一部のホスティングサービスです。

var serviceHost = new ServiceHost 
     (typeof(Service), new Uri[] { new Uri("net.pipe://localhost/MyAppNameThatNobodyElseWillUse") }); 
    serviceHost.AddServiceEndpoint(typeof(IService), new NetNamedPipeBinding(), "helloservice"); 
    serviceHost.Open(); 

    Console.WriteLine("Service started. Available in following endpoints"); 
    foreach (var serviceEndpoint in serviceHost.Description.Endpoints) 
    { 
     Console.WriteLine(serviceEndpoint.ListenUri.AbsoluteUri); 
    } 
+0

ありがとう、私は見てみましょう... – javapowered

+0

これは動作するかもしれませんが、 .configファイルをクライアントとサーバのために使用します。 –

+7

app.configファイルを介してアプリケーションの詳細を公開することはしばしば望ましくありません。 –

11

highly simplified Echo exampleをチェックアウト: 基本的なHTTP通信を使用するように設計されていますが、簡単に、クライアントとサーバーのためapp.configをファイルを編集して、名前付きパイプを使用するように変更することができます。以下の変更を行います。

編集サーバのapp.configファイル、削除またはhttpをコメントアウトBASEADDRESSエントリーをして、新しいBASEADDRESSに名前付きパイプ用エントリを追加する(net.pipeと呼ばれます)。あなたは、通信プロトコルにHTTPを使用して予定がない場合も、必ずserviceMetadata作るserviceDebugいずれかコメントアウトまたは削除されています

<configuration> 
    <system.serviceModel> 
     <services> 
      <service name="com.aschneider.examples.wcf.services.EchoService"> 
       <host> 
        <baseAddresses> 
         <add baseAddress="net.pipe://localhost/EchoService"/> 
        </baseAddresses> 
       </host> 
      </service> 
     </services> 
     <behaviors> 
      <serviceBehaviors></serviceBehaviors> 
     </behaviors> 
    </system.serviceModel> 
</configuration> 

編集クライアントのapp.configファイルので、 basicHttpBindingであることのいずれかをコメントアウトまたは削除し、netNamedPipeBindingエントリが追加されます。

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <netNamedPipeBinding> 
       <binding name="NetNamedPipeBinding_IEchoService"/> 
      </netNamedPipeBinding> 
     </bindings> 
     <client> 
      <endpoint address    = "net.pipe://localhost/EchoService" 
         binding    = "netNamedPipeBinding" 
         bindingConfiguration = "NetNamedPipeBinding_IEchoService" 
         contract    = "EchoServiceReference.IEchoService" 
         name     = "NetNamedPipeBinding_IEchoService"/> 
     </client> 
    </system.serviceModel> 
</configuration> 

上記の例では、唯一の名前付きパイプで実行されますが、何もあなたのサービスを実行するために、複数のプロトコルを使用してからあなたを停止されていない:あなたはまた、パイプを使用するようにエンドポイントエントリを変更する必要があります。 AFAIKでは、名前付きパイプとHTTP(および他のプロトコル)の両方を使用して、サーバーにサービスを実行させることができます。

また、クライアントのapp.configファイルのバインディングは非常に単純化されています。あなたが調整することができ、多くの異なるパラメータが、私は、インターネット上の別の検索結果から、この単純な例を作成し

+3

リンクが死んでいます。 –

0

...さておきだけBASEADDRESSを指定してから、があります。

public static ServiceHost CreateServiceHost(Type serviceInterface, Type implementation) 
{ 
    //Create base address 
    string baseAddress = "net.pipe://localhost/MyService"; 

    ServiceHost serviceHost = new ServiceHost(implementation, new Uri(baseAddress)); 

    //Net named pipe 
    NetNamedPipeBinding binding = new NetNamedPipeBinding { MaxReceivedMessageSize = 2147483647 }; 
    serviceHost.AddServiceEndpoint(serviceInterface, binding, baseAddress); 

    //MEX - Meta data exchange 
    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); 
    serviceHost.Description.Behaviors.Add(behavior); 
    serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), baseAddress + "/mex/"); 

    return serviceHost; 
} 

上記のURIを使用して、クライアントの参照をWebサービスに追加できます。

関連する問題