いろいろ備忘録日記

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

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


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



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


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


以下、svcファイル.
面倒なので、確認はログファイルに落として見るようにしてます。

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

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

namespace WCFMsmqHost
{
  [ServiceContract]
  public interface IMyService
  {
    [OperationContract(IsOneWay=true)]
    void GetMessage(string name);
  }
  
  public class MyService : IMyService
  {
    public void GetMessage(string name)
    {
      const string LOG_PATH = @"d:\tmp\MyServiceMsmqWasHost.log";
      
      if (!System.IO.File.Exists(LOG_PATH))
      {
        using (var f = System.IO.File.CreateText(LOG_PATH)) { f.Close(); }
      }
      
      using (var writer = System.IO.File.AppendText(LOG_PATH))
      {
        writer.WriteLine(name);
      }
    }
  }
}


次にweb.configファイル.
net.msmq用のmexバインディングは存在しないのでmexHttpBindingを利用します。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="WCFMsmqHost.MyService">
        <endpoint address="net.msmq://localhost/private/testqueue" 
                  binding="netMsmqBinding" 
                  contract="WCFMsmqHost.IMyService"
                  bindingConfiguration="NoMsmqSecurity" />
        <endpoint address="mex" 
                  binding="mexHttpBinding" 
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netMsmqBinding>
        <binding name="NoMsmqSecurity">
          <security mode="None" />
        </binding>
      </netMsmqBinding>
    </bindings>
  </system.serviceModel>
</configuration>


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

ttp://localhost/WCFMsmqHost/MyService.svc

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


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



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