いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

WCF入門-017 (基本的なサンプル, IIS, WAS, net.tcp,net.pipe,net.msmqのWCFサービスを1サービスでホスト)


IIS上でnet.tcp,net.pipe,net.msmqを一つのサービスでホスト。
net.tcp,net.pipe,net.msmqのWCFサービスをホストするためには、IISでWASが有効になっていないと動作しません。



WASの有効化については、以前の記事をご参照ください。


net.msmqの場合、サービスの記述方法がちょっと特殊です。
前にnet.msmqのサンプルを記述した記事があるので、ご参照ください。


以下、svcファイル.
2つのインターフェースを定義して、サービスクラスは両方のインターフェースを実装するように
しています。(MSMQ用とそれ以外)

<%@ ServiceHost Language="C#" Debug="true" Service="WCFWasHost.MyService" %>

using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceModel;

namespace WCFWasHost
{
  [ServiceContract]
  public interface IMyNormalService
  {
    [OperationContract]
    string GetMessage(string name);
  }
  
  [ServiceContract]
  public interface IMyMsmqService
  {
    [OperationContract(IsOneWay=true)]
    void SetMessage(string name);
  }
  
  public class MyService : IMyNormalService, IMyMsmqService
  {
    public string GetMessage(string name)
    {
      return string.Format("Hello '{0}'", name);
    }
    
    public void SetMessage(string name)
    {
      const string LOG_PATH = @"d:\tmp\MyServiceMsmqWasHost.log";
      
      if (!File.Exists(LOG_PATH))
      {
        using (var f = File.CreateText(LOG_PATH)) { f.Close(); }
      }
      
      using (var writer = File.AppendText(LOG_PATH))
      {
        writer.WriteLine(name);
      }
    }
  }
}


次にweb.configファイル.
MSMQ用とそれ以外でエンドポイントで指定するcontractを変更しています。
ついでにmexエンドポイントも3種類用意。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="WCFWasHost.MyService" behaviorConfiguration="MyServiceBehavior">
        <!-- HTTP -->
        <endpoint address="" 
                  binding="basicHttpBinding" 
                  contract="WCFWasHost.IMyNormalService"
                  name="epHttp" />
        <!-- TCP -->
        <endpoint address="" 
                  binding="netTcpBinding" 
                  contract="WCFWasHost.IMyNormalService"
                  name="epTcp" />
        <!-- Named Pipe -->
        <endpoint address="" 
                  binding="netNamedPipeBinding" 
                  contract="WCFWasHost.IMyNormalService"
                  name="epPipe" />
        <!-- MSMQ -->
        <endpoint address="net.msmq://localhost/private/testqueue" 
                  binding="netMsmqBinding" 
                  contract="WCFWasHost.IMyMsmqService" 
                  bindingConfiguration="NoMsmqSecurity" 
                  name="epMsmq" />
        
        <!-- MEX -->
        <endpoint address="mex" binding="mexHttpBinding"      contract="IMetadataExchange" />
        <endpoint address="mex" binding="mexTcpBinding"       contract="IMetadataExchange" />
        <endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug    includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netMsmqBinding>
        <binding name="NoMsmqSecurity">
          <security mode="None" />
        </binding>
      </netMsmqBinding>
    </bindings>
  </system.serviceModel>
</configuration>


後は、IISマネージャにて、該当Webアプリの有効プロトコルに「net.tcp,net.pipe,net.msmq」を追加し
WcfTestClientなどで、

ttp://localhost/WCFWasHost/MyService.svc
net.tcp://localhost/WCFWasHost/MyService.svc
net.pipe://localhost/WCFWasHost/MyService.svc

などとすると、動作していることが確認できます。


サンプルは以下の場所にアップしてあります。
https://sites.google.com/site/gsfzero1/Home/WCFSample-016.zip?attredirects=0&d=1



================================
過去の記事については、以下のページからご参照下さい。